Css: How to align vertically a "label" and "input" inside a "div"?
Use flexbox
for vertical alignment. Set the div
container to display: flex
and align-items: center
:
Both label
and input
will be centered regardless of their height.
The Flexbox approach for advanced vertical alignment
When you've got the basic flexbox alignment nailed down, and you're ready to dive deep into controlling your layout, try these professional techniques.
Fine-tuning with Flexbox
Flexbox
lets you control not just the group as a whole, but each individual element's vertical alignment:
You can combine this technique with justify-content
for granular control over horizontal space as well:
The legendary table-cell method
display: table-cell
might not be the trendy kid on the block, but it still has its uses:
However, flexbox is generally the life of the party when it comes to responsive designs.
Getting your heights fair and even
Alignment doesn't just happen. Ensure a consistent div height:
Adding style to make a statement
Use a simple background color and adjust the position property to make your layout intuitive:
Quick and easy inline-block alignment
As a quick-fix, setting line-height equal to the div's height can do the trick:
The responsive power of Flexbox
Flexbox is your best friend for responsive designs:
Wrapped up for free structure
Wrapping your input
with the label
can give your structure a little boost:
The perfect margin for the perfect fit
Manage the margin
on inputs for some fine vertical alignment:
Have you checked it on IE?
Don't forget to test your design in various browsers to ensure it works everywhere.
Was this article helpful?