Put a <label> and <input type="text"> tag in the same row - html

I am trying to create a <form> as part of a project, which has text inputs with a label before them. I am trying to put the <label> and the <input> on the same line, aligned to the right side like this example project:
Here is the code I have attempted to use:
.labelrow {
text-align: right;
vertical-align: top;
}
.inputrow {
display: inline-block;
text-align: left;
vertical-align: top;
}
<form id="survey-form">
<div class="labelrow">
<label id="name" for="name">* Name:</label></div>
<div class="inputrow">
<input type="text" id="name" class="input-field" required placeholder="Enter your name"></div>
<div class="labelrow">
<label id="email" for="email">* Email:</label>
</div>
<div class="inputrow">
<input type="email" id="name" class="input-field" required placeholder="Enter your email">
</div>
</form>
This code gives me the result of this:
The <label> are aligned correctly, but the <input> are on the other line. What can I fix to get both on the same line and aligned to the right like the example?

Solution
House both label and input into a single div
Add display: flex to the parent so you can have more flexibility styling your fields on small screens. For example, you could move the label above the input on small screens when viewport space is limited using flex-direction: column
labels typically don't have ids. Instead, they point to form elements containing ids. I've fixed your labels in the following code
Duplicate ids are a no-no as well (also fixed)
.row {
display: flex;
align-items: center;
justify-content: flex-end;
}
.input-field {
margin-left: 1em;
padding: .5em;
margin-bottom: .5em;
}
<form id="survey-form">
<div class="row">
<label for="name">* Name:</label>
<input type="text" id="name" class="input-field" required placeholder="Enter your name">
</div>
<div class="row">
<label for="email">* Email:</label>
<input type="email" id="email" class="input-field" required placeholder="Enter your email">
</div>
</form>

By enclosing both elements into the same "div" you can align them together in a row.
<form id="survey-form">
<div class="inputrow">
<label id="name" for="name">* Name:</label>
<input type="text" id="name" class="input-field" required placeholder="Enter your name">
</div>
<div class="inputrow">
<label id="email" for="email">* Email:</label>
<input type="email" id="name" class="input-field" required placeholder="Enter your email">
</div>
</form>
By default, "div" tags always place a line break before and after they're inserted.

Potentially the simplest option is to put the input inside the label. Make the label a block item with text align right.
label {
display:block;
text-align:right;
margin: 5px;
}
<form id="survey-form">
<label id="name" for="name">* Name: <input type="text" id="name" class="input-field" required placeholder="Enter your name"></label>
<label id="email" for="email">* Email: <input type="email" id="name" class="input-field" required placeholder="Enter your email"></label>
</form>

Remove the <div>s and add a <br> after each <input>. Add the following to both <label> and <input>:
display: inline-block;
height: 1.2rem;
line-height: 1.2rem;
vertical-align: middle;
height and line-height can be adjusted but keep them equal to each other. Set <form> width to 100vw and of course text-align: right on <label>s. Place the <label>s and <input>s into a <fieldset> and assign the following to the <fieldset>
width: 50vw;
margin-left: 40vw;
border: 0px none transparent
BTW the <label>s have a duplicate #id which is invalid, therefore removed.
Demo
html,
body {
width: 100%;
height: 100%;
font: 400 16px/1.2 Raleway;
background: #FBFBFB;
}
form {
width: 70vw;
}
fieldset {
width: 50vw;
text-align: right;
margin-left: 20vw;
border: 0px none transparent;
background: none;
}
legend {
width: 70vw;
text-align:center;
margin: 0 auto;
}
label,
input,
button {
display: inline-block;
height: 1.2rem;
line-height: 1.2rem;
vertical-align: middle;
padding-left: 5px;
margin-top: 15px;
font: inherit;
}
input {
width: 60%;
max-width: 300px;
border-radius: 4px;
}
label {
width: 30%;
text-align: right;
}
button {
height: 1.5rem;
padding: 0 5px;
margin: 0 0 0 auto;
float: right;
cursor:pointer;
}
sup {
display:inline-block;
width: 25%;
margin-left: 70%;
margin-top: 10px;
text-align: right;
}
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
<form id="survey-form">
<fieldset>
<legend>Let us know how we can improve freeCodeCamp</legend>
<label for="name">* Name:</label>
<input id="name" type="text" placeholder="Enter your name" required><br>
<label for="email">* Email:</label>
<input id="email" type="email" placeholder="Enter your email" required><br>
<label for="age">* Age:</label>
<input id="age" type="number" placeholder="Enter your age" min='18' max='120' required><br>
<sup>* Required</sup>
<button type='submit'>Submit</button>
</fieldset>
</form>

When you wrap each in their own div you get the stacking that you are seeing. Put both the label and the input into a single div.

Related

Radio buttons wont align to the left

I am trying to create a modal and everything has come along perfectly except my radio buttons. No matter what I try I don't understand how to move them to the left like the other elements in my form. All I want to do is move the radio buttons to the left and align them with the other elements like normal radio buttons.
.feedback-background {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
position: absolute;
top: 0px;
display: flex;
align-items: center;
justify-content: center;
}
.feedback-content {
width: 500px;
height: 600px;
background-color: white;
border-radius: 4px;
padding: 20px;
}
input {
width: 50%;
display: block;
margin: 10px 0px;
}
input[type="radio"] {
display: inline;
}
<div class="feedback-background">
<div class="feedback-content">
<img src="E:\IIT\Lectures\WEB\coursework1\Images\feedbackimg1.jpg" alt="Givefeedback" style="width:100px;height:100px;">
<form action="">
Name:
<input type="text" placeholder="Name"> E-Mail:
<input type="text" placeholder="E-mail"> What do you think about us?<br>
<textarea rows="6" cols="33" name="comment "></textarea>
<br> How would you rate us ?
<br>
<label>
<input type ="radio " name="rating " id="rating " value="Excellent ">Excellent
<input type ="radio " name="rating " id="rating " value="Very Good ">Very Good
<input type ="radio " name="rating " id="rating " value="Average ">Average
<input type ="radio " name="rating " id="rating " value="Poor ">Poor
<input type ="radio " name="rating " id="rating " value="Extreamly Poor ">Extreamly Poor
</label>
<br>
<br>
SUBMIT
</form>
</div>
</div>
Here's the fiddle: https://jsfiddle.net/dcag0167/2/
Theres a lot of work an little bugs in your code. I will focus on the radio buttons. 2 main errors.
1- You are setting the radio buttons width to 50% with:
input {
width: 50%;
display: block;
margin: 10px 0px;
}
Solution comes to reset it in its own style
input[type="radio"]{
width: auto;
display: inline;
}
2- You are making all your text and radio buttons to just one label tag. This is not a good pratice. We can set on label for each radio button and text. An by adding its display to block, they will align.
label {
display: block;
}
Hope this helps :>
.feedback-background{
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.7);
position: absolute;
top: 0px;
display: flex;
align-items: center;
justify-content: center;
}
.feedback-content{
width: 500px;
height: 600px;
background-color: white;
border-radius: 4px;
padding: 20px;
}
input {
width: 50%;
display: block;
margin: 10px 0px;
}
label {
display: block;
}
input[type="radio"]{
width: auto;
display: inline;
}
<div class="feedback-background">
<div class="feedback-content">
<img src="E:\IIT\Lectures\WEB\coursework1\Images\feedbackimg1.jpg" alt="Givefeedback" style="width:100px;height:100px;">
<form action="">
Name:
<input type="text" placeholder="Name">
E-Mail:
<input type="text" placeholder="E-mail">
What do you think about us?<br>
<textarea rows="6" cols="33" "name="comment"></textarea>
<br>
How would you rate us?
<br>
<label><input type ="radio" name="rating" id="rating" value="Excellent">Excellent</label>
<label><input type ="radio" name="rating" id="rating" value="Very Good">Very Good</label>
<label><input type ="radio" name="rating" id="rating" value="Average">Average</label>
<label><input type ="radio" name="rating" id="rating" value="Poor">Poor</label>
<label><input type ="radio" name="rating" id="rating" value="Extreamly Poor">Extremely Poor</label>
<br>
<br>
SUBMIT
</form>
</div>
</div>
First, remove the entire input css styles and just keep one style.
Next, I changed your HTML to include a <label> for each radio button. That way, you can also click the <label> to have the radio button clicked. The for="" is the trick there.
Finally, Surround each group of ratings in a <div class="ratings"> so they can all belong to their own group with a shared style.
.feedback-background {
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
position: absolute;
top: 0px;
display: flex;
align-items: center;
justify-content: center;
}
.feedback-content {
width: 500px;
height: 600px;
background-color: white;
border-radius: 4px;
padding: 20px;
}
.ratings {
margin: 10px 0px;
}
<div class="feedback-background">
<div class="feedback-content">
<img src="E:\IIT\Lectures\WEB\coursework1\Images\feedbackimg1.jpg" alt="Givefeedback" style="width:100px;height:100px;" />
<form action="">
Name:
<input type="text" placeholder="Name"> E-Mail:
<input type="text" placeholder="E-mail"> What do you think about us?<br>
<textarea rows="6" cols="33" name="comment"></textarea>
<br> How would you rate us ?
<br>
<div class="ratings">
<input type="radio" name="rating" id="ExcellentRating" value="Excellent">
<label for="ExcellentRating">Excellent</label>
</div>
<div class="ratings">
<input type="radio" name="rating" id="VeryGoodRating" value="Very Good">
<label for="VerGoodRating">Very Good</label>
</div>
<div class="ratings">
<input type="radio" name="rating" id="AverageRating" value="Average">
<label for="AverageRating">Average</label>
</div>
<div class="ratings">
<input type="radio" name="rating" id="PoorRating" value="Poor">
<label for="PoorRating">Poor</label>
</div>
<div class="ratings">
<input type="radio" name="rating" id="ExtremelyPoorRating" value="Extremely Poor">
<label for="ExtremelyPoorRating">Extremely Poor</label>
</div>
<br>
<br>
SUBMIT
</form>
</div>
</div>

inline form elements with flex [duplicate]

This question already has answers here:
Flex / Grid layouts not working on button or fieldset elements
(3 answers)
Closed 5 years ago.
I am trying to create a form at the moment, that ideally with flex responds to the number of inputs with a group,
So I have a form setup like this:
<fieldset class="form__group">
<input type="text" class="form_input" />
<input type="text" class="form_input" />
</fieldset>
<fieldset class="form__group">
<input type="text" class="form_input" />
<input type="text" class="form_input" />
<input type="text" class="form_input" />
</fieldset>
<fieldset class="form__group">
<input type="text" class="form_input" />
</fieldset>
What I am trying to achieve is that the container does not care how many children it has but will allow them to fill the available space evenly in a single row.
So 2 items get 50% (minus a but for margins/padding), 3 items get 33.3% and 1 item 100% etc etc etc,
My CSS looks like this,
.form__group {
display: flex;
}
.form__input {
flex: 1 1 0;
background: #fff;
color: #939598;
border-radius: 30px;
box-shadow: none;
border: 1px solid #f1f2f2;
padding-left: 15px;
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
}
Which I thought would allow me to put children inline on the same row and allow flex to sort out widths and spacing?
Here is my WIP at the moment,
https://codepen.io/87Development/project/editor/AoNJzN/
So using flex how can create a row of inline form inputs that are equally spaced and widthed, without knowing how many elements may be in each form__group?
fieldset can present some issues with rendering...use a div instead.
Fieldset # MDN
* {
box-sizing: border-box;
}
.form__group {
display: flex;
}
.form_input { /* note the single underscore */
flex: 1;
background: lightgrey;
color: #939598;
border-radius: 30px;
box-shadow: none;
border: 1px solid #f1f2f2;
padding-left: 15px;
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
}
<div class="form__group">
<input type="text" class="form_input" />
<input type="text" class="form_input" />
</div>
<div class="form__group">
<input type="text" class="form_input" />
<input type="text" class="form_input" />
<input type="text" class="form_input" />
</div>
<div class="form__group">
<input type="text" class="form_input" />
</div>

