I'm creating a form and I have several input boxes on the same line.
I'd like to have the email input to take one line. The date, time, and number inputs to take another line. However, I'm not sure how to get the date/time/number inputs to span exactly 100% of the width of the form.
The percentages I have now in the CSS are estimates, so the edge of the number box doesn't vertically align with the email input box.
input[type=email] {
width: 100%;
}
input[type=date] {
width: 22%;
margin-right: 15px;
}
input[type=time] {
margin-right: 15px;
}
input[type=number] {
width: 11.4%
}
<form>
Email: <input type="email"><br>
Date: <input type="date">
Time: <input type="time">
Number in Party: <input type="number">
</form>
I would do it using flex, Here's a working example
I wrapped each line on a div, like this:
<form>
<div>Email: <input type="email"></div>
<div>
<div>Date: <input type="date"></div>
<div>Time: <input type="time"></div>
<div>Number in Party: <input type="number"></div>
</div>
</form>
css
form{
display: flex;
flex-direction: column;
}
form>div{
display: flex;
}
form>div input{
flex-grow: 1;
}
form>div>div{
display: flex;
flex-grow: 1;
}
You can use <label> to give yourself some explicit control the width, padding & margin of the form field labels.
Example:
label, input {
display: inline-block;
height: 24px;
line-height: 24px;
}
label {
width: 18%;
margin: 6px 0;
padding: 3px 0 3px 3px;
background-color: rgb(227,227,227);
}
input {
width: 80%;
}
input:nth-of-type(n+2) {
width: 13%;
}
<form>
<label for="email">Email:</label><input type="email" name="email" id="email"><br />
<label for="date">Date:</label><input type="date" name="date" id="date">
<label for="time">Time:</label><input type="time" name="time" id="time">
<label for="number">Number in Party:</label><input type="number" name="number" id="number">
</form>
input[type=email] {
width: 100%;
}
input[type=date] {
width: 21%;
}
input[type=time] {
width: 21%;
margin-right:42px;
}
input[type=number] {
width: 20.9%;
}
<form>
Email: <input type="email"><br><br>
Date : <input type="date">
Time : <input type="time">
Number in Party : <input type="number">
</form>
Related
How to add small letter (sub) under a word in html and css?
I am attaching the screenshot.
Want to write "Max 100, Min 1 just like the picture below.
my code is here:
<input type="text" class="textarea" alt="" value="" style="color: #1c91df; height: 50px; width: 100%; margin-top: 0px; margin-bottom: 0px; border-color: #1c91df; box-shadow: none; border-width: 1px;">
<input type="number" class="textarea" alt="height" value="10" style="color:#30a2ee;"/> Height <sub>Max 100</sub> <sub>Min 1</sub>
<input type="number" class="textarea1" alt="depth" value="10" style="color:#30a2ee;"/> Depth <sub>Max 100</sub> <sub>Min 1</sub>
I hope this is what you're looking for. : )
I am attaching a screenshot of it.
Here's the code.
<div class="demention">
<input type="number" class="textarea" alt="height" value="10" style="color:#30a2ee;"/>
<p class="big_text">Height</p>
<input type="number" class="textarea" alt="depth" value="100" style="color:#30a2ee;"/>
<p class="big_text">Depth</p>
</div>
<style>
.demention {
display: flex;
flex-direction: row;
align-content: center;
min-height: 50px;
}
.demention .textarea {
margin-right: 10px;
padding: 10px;
width: 100px;
border: 1px solid #30a2ee;
}
.demention .big_text {
position: relative;
padding-right: 30px;
color: #30a2ee;
}
p.big_text:before {
content: "Max. 100";
position: absolute;
left: 0;
top: 25px;
opacity: 0.5;
}
p.big_text:after {
content: "Min. 1";
position: absolute;
left: 0;
top: 40px;
opacity: 0.5;
}
</style>
sub {
text-transform: lowercase;
}
There are 5 different values you can use:
**lowercase**: makes all of the letters in the selected text lowercase
**uppercase**: makes all of the letters in the selected text uppercase or ALL CAPS
**capitalize**: capitalizes the first letter of each word in the selected text
none: leaves the text's case and capitalization exactly as it was entered
**inherit**: gives the text the case and capitalization of its parent
**full-width**: This is a keyword forcing the writing of a character (mainly ideograms and Latin scripts) inside a square.
There are million of ways to do that. For example you can use flex:
<div class="mygroup">
<input type="number" class="textarea" alt="height" value="10" style="color:#30a2ee;"/>
<div class="mystyle">
<span>Height</span>
<sub>Max 100</sub>
<sub>Min 1</sub>
</div>
<input type="number" class="textarea1" alt="width" value="10" style="color:#30a2ee;"/>
<div class="mystyle">
<span>Width</span>
<sub>Max 100</sub>
<sub>Min 1</sub>
</div>
</div>
And this css:
.mygroup {
display: flex;
}
.mystyle {
display: flex;
flex-direction: column;
}
Please add this custom css inside your css file and check again.
sub {
text-transform: lowercase;
}
I currently have the two following forms.
form {
text-align: center;
width: 100%;
}
<form>
<input type="text" name="email">
<input type="submit" value="Submit">
</form>
And
input[type=text] {
width: 80%;
padding: 1vh;
}
<form>
<p>First Name:</p>
<input type="text" name="firstname"><br>
</form>
How can I have different rules for each form? The first one needs the contents centre aligned and the second needs the contents aligned left/padded left.
If I change the css for either the alignment and size change.
Thanks in advance.
I've updated the form, how can I have it so that the text input is only styled for the left form? I don't want any text input rules on the center one.
.form-container {
background-color: #ff8e08;
width: 90%;
margin: auto;
}
.center-form {
text-align: center;
width: 100%;
}
.left-form {
text-align: left;
margin-left: 2vh;
}
input[type=text] {
width: 80%;
}
input[type="text"] {
padding: 1vh;
}
<div class="applicationform">
<br>
<form class="left-form">
<p>First Name:</p>
<input type="text" name="firstname">
</form>
</div>
</div>
<br>
<form class= "center-form">
<input type="text" name="email">
<input type="submit" value="Submit">
</form>
Add different class names for them.
CSS:
.center-form {
/* center-form specific rules here */
}
.left-form {
/* left-form specific rules here */
}
HTML:
<form class="center-form">
<input type="text" name="email">
<input type="submit" value="Submit">
</form>
<form class="left-form">
<p>First Name:</p>
<input type="text" name="firstname"><br>
</form>
And if you want to share some of the CSS rules that they both need:
.form {
/* shared CSS rules */
}
Then, add this form class name to both of them in the HTML like this:
<form class="form center-form">
<input type="text" name="email">
<input type="submit" value="Submit">
</form>
<form class="form left-form">
<p>First Name:</p>
<input type="text" name="firstname"><br>
</form>
Remeber, override class names like center-form and left-form should come after this formutility class name.
For your particular case, change your CSS to:
body {
margin: 0; /* Remove user agent default styles */
}
.center-form {
text-align: center;
width: 100%;
}
.left-form {
text-align: left;
margin-left: 2vh;
}
input[type=text] {
width: 80%;
}
input[type="text"] {
padding: 10px;
}
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;
I have a simple HTML form with some validations but the text labels get misplaced when the form appears.
This is the form before (PLEASE NOTE: The red square around it is not part of the form, I just placed it with Paint to help you see the problem quick):
And after:
Any hint to keep the label aligned with the text field please?
UPDATE:
This is the code:
<form name="senstageaddform">
<div class="form-element">
<label for="ed_senStartDate" class="text-label">
{{i18n "EDUCATION_SEN_START_DATE"}} <span class="required">*</span>
</label>
<input type="text" class="date standard-text-field" maxlength="16" id="ed_senStartDate" name="startDate"/>
</div>
<div class="form-element">
<label for="ed_senEndDate" class="text-label">{{i18n "EDUCATION_SEN_END_DATE"}}</label>
<input type="text" class="date standard-text-field" maxlength="16" id="ed_senEndDate" name="endDate"/>
</div>
</form>
Here is the CSS:
.form-element {
display: inline-block; margin: 5px; width: 98%; clear: both; vertical-align:top
.text-label {
width: 20%; text-align: right; display: inline-block; padding: 3px 5px 0px 1px;
}
.standard-text-field {
width: 10em;
}
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/