Vertical and Horizontal Alignment of Text in a DIV
Posted: 27 June 2009 07:13 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  40
Joined  09-11-2007

Working on my first CSS layout has been humbling! There’s lots to learn. I’d like to vertically and horizontally align text in a DIV. Some of the text will need to wrap to a second line.  Any suggestions? Or links to a tutorial?  Thanks!

 Signature 

ExpressionEngine 1.6.7 Build: 20090515

Profile
 
 
Posted: 28 June 2009 09:11 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  282
Joined  06-15-2008

Everett,

You can use the text-align property in CSS to get your text the way you want it (try values of “left,” “right,” and “center”).

There is a vertical-align property, but it’s buggy in IE and Firefox and (I think) only works in tables. Read more about it at the Sitepoint CSS Reference. Vertical alignment is really only useful if you’re working inside a table, in my opinion.

You could set the display of your DIV to “inline” to try and get vertical-align to work, but (if I remember correctly) then your text-align wouldn’t work.

When I need to vertically position text, I usually set the text I want to position as a block level element and add some margin or padding to the top.

 Signature 

RYAN BURNEY • Lead Web Developer, 3 Roads Media
Denver based web and graphic design

Profile
 
 
Posted: 29 June 2009 07:07 AM   [ Ignore ]   [ # 2 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1027
Joined  12-18-2008

yeah vertical-align is hard to use properly. if you can’t use top/bottom padding or line height to center what you want (doesn’t work well with variable height content), then you’re pretty much stuck using a single row/column table.

 Signature 

EE Pro Network
eMarketSouth - web design & development, custom EE plugins & extensions, SEO, and more

Profile
 
 
Posted: 29 June 2009 02:33 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  40
Joined  09-11-2007

Thanks for the info! The Sitepoint CSS Reference was helpful. It had the most up-to-date notes on browser compatibility that I have seen. I decided to go with a table with a single column; it seems to be the best choice for this situation.

 Signature 

ExpressionEngine 1.6.7 Build: 20090515

Profile
 
 
Posted: 01 July 2009 07:19 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  145
Joined  12-29-2008

If you know the height of the div you want to center vertically, you can do this…

<html>
<
head>
<
title>Vertical Align</title>

<
style type="text/css">
body {margin:0;padding:0;}
#main {height:400px;width:400px; position:relative;}
#sub {margin:auto 0; position:absolute; top:0; bottom:0; height:50px;}
p {text-align:center;}
</style>

</
head>
<
body>
    
    <
div id="main">
        <
div id="sub">
            <
p>Some Text. Lorem impsum...blah blah. Text. Lorem impsum...blah blah. Text. Lorem impsum...blah blah</p>
        </
div>
    </
div>
</
body>
</
html>
Profile
 
 
Posted: 02 July 2009 10:44 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  40
Joined  09-11-2007

Absolute positioning inside relative positioning? I would have never thought to have tried that. Interesting!

 Signature 

ExpressionEngine 1.6.7 Build: 20090515

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 02:47 PM
Total Registered Members: 114987 Total Logged-in Users: 89
Total Topics: 122430 Total Anonymous Users: 49
Total Replies: 647270 Total Guests: 440
Total Posts: 769700    
Members ( View Memberlist )