Search code examples
htmlcsstextinput

Aligning labels below text input


I am trying to align labels in a form to go below a text input box. I have adapted my CSS from here.

It displays perfectly in IE 9, but I am having trouble with firefox, chrome and others.

Here's what it looks like in IE:

IE

And this is what it looks like in firefox:

enter image description here

It looks much worst in chrome, but I would like to get it working in firefox first.

The main concerns are:

  • The text input should be centered above the labels.
  • The colons should be vertically aligned with the text inputs.
  • The drop-down should be vertically aligned with the text inputs.

Here's my markup:

<form action="" method="post">

    <label for="time">Time settings</label>

    <div class="description">
        Description goes here
    </div>

    <span><label for="time">Hour</label> <input type="text" id="time" name="time" maxlength="2" size="2"></span>
    <span>:</span>
    <span><label for="time-minute">Minute</label> <input type="text" id="time-minute" name="time-minute" maxlength="2" size="2"></span>
    <span>:</span>
    <span><label for="time-seconds">Seconds</label> <input type="text" id="time-seconds" name="time-seconds" maxlength="2" size="2"></span>

    <span>
        <select id="time-ampm" name="time-ampm">
            <option value="am">AM</option>
            <option value="pm">PM</option>
        </select>
    </span>
    </form>

And here is the CSS style:

<style>
    span{
        display: inline-block;
    }

    span input { 
        display: block; 
        position: relative; 
        top: -3em; 
        text-align: center;
    } 

    span label { 
        display: block; 
        padding-top: 1.5em; 
        text-align: center;
    } 
</style>

I am also using the HTML 4.01 strict doctype.

Here's the form live in jsfiddle: http://jsfiddle.net/VuShK/

Can anyone give me some pointers as to how to fix this?

Solution: Thanks to everyone who replied.

Here is my final solution with the text inputs being centered. Thanks to AVD for his help.

<style> 
    span{ 
        display: inline-block; 
        position: relative;
        top: -1em;
        text-align:center;
    } 

    span select input {  
        display: block;  
        position: relative;  
        top: -3em;  
    }  

    span label {  
        display: block;  
        position: relative;  
        top:2.7em; 
        text-align: center; 
    }  
</style> 

Solution

  • Try this,

    <style>
        span{
            display: inline-block;
        }
    
        span select input { 
            display: block; 
            position: relative; 
            top: -3em; 
        } 
        span input { text-align:center;}
        span label { 
            display: block; 
            position: relative; 
            top:3em;
            text-align: center;
        } 
    </style>