Forum Moderators: not2easy

Message Too Old, No Replies

Vertically centre align text

The usual CSS question

         

SilverLining

12:10 pm on Mar 20, 2007 (gmt 0)

10+ Year Member



I am trying to vertically align text inside a div to ensure that short text will line up nicely along an image.

The parent div's position is set to relative and then the inside div's position is set to absolute. I have read that one needs to set display to table or table-cell for the

vertical-align: middle
property to work. Something tells me that I shouldn't need
top:50%
as well as
margin:auto
...

This should be an easy fix, but I know that I'm overlooking something simple:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Intro page</title>
<style type="text/css">
.box {height: 150px; font-weight: bold; position: relative; border: 1px solid red; width: 600px;}
.box .intro {position: absolute; top: 50%; left: 315px; margin: auto; display: table-cell; border: 1px solid green; vertical-align: middle;}
</style>
</head>
<body>
<div class="box">
<img src="blah.gif" width="291px" height="148px" border="1" alt="" />
<div class="intro">
Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text
Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text
</div>
</div>
</body>
</html>

Thank you for any assistance.

simonuk

2:52 pm on Mar 20, 2007 (gmt 0)

10+ Year Member



Many ways of doing it. A more simple approach would be:

<style type="text/css">
<!--
.box {font-weight: bold;border: 1px solid red;width: 600px;height: 148px;}

img {padding: 0;margin: 0em 1em 0em 0em;}

p {margin: 0;padding: 0;}

.left {float: left;}

-->
</style>

html:

<div class="box">
<img src="blah.gif" alt="" width="291" height="148" border="1" class="left" /><p>Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text <br>
Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text Intro text </p>
</div>

SilverLining

3:18 pm on Mar 20, 2007 (gmt 0)

10+ Year Member



Hi simonuk. Thank you for your reply. The content inside my original
.intro
div is dynamic, and for this reason I didn't set a height on that div. What I am trying to achieve is to force the top and bottom margin (above and below .intro) to be the same height and so aligning it in the centre, vertically. The max height will be the same as the image.

I cannot see how your float: left does this. Could you please clarify? Thanks.

simonuk

4:54 pm on Mar 20, 2007 (gmt 0)

10+ Year Member



Ahhh sorry, misunderstood your question.

If no-one replies I'll answer it tomorrow (about to leave work ;-) )

SilverLining

10:15 am on Mar 21, 2007 (gmt 0)

10+ Year Member



Just found this:

Specify the line height to be the same as the height of the box itself in the CSS.

This would work if the text was only one line, however the text spans multiple lines.

Any other ideas?