Best way to arrange labels and inputs side-by-side
align-items: stretch
Flexbox has a feature commonly known as "equal height columns". This feature enables flex items in the same container to all be equal height.
This feature comes from two initial settings:
flex-direction: row
align-items: stretch
With a slight modification, this feature can become "equal width rows".
flex-direction: column
align-items: stretch
Now a column of flex items will have the width of the longest item.
Reference:
- Equal Height Columns with Flexbox
align-content: stretch
An initial setting on a flex container is align-content: stretch
. This setting will distribute rows or columns (depending on flex-direction
) across the length of the container.
In this case, in order to pack two columns to the start of the container, we need to override the default with align-content: flex-start
.
References:
- Remove space (gaps) between multiple lines of flex items when they wrap
- How does flex-wrap work with align-self, align-items and align-content?
flex-direction: column
, flex-wrap: wrap
and height
Since you have a preferred HTML structure (with all form elements logically ordered in one container), flex items will need to wrap in order to form a column of labels and a column of inputs.
So we need to override flex-wrap: nowrap
(the default) with wrap
.
Also, the container must have a fixed height so that items know where to wrap.
References:
- Is it possible for flex items to align tightly to the items above them?
- Make a div span two rows in a grid
The order
property
The order
property is needed to properly align labels and inputs across columns.
Reference:
- Is there a “previous sibling” CSS selector?
.aligned_form { display: flex; flex-flow: column wrap; align-content: flex-start; height: 75px;}
label[for='a'] { order: -3; }label[for='b'] { order: -2; }label[for='c'] { order: -1; }
label, input { height: 25px; padding: 5px; box-sizing: border-box;}
<!-- No changes to the HTML. -->
<div class='aligned_form'>
<label for='a'>short_label</label> <input type='text' id='a' placeholder="short_label">
<label for='b'>medium_label</label> <input type='text' id='b' placeholder="medium_label">
<label for='c'>very_extra_long_label</label> <input type='text' id='c' placeholder="very_extra_long_label">
</div>
Align labels in form next to input
WARNING: OUTDATED ANSWER
Nowadays you should definitely avoid using fixed widths. You could use flexbox or CSS grid to come up with a responsive solution. See the other answers.
One possible solution:
- Give the labels
display: inline-block
; - Give them a fixed width
- Align text to the right
That is:
label {
display: inline-block;
width: 140px;
text-align: right;
}
<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>
How to make label and input appear on the same line on an HTML form?
Assuming you want to float the elements, you would also have to float the label
elements too.
Something like this would work:
label {
/* Other styling... */
text-align: right;
clear: both;
float:left;
margin-right:15px;
}
#form { background-color: #FFF; height: 600px; width: 600px; margin-right: auto; margin-left: auto; margin-top: 0px; border-top-left-radius: 10px; border-top-right-radius: 10px; padding: 0px; text-align:center;}label { font-family: Georgia, "Times New Roman", Times, serif; font-size: 18px; color: #333; height: 20px; width: 200px; margin-top: 10px; margin-left: 10px; text-align: right; clear: both; float:left; margin-right:15px;}input { height: 20px; width: 300px; border: 1px solid #000; margin-top: 10px; float: left;}input[type=button] { float:none;}
<div id="form"> <form action="" method="post" name="registration" class="register"> <fieldset> <label for="Student">Name:</label> <input name="Student" id="Student" /> <label for="Matric_no">Matric number:</label> <input name="Matric_no" id="Matric_no" /> <label for="Email">Email:</label> <input name="Email" id="Email" /> <label for="Username">Username:</label> <input name="Username" id="Username" /> <label for="Password">Password:</label> <input name="Password" id="Password" type="password" /> <input name="regbutton" type="button" class="button" value="Register" /> </fieldset> </form></div>
Labels not aligned on top left side above text inputs
Make Parent div width to same as your <input>
width so that you can use margin:auto
to center a div. Finally you can use text-align: left
as shown here
.account-form{
text-align: left;
margin: auto;
width: 550px;
}
Note: By default it takes text-align: left
. I just mentioned because you used text-align: center
How can I get two form fields side-by-side, with each field’s label above the field, in CSS?
<form>
<label for="company">
<span>Company Name</span>
<input type="text" id="company" />
</label>
<label for="contact">
<span>Contact Name</span>
<input type="text" id="contact" />
</label>
</form>
label { width: 200px; float: left; margin: 0 20px 0 0; }
span { display: block; margin: 0 0 3px; font-size: 1.2em; font-weight: bold; }
input { width: 200px; border: 1px solid #000; padding: 5px; }
Illustrated at http://jsfiddle.net/H3y8j/
How to align image and form side by side and it should also be perfectly aligned even in different views like mobile view and desktop
You have your primary box
container set to position: absolute
and several positioning attributes that are not present on the image which is probably why everything is shifting. I don't know what else this form and image are going to be put into but they don't seem necessary in your example so I've removed them and added a flexbox .flexContainer
to wrap your content in and that helps keep your content aligned in a single row. You could take it a step further and add a @media
query (https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries) to control how the image reacts on mobile (for example, change the flex-direction
to column
so the image appears below the contact box instead of stretching inward on smaller screens.
`
.flexContainer {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
img {
width: 100%;
height: auto;
height: 475px;
cursor: pointer;
}
span {
content: "\27F6";
}
label {
padding-left: 10px;
font-size: 16px;
padding-top: 10px;
}
.box {
height: 475px;
background: green;
padding: 40px;
box-sizing: border-box;
margin-left: 1%;
cursor: pointer;
}
.box p {
font-family: 'jmsans', sans-serif;
padding: 0;
margin: 0 0 40px;
color: white;
font-size: 40px;
width: 467px;
}
.box input {
margin-bottom: 30px;
width: 466px;
box-sizing: border-box;
box-shadow: none;
border: none;
border-bottom: 2px solid #999;
outline: none;
padding-left: 10px;
font-weight: bold;
height: 56px;
}
.box input[type="submit"] {
border-bottom: none;
cursor: pointer;
color: #fff;
margin-bottom: 0;
}
.box form div {
position: relative;
}
.box form div label {
position: absolute;
top: 10px;
left: 0;
color: #999;
transition: 0.5s;
pointer-events: none;
}
<div class="flexContainer">
<div class="box">
<p>contact us</p>
<form>
<div>
<input type="text" name="" required="">
<label>firstname</label>
</div>
<div>
<input type="text" name="" required="">
<label>last name</label>
</div>
<div>
<input type="text" name="" required="">
<label>mail</label>
</div>
<div class="button">
<button type="button">submit <span>⟶</span></button>
</div>
</form>
</div>
<div>
<img src="https://www.fnordware.com/superpng/pnggrad8rgb.png">
</div>
</div>
Related Topics
Provide an Image for Whatsapp Link Sharing
How to Playback Mkv Video in Web Browser
Angular (Typescript): How to Set Option of Selected to 'Selected' If Two-Way Binded
Angular - Show Empty Row by Default in Drop Down List and Disable Button
How to Remove Button Style or Overwrite Dynamically Added Classes to Button in Material2
How to Remove Unwanted Space in This Table
How to Divide Div Color With 30% Different Color and 70% Different Color in Vertical Using CSS
How to Center an Element Horizontally and Vertically
Is It Valid to Replace Http:// With // in a ≪Script Src="Http://..."≫
How to Remove Blue Highlight on Hover in Select, Option Inputs HTML for Chrome
On Hover of Child, Change Background Color of Parent Container (Css Only)
How to Open Excel File in Browser, Not in Excel Application
How to Disable Hover Effect for Active Link
Add Line Break Within Tooltips
Align Left and Right in Mat-Card-Title
Moving an Image Across a HTML Canvas
What to Do Regular Expression Pattern Doesn't Match Anywhere in String