tags:

views:

76

answers:

6

Example of problem

I'm trying to recode one of my older forms. It was filled with tables that I want to replace with CSS. However I'm having trouble having text and a form element aligned vertically together. As the picture shows the text defaults to starting at the top instead of in the middle. The blue highlights around the row is dreamweavers interpretation / selection of what is going on.

I have label and input divs, both floated left, inside a div called #light, which is inside a general container. This is what my css code looks like:

#contentBox{
 width: 600px;
 float: left;
 background-color: #e2e2e2;
 overflow: auto;
 border-color: #c5c5c5;
 border-width: 1px;
 border-style: solid;
 font-size: 12px;
}
#light {
 float: left;
 width: 500px;
 padding: 15px;
 background-color: #e2e2e2;
 margin: 7px;
 border-color: #c5c5c5;
 border-width: 1px;
 border-style: solid;
 vertical-align: middle;
}
input {
 float: right;
 width: 20em;

}
label {
 float: left;
 text-align: right;
 vertical-align: baseline;
}

Any idea what the problem is? I've tried swapping around the vertical-align in different divs, floating in different directions, getting rid of the label but I just end up with more problems rather than less.

+3  A: 

You cannot use vertical-align on elements unless they are table cells (or displayed as such) as this article explains. Set line-height to the element height if you've only got one row of text.

You
A: 

Vertical alignment of text can be incredibly annoying or incredibly easy.

If the size of all the involved elements are known, your best bet is to set manual padding/margins on the text itself to make sure it's aligned.

If the content you want to center vertically is dynamic, this is your best bet.

Andres Freyria
A: 

Not sure, but your input tag is set to "float:right", so its height won't be taken into account by the parent. Hence, the height of the parent is actually probably the height of the label (I suspect dreamweaver is not interpreting correctly what browsers do.) Try to remove the float on the input tag and see if it makes a difference.

R. Hill
+1  A: 

Usually, to solve that problem, I use the line-height property:

Ex:

div{width:600px;font:normal normal 12px/30px Arial, Helvetica, sans-serif;}

This will set the font to 12px, and the line-height to 30px, keeping the font vertically align within the 30px of its line.

Zuul
This does solve the problem of the text not being aligned, but now the input box will be off center. For some reason setting the input div's height to the same as the the line-height doesn't work, one or the other will be slightly off-center
Imagine that your input as a text of 12px, it will need some padding to avoit text inside to colapse with the input border, so, add padding:3px;, then, to a line-weight of 30px, you still need 12px (12px + 3px + 3px = 18px) add for example margin:6px 0; (if you need, will post a working sample!
Zuul
A: 

Vertical alignment can be applied only to inline elements. The best solution is to modify your HTML and make it like in this examples

cesarnicola
Wrong. Vertical alignment only works on elements with `display: table-cell`.
You
A: 

You could go for a 'cheap' solution and apply a padding-top to the label divs.

Slevin