Forum Moderators: not2easy

Message Too Old, No Replies

Vertical align

Can't align text in vertical

         

LetItBe

3:34 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



Hi.
I have a problem. There's a div and I want text to be aligned to middle of this div.
How?
Thanks.

Adam5000

5:12 am on Jun 10, 2006 (gmt 0)

10+ Year Member



I'm not sure I understand your question but using the code below, you can place a text div anywhere on the screen by changing the top and left numbers in the inline style sheet. Hope this helps.

<html>
<head>
<title>place div</title>
</head>
<body>
<div id="div_one" style="top: 100px; position: absolute; left: 150px;">
<p>Text div</p>
</div>
</body>
</html>

LetItBe

7:34 am on Jun 10, 2006 (gmt 0)

10+ Year Member



No.
I want to put a text in div. Text should be aligned in the middle of the div.

<table>
<tr>
<td valign="middle">
some text here
</td>
</tr>
</table>

Here's text's alignment without CSS. But I want to have similar effect with CSS. Don't know how.

Vishal

8:13 am on Jun 10, 2006 (gmt 0)

10+ Year Member



Hello LetitBe,

If I am not wrong, 100% css layout don't support vertial-align=middle property. However there is a work-around using linewidth, don't remember it right now though :(

*edit*
Here is the work-around link : [student.oulu.fi...] (not sure if mods will allow it.. but if they keep it, the link is pretty handy)

LetItBe

8:22 am on Jun 10, 2006 (gmt 0)

10+ Year Member



Thanks, Vishal, for useful information.

(and who said css is all-mighty...)

ibilly

10:04 pm on Jun 14, 2006 (gmt 0)



u must use table

doodlebee

4:21 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



No.
I want to put a text in div. Text should be aligned in the middle of the div.

<table>
<tr>
<td valign="middle">
some text here
</td>
</tr>
</table>

Here's text's alignment without CSS. But I want to have similar effect with CSS. Don't know how.

Unfortunately, this is a hard thing to do with CSS2. (CSS3 is supposed to have this problem resolved...but that's not the issue now, is it? ;) )

To do it with text, you must simply do something like so:

<div id="wrapper">
<p>some text here</p>
</div>

The CSS to go along with that would be:

#wrapper {
height:100px;
vertical-align:middle;
}

p {
font-size:10em;
line-height:10em;
}

This'll make your text align to the center of the div. The thing is that the line-height and font size need to be the same.

Also, this only works with one line of text. If the text ends up wrapping, it won't work. This also does not work with stuff like images and divs - only text.

However, an alternate workaround [jakpsatweb.cz] was posted on the CSS-D list a while back, and I've found it works very nicely.