Centering left-aligned text using CSS

Thread Solved

Join Date: Jun 2009
Posts: 28
Reputation: PopeJareth is an unknown quantity at this point 
Solved Threads: 3
PopeJareth PopeJareth is offline Offline
Light Poster

Centering left-aligned text using CSS

 
0
  #1
Jun 26th, 2009
I am working on a page that has 3 columns of text. I am trying to center the text in each column while maintaining the left justification for the text.

When I try to use margin-left:auto; margin-right:auto; the spacing for the columns is destroyed. I have also tried putting a div around the paragraphs and tried to center that, but either I am doing something wrong or it isn't affecting anything.

Here is a link to a page that shows the text and how they are not working. http://pages.suddenlink.net/salisburra/index2.html

Thanks so much for any help you can provide =)


This is the html from the page:
  1. ...
  2. <body>
  3. <div class="tripletBox linkBlock">
  4. <p>
  5. Suggested Itinerary<br />
  6. Brochures
  7. </p>
  8. </div>
  9. <div class="tripletBox linkBlock">
  10. <p>
  11. Lesson Plans<br />
  12. Cool Sites
  13. </p>
  14. </div>
  15. <div class="tripletBox linkBlock">
  16. <p>
  17. Featured Images<br />
  18. Opportunities
  19. </p>
  20. </div>
  21. </body>

Here is the css applying to the html:
  1. body {
  2. margin-left: auto;
  3. margin-right: auto;
  4. min-width:1020px;
  5. max-width:1020px;
  6. ...
  7. }
  8.  
  9. .tripletBox{
  10. float:left;
  11. width:26.36%;
  12. margin-left:3.48%;
  13. margin-right:3.48%;
  14.  
  15. }
  16.  
  17.  
  18. .tripletBox > * {/*centers images in the tripletBox container*/
  19. display:block;
  20. margin-left:auto;
  21. margin-right:auto;
  22. }
  23.  
  24. .linkBlock {
  25. ...
  26. text-align:left;
  27. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 101
Reputation: ingeva is an unknown quantity at this point 
Solved Threads: 9
ingeva ingeva is offline Offline
Junior Poster

Re: Centering left-aligned text using CSS

 
0
  #2
Jun 27th, 2009
I think I understand what you're trying to do.

You are using a "double" class:
HTML and CSS Syntax (Toggle Plain Text)
  1. <div class="tripletBox linkBlock">

I don't think you can do that.

I think you should define 3 DIVS, for instance width 33% for each.
Keep the "margin:auto" definitions.
Inside each of them, place a new block where the text-alignment is "left".
For instance:
HTML and CSS Syntax (Toggle Plain Text)
  1. <div class="tripletBox">
  2. <div class="linkBlock">
  3. Your text
  4. </div></div>

Other suggestion: Remove the definition of body width. The body should fill the window, and it will if you omit it.


I haven't tried this, but I think it should work.
Last edited by ingeva; Jun 27th, 2009 at 7:00 am.
Yes, I'm running Ubuntu. Of course.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Centering left-aligned text using CSS

 
0
  #3
Jun 28th, 2009
I use multiple classes all the time. As long as they don't contradict each other, it works great.

Use text-align: center; to center text. The margin works on everything else, but not text.

Remember that any surrounding styles (margin, border, padding) are rendered OUTSIDE any size style (width, height) you define. So you need to leave extra room for the surrounding styles to fit into.
Last edited by MidiMagic; Jun 28th, 2009 at 8:59 pm.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 101
Reputation: ingeva is an unknown quantity at this point 
Solved Threads: 9
ingeva ingeva is offline Offline
Junior Poster

Re: Centering left-aligned text using CSS

 
0
  #4
Jun 29th, 2009
Originally Posted by MidiMagic View Post
I use multiple classes all the time. As long as they don't contradict each other, it works great.
OK! I'll take that as a tip. Wouldn't have thought it was possible. You learn something every day.
Yes, I'm running Ubuntu. Of course.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: PopeJareth is an unknown quantity at this point 
Solved Threads: 3
PopeJareth PopeJareth is offline Offline
Light Poster

Re: Centering left-aligned text using CSS

 
0
  #5
Jun 30th, 2009
Originally Posted by MidiMagic View Post
Use text-align: center; to center text. The margin works on everything else, but not text.
That was my first attempt, but using text-align:center made the text center but with out a left justified look

ex
 
                   ------
               ------------
                   -------

instead of
                   ---------
                   ---------
                   ---------


I also feel that Ingeva's idea should work, but when I actually implement that, nothing changes.

Thanks again for your help =)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: PopeJareth is an unknown quantity at this point 
Solved Threads: 3
PopeJareth PopeJareth is offline Offline
Light Poster

Re: Centering left-aligned text using CSS

 
0
  #6
Jun 30th, 2009
I think I have come up with a small hackjob of a solution. If I add
  1. .linkBlock p{
  2. margin-left:80px;
  3. }

Then the text is lined up with their above headers, and nothing strange is happening with the floating divs.

What do you guys think about this (slightly tacky) solution?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 101
Reputation: ingeva is an unknown quantity at this point 
Solved Threads: 9
ingeva ingeva is offline Offline
Junior Poster

Re: Centering left-aligned text using CSS

 
1
  #7
Jul 1st, 2009
Originally Posted by PopeJareth View Post
I think I have come up with a small hackjob of a solution. If I add
  1. .linkBlock p{
  2. margin-left:80px;
  3. }

What do you guys think about this (slightly tacky) solution?
I'm sorry I haven't looked at this. It might work.
But I think I wasn't clear enough in my first post.
If I may say so, your stylesheets looked like overkill, but I realize
that you also have other things in mind with your HTML.

I have re-written your HTML and stylesheet a little. I've used borders and background colors to indicate "what's what". I ignored the text images and use text instead. You can use images of course, but text is faster and more elegant. You can still use varying colors.

I try to avoid fixed sizes, like font sizes in pixels. I do that only once: in the body style. Otherwise I specify sizes as "em" or percent. With my example, vary the width of the window and see how everything follows. This reduces horizontal scrolling, but be aware that you can get some folding of DIVs that are too wide in a narrow window.

A design should be as simple as possible. Here I ended up with 4 levels of DIV, which I really think is too much, but an easier option (to write) is with a table, and that's not always considered "kosher".

My suggestion can be found here:

http://intertrafico.com/helper

Hope you can make it work just the way you want it.
Last edited by ingeva; Jul 1st, 2009 at 4:35 pm.
Yes, I'm running Ubuntu. Of course.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: PopeJareth is an unknown quantity at this point 
Solved Threads: 3
PopeJareth PopeJareth is offline Offline
Light Poster

Re: Centering left-aligned text using CSS

 
0
  #8
Jul 2nd, 2009
Thanks ingeva =)

This works wonderfuly. Too bad there isn't a simpler soultion. (that is concidered "kosher")

Maybe it can be easier once css3 comes out =)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 101
Reputation: ingeva is an unknown quantity at this point 
Solved Threads: 9
ingeva ingeva is offline Offline
Junior Poster

Re: Centering left-aligned text using CSS

 
0
  #9
Jul 3rd, 2009
Originally Posted by PopeJareth View Post
Thanks ingeva =)
Maybe it can be easier once css3 comes out =)
Let's hope so!
Maybe someone else can simplify this, even before CSS3!
Yes, I'm running Ubuntu. Of course.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Centering left-aligned text using CSS

 
0
  #10
Jul 8th, 2009
Put the text in a container, and center the container.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the HTML and CSS Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC