Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vertically align a label and a textbox in all browsers

Is there an easy way to align a label with a textbox in all major browsers (i.e. including IE7? I set display to inline-block on my label and textbox which seems to work everywhere but in IE7, where the label is on the bottom of the div.

<label class="inputLabel" for="emailTextBox">
            Email:</label>
<input class="textBox" type="text" id="emailTextBox" value=" Email address" />

input.textBox 
{
    margin-top: 5px;
    margin-bottom: 5px;
    height: 30px;
    width: 350px;
    font-size: 15px;
    font-family: Verdana;
    line-height: 30px;
    display:inline-block; 
}

label.inputLabel
{
    font-family: Verdana;
    font-size: 15px;
    line-height: 30px;
    display:inline-block;
}
like image 678
Joe Cartano Avatar asked Mar 03 '12 00:03

Joe Cartano


1 Answers

The display:inline-block declaration is not fully supported by IE7 and below, so you have to use display:inline instead in conjunction with the zoom:1 hasLayout hack to imitate the declaration with the star hack to target IE7. To align both the textbox and labelwe can use the vertical-align:middle property, like so:

CSS

input.textBox {
    margin-top: 5px;
    margin-bottom: 5px;
    height: 30px;
    width: 350px;
    font-size: 15px;
    font-family: Verdana;
    line-height: 30px;
    display:inline-block; 
    *display: inline;     /* for IE7*/
    zoom:1;              /* for IE7*/
    vertical-align:middle;
}

label.inputLabel {
    font-family: Verdana;
    font-size: 15px;
    line-height: 30px;
    display:inline-block;
    *display: inline;     /* for IE7*/
    zoom:1;              /* for IE7*/
}

Demo

like image 161
Andres Ilich Avatar answered Oct 06 '22 01:10

Andres Ilich