How to allow a group of labels to grow together and to respectively shrink the input fields?

I want to have a group of labels that take as much horizontal space as the widest label and at the same time all input fields should shrink to accommodate the larger labels. I need this since I deal with translations and cannot be sure how wide a certain label will be.
Something like this:
form {
display: table;
width: 100%;
}
label {
width: 1%;
display: table-cell;
white-space: nowrap;
padding-right: 1em;
}
input {
display: inline-block;
width: 100%;
}
.input-group {
display: table-row;
}
* {
line-height: 2em;
}
body {
padding: 2em;
}
<form>
<div class="input-group">
<label for="name">Full name of your family and yourself</label>
<input type="text" id="name" name="name" placeholder="optional">
</div>
<div class="input-group">
<label for="email">Email</label>
<input type="text" id="email" name="email" placeholder="optional">
</div>
<div class="input-group">
<label for="phone">Phone</label>
<input type="text" id="phone" name="phone" placeholder="optional">
</div>
</form>
You can check this in Codepen too:
http://codepen.io/aboutandre/pen/bZRjqB
But is it there a more elegant way to do this that is more generic and doesn't involve using tables (be as HTML or CSS)?
How about specifying the width of the input fields to inherit from the desired/determining input field?
So for example if the name field is the determinant,make that have a width of 100% and and the other two elements -
width: inherit;

Input box aligned with text?

How can I get my boxes to align with my text?
I have also copy and pasted the html/css code in jsFiddle!
http://jsfiddle.net/EFByC/51/
<form
action="http://www.sblogger/cgi-bin/subcomments"
method="post" >
<fieldset name="commentFS" id="commentFS">
<label for="username">Username</label>
<input name="username" id="username" title="Supply your username" required="required"/>
<label for="email">E-mail</label>
<input name="email" id="email" type="email" title="Supply a valid e-mail address" required="required"/>
<label for="password">Password</label>
<input name="password" id="password" type="password" title="You must provide your password" required="required"/>
<label for="commentbox">Comment<br />
(500 character limit)</label>
<textarea maxlength="500" name="commentbox" id="commentbox"></textarea>
<input type="submit" value="Submit Comment"/>
</fieldset>
</form>
here you go, edited your Fiddle
It comes down to this:
If you float left & right, you need a wrapper to preserve the room for the floats.
so i added this:
p {
overflow: hidden;/*this should be clearfix, just for demo it is overflow fix*/
}
label{
display: block;
float: left;
font-size: 0.9em;
width: 20%;/* was 100%*/
margin-top: 5px;
margin-bottom: 5px;
/*clear: left*/
}
and the wrapper:
<p>
<label for="username">Username</label>
<input name="username" id="username" title="Supply your username" required="required">
</p>
i see you use float, display and width:100%; , you definitly have too much unnedeed rules here .
inline-block + width, can do it and allow you to vertiacal-align labels and inputs,
float+clear can work too, but vertical-align will not be avalaible :
example with inline-block:
/*Field set styles */
fieldset {
background-color: rgb(245,245,255);
margin: 15px auto;
padding: 5px;
width: 90%;
}
/* Label Styles */
label{
display: inline-block;
font-size: 0.9em;
margin-top: 5px;
margin-bottom: 5px;
width:35%;
}
/*Input control styles */
input, textarea {
font-size: 0.9em;
margin-left: 10px;
margin-right: 10px;
width: 55%;
vertical-align:middle;
}
/*Text area styles */
textarea {
height: 150px;
}
http://jsfiddle.net/EFByC/58/

Trouble aligning contact form

I have been trying to edit this easy form to just look good for 3 hours now and Im still not quite there. I want the input fields to be on the same row as the labels naturally, but somehow the inputs are a bit lower than the labels and I cant seem to edit them with margins. What am I doing wrong?
Heres a snaggy picture of what the form looks like:
// CONTACT FORM
<label for="name"><p>Name:</p></label>
<input type="text" name="name" id="name" tabindex="1" />
<br/>
<label for="email"><p>Email:</p></label>
<input type="text" name="email" id="email" tabindex="2" />
<br/>
<label for="subject"><p>Subject:</p></label>
<input type="text" name="subject" id="subject" tabindex="3" />
<br/>
<label for="comments"><p>Comments:</p></label>
<textarea name="comments" id="comments" cols="45" rows="5" tabindex="4"></textarea>
<br/>
<label for="submit"></label>
<input type="submit" name="submit" id="submit" value="Submit" tabindex="5" />
<label for="reset"></label>
<input type="reset" name="reset" id="reset" value="Clear" tabindex="6" />
// CSS
label {
float: none;
font-size: 100%;
width: 250px; /* just this width evens out input box placement */
font-weight: bold;
}
input { /*I think these just fall in because they are naturally following the labels!*/
width: 250px;
padding:5px;
margin-top: -10px;
}
textarea {
width: 250px;
height: 150px;
resize:none;
}
guestbook {
margin-top:50px;
text-align:center;
font-size:26px;
color:#05924b;
font-family:Gisha;
}
gb p {
color:#05924b;
font-family:Gisha;
text-align:left;
margin-left:85px;
margin-top:0px;
margin-bottom:0px;
}
// SOLUTION: Removed the "p" paragraph from within the form and adjusted the rest from CSS, added float:left to the different inputs fields and rows, lowered the label width so the input came closer, then calculated and put the correct margin-right to both input{} and textarea{} and last to #submit to get everything in nice order.
Heres a screenshot from the new code: http://snag.gy/PwpbQ.jpg
// CSS
/* Input */
label {
float: left;
font-size: 100%;
width: 50px; /* just this width evens out input box placement */
font-weight: bold;
margin: 2px 0;
padding:5px;
font-family: Gisha;
font-style: normal;
font-variant: normal;
text-decoration: none;
text-align: left;
}
input { /*I think these just fall in because they are naturally following the labels!*/
width: 300px;
padding:5px;
margin: 5px 0;
font-size:24px;
margin-right:192px;
}
textarea {
width: 300px;
height: 150px;
resize:none;
margin:5px 0;
padding:5px;
margin-right:192px;
}
#submit {
margin-right:225px;
}
/* End of input */
Remove all of the <p> tags from the labels. A <p> tag is a block level element, therefore it should not be nested within the inline element <label>. Block level elements also clear, meaning they do not allow content on either side (unless floated). I believe this is causing your issue.
<label for="name">Name:</label>
<input type="text" name="name" id="name" tabindex="1" />
<br/>
<label for="email">Email:</label>
<input type="text" name="email" id="email" tabindex="2" />
<br/>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" tabindex="3" />
<br/>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" cols="45" rows="5" tabindex="4"></textarea>
<br/>
<label for="submit"></label>
<input type="submit" name="submit" id="submit" value="Submit" tabindex="5" />
<label for="reset"></label>
<input type="reset" name="reset" id="reset" value="Clear" tabindex="6" />
Once the markup has been adjusted the label and input tags have no vertical spacing. To add vertical spacing you can add a margin to both elements.
label {
font-size: 100%;
width: 250px;
font-weight: bold;
margin: 2px 0;
}
input {
width: 250px;
padding:5px;
margin: 2px 0;
}
Example: http://jsfiddle.net/KZrXD/