943,640 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jun 26th, 2009
0

Centering left-aligned text using CSS

Expand Post »
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:
html Syntax (Toggle Plain Text)
  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:
css Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 11
Solved Threads: 3
Light Poster
PopeJareth is offline Offline
29 posts
since Jun 2009
Jun 27th, 2009
0

Re: Centering left-aligned text using CSS

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.
Reputation Points: 16
Solved Threads: 9
Junior Poster
ingeva is offline Offline
106 posts
since Jul 2008
Jun 28th, 2009
0

Re: Centering left-aligned text using CSS

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.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
Jun 29th, 2009
0

Re: Centering left-aligned text using CSS

Click to Expand / Collapse  Quote originally posted by MidiMagic ...
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.
Reputation Points: 16
Solved Threads: 9
Junior Poster
ingeva is offline Offline
106 posts
since Jul 2008
Jun 30th, 2009
0

Re: Centering left-aligned text using CSS

Click to Expand / Collapse  Quote originally posted by MidiMagic ...
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 =)
Reputation Points: 11
Solved Threads: 3
Light Poster
PopeJareth is offline Offline
29 posts
since Jun 2009
Jun 30th, 2009
0

Re: Centering left-aligned text using CSS

I think I have come up with a small hackjob of a solution. If I add
css Syntax (Toggle Plain Text)
  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?
Reputation Points: 11
Solved Threads: 3
Light Poster
PopeJareth is offline Offline
29 posts
since Jun 2009
Jul 1st, 2009
1

Re: Centering left-aligned text using CSS

Click to Expand / Collapse  Quote originally posted by PopeJareth ...
I think I have come up with a small hackjob of a solution. If I add
css Syntax (Toggle Plain Text)
  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.
Reputation Points: 16
Solved Threads: 9
Junior Poster
ingeva is offline Offline
106 posts
since Jul 2008
Jul 2nd, 2009
0

Re: Centering left-aligned text using CSS

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 =)
Reputation Points: 11
Solved Threads: 3
Light Poster
PopeJareth is offline Offline
29 posts
since Jun 2009
Jul 3rd, 2009
0

Re: Centering left-aligned text using CSS

Click to Expand / Collapse  Quote originally posted by PopeJareth ...
Thanks ingeva =)
Maybe it can be easier once css3 comes out =)
Let's hope so!
Maybe someone else can simplify this, even before CSS3!
Reputation Points: 16
Solved Threads: 9
Junior Poster
ingeva is offline Offline
106 posts
since Jul 2008
Jul 8th, 2009
0

Re: Centering left-aligned text using CSS

Put the text in a container, and center the container.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: Quick Check Missing Tag
Next Thread in HTML and CSS Forum Timeline: div does not inherit correct width





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC