Home > Web Front-end > CSS Tutorial > How to Align Labels Next to Inputs Correctly in a Form?

How to Align Labels Next to Inputs Correctly in a Form?

Susan Sarandon
Release: 2024-11-05 03:32:02
Original
1035 people have browsed it

How to Align Labels Next to Inputs Correctly in a Form?

Align Labels Next to Inputs Correctly in a Form

A common scenario in form design is aligning labels correctly next to their input fields. To achieve this alignment, consider the following approach:

Determine a suitable fixed width for label elements and assign it using the width property. Set the display property to inline-block for labels. Align the text within labels to the right using text-align: right.

Here's an example code snippet:

<code class="css">label {
  display: inline-block;
  width: 140px;
  text-align: right;
}</code>
Copy after login
<code class="html"><div class="block">
  <label>Simple label</label>
  <input type="text">
</div>
<div class="block">
  <label>Label with more text</label>
  <input type="text">
</div>
<div class="block">
  <label>Short</label>
  <input type="text">
</div></code>
Copy after login

This approach uses fixed widths, which may not be ideal for responsive designs. Consider using flexbox or CSS grid for a more responsive solution.

The above is the detailed content of How to Align Labels Next to Inputs Correctly in a Form?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template