Here is what my work is so far:
<div id="main">
<form>
<label>First Name:<input type="text" id="firstname"></label><br/>
<label>Last Name:<input type="text" id="lastname"></label><br>
<label>E-Mail:<input type="text" id="email"></label><br/>
<label>Phone:<input type="text" id="phone"></label><br/>
</form>
</div>
CSS
#main {
width:300px;
}
#main input {
float:right;
display:inline;
}
#main label {
color: #2D2D2D;
font-size: 15px;
width:250px;
display: block;
}
Currently, the label (on the left) is kind of towards to top of the input field (on the right). I want to vertically align them so the label since in the middle of the input field.
I've tried vertical-align and it does not work. Please help me try to figure out the problem. Thanks.
html:
I add span in your label so we can add style specific for the text label:
css:
demo
I feel nesting
<span>
adds a lot of unnecessary markup.display: inline-block
lets the<label>
and<input>
sit next to each other just like withfloat: right
but without breaking document flow. Plus it's much more flexible and allows more control over alignment if you (or the user's screen reader) want to change thefont-size
.Edit: jsfiddle
You can enclose the
<label>
elements in a span and set the span'svertical-align
tomiddle
HTML
CSS
http://jsfiddle.net/2RCBQ/2/
I think that the following is the only method that works for all input types.
I put
because it seems to be the simplest way to align different input types; however, margins work just fine.You can use flexbox css to vertical align.
Just wrap the parent element
display-flex
.