Aligning Label and Input Elements Horizontally
Enhancing the aesthetics of web forms requires an ability to position labels and input fields alongside each other. This guide illustrates how to achieve this horizontal alignment using CSS.
The Issue:
As highlighted in the initial post, there are two main challenges:
Solution 1: Inline-Block
Using display: inline-block for both label and input elements allows them to align horizontally while maintaining their individual sizes. However, this may not fulfill the requirement of filling the remaining width for the input field.
Improved Solution: float/overflow Magic
**CSS:
label { float: left } span { display: block; overflow: hidden; padding: 0 4px 0 6px } input { width: 100% }
Solution 2: table-cell
Another effective approach is to treat both elements as table cells.
**CSS:
.container { display: table; width: 100% } label { display: table-cell; width: 1px; white-space: nowrap } span { display: table-cell; padding: 0 0 0 5px } input { width: 100% }
The above is the detailed content of How to Align Labels and Input Fields Horizontally in Web Forms?. For more information, please follow other related articles on the PHP Chinese website!