Forum Moderators: not2easy

Message Too Old, No Replies

vertical-align

alternatives to vertical-align

         

crow976

2:26 pm on Jul 18, 2003 (gmt 0)

10+ Year Member



I am just starting to play around with css positioning in order to move away from using tables for my sites layout. It seems that the vertical-align property doesn't work at all in IE 5.5, at least for div objects. So I am wondering how you can get to do some vertical alignment (to replace the much used valign="middle" attribute in tables) with div or other objects...but not tables. The only thing I can think of is using some padding attibutes... but im not sure that can work in all cases..

DrDoc

4:43 pm on Jul 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World!

There are a few things you need to understand about the vertical-align CSS property:

It was never meant to reproduce the valign attribute for tables. It doesn't affect how things are vertically aligned within the element itself. Instead, it aligns the element in reference to surrounding elements.

If you need something that reproduces valign=bottom, for example, then absolute positioning (with bottom set to 0) is what you need.

CSS was designed to allow for fluid layouts that gracefully adjust themselves to whatever basic rules you have set for the flow of the page.

CSS is a new way of thinking. You're not doing yourself a favor by trying to apply CSS to HTML thinking, trying to reproduce HTML behavior.

crow976

5:26 pm on Jul 18, 2003 (gmt 0)

10+ Year Member



It'll probably take me a while before I stop thinking in html. What I'm doing here is just try to do random stuff using css and see if I succeed..

For example, I have created a small page with a 250x250px box in the center of the page (using some css trick). I put a paragraph in that box, and i'd like to have that paragraph vertically centered in the box. How would that be acheived using css?

here's the code:

<html>
<head>
<title>css test</title>
<style type="text/css">

body {
margin: 0px;
}

div#test {
background-color: #689475;
width: 250px;
height: 250px;
position: relative;
top: 50%;
left: 50%;
margin: -125 px;
}

p {
font-family: tahoma;
font-size: 12px;
color: #ffffff;
}

</style>

</head>

<body>

<div id="test">
<p>How can I make this paragraph vertically centered in the box?</p>
</div>

</body>

</html>

amznVibe

6:20 pm on Jul 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



most css2 browsers, and this means NOT IE, can do display:table-cell on a DIV which will let you use vertical-align... otherwise you are out of luck doing this

I came up with a very nasty hack that lets you take advantage of the fact that spans will vertical align with the text to the left of it. By setting the font-size of the DIV to the height of the DIV, you can use spans within the DIV and tell them to vertical-align:middle (but no other element type) But it causes all sorts of other problems and your needs and goals have to be simple.

Sorry. Google this to death, you'll see there is no easy answer. Most work-arounds require javascript, and you might as well use a table instead of that.

change this core of your code to see my nasty hack (but don't bother to use it in real-world)

div#test {
background-color: #689475;
width: 250px;
height: 250px;
position: relative;
top: 50%;
left: 50%;
margin: -125 px;
line-height:200px;
font-size:200px;
}

.text {
font-family: tahoma;
font-size: 12px;
color: #ffffff;
vertical-align:middle;
line-height:100%;
}
</style>
</head>

<body>
<div id="test">
<span class=text>How can I make this paragraph vertically centered in the box?</span>
</div>