I'm new at this so please bear with me. I'm trying to align the text and their input fields - just like this example - by using the label class in the CSS file (please check below) but for some reason it is not working and I can't figure out what I'm doing wrong. Could someone please help me out?
Here's my code:
/* Set main division background color, color, width, padding, border-radius, box-alignment */
.mainD {
background-color: white;
color: black;
width: 500px;
padding: 20px;
border-radius: 25px;
box-align: center;
}
/* Set fieldset in the center with no border */
fieldset {
border: none;
text-align: center;
}
/* Aligns texts and their fields */
label {
display: inline-block;
text-align: right;
}
<div class="mainD">
<h1>Interest Calculator</h1>
<fieldset>
<!---Input box for the amount--->
<label for="principal">Amount
<input type="number" id="principal"></label>
<br><br>
<!---Slider for the interest rate--->
<label for="rate" onchange="updateRate()">Interest Rate
<input type="range" id="rate" min="1" max="10" step="0.25" value="5.25">
<span id="rate_val">5.25</span>%</label>
<br><br>
<!---Dropdown box for the years--->
<label> No. of Years
<select id="years">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option></select></label> (additional code..)
</fieldset>
</div>
You can make use of flexbox to align the items vertically and horizontally. Then use justify-content to space them evenly as well to achieve the effect you want in the picture. See the snippet below:
/* Set main division background color, color, width, padding, border-radius, box-alignment */
.mainD {
background-color: white;
color: black;
width: 500px;
padding: 20px;
border-radius: 25px;
box-align: center;
}
/* Set fieldset in the center with no border */
fieldset {
border: none;
text-align: center;
}
/* Aligns texts and their fields */
label {
display: flex;
align-items: center;
justify-content: space-between;
}
.input {
display: flex;
width: 300px;
justify-content: flex-start;
align-items: center;
}
<div class="mainD">
<h1>Interest Calculator</h1>
<fieldset>
<!---Input box for the amount--->
<label for="principal">Amount
<div class="input"><input type="number" id="principal"></div>
</label>
<br><br>
<!---Slider for the interest rate--->
<label for="rate" onchange="updateRate()">
Interest Rate
<div class="input">
<input type="range" id="rate" min="1" max="10" step="0.25" value="5.25">
<span id="rate_val">5.25</span>%
</div>
</label>
<br><br>
<!---Dropdown box for the years--->
<label> No. of Years
<div class="input">
<select id="years">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</label>
</fieldset>
</div>
i center everything just to prove it actually center i added border feel free to remove it after
/* Set main division background color, color, width, padding, border-radius, box-alignment */
.mainD {
background-color: white;
color: black;
width: 500px;
padding: 20px;
border-radius: 25px;
box-align: center;
}
/* Set fieldset in the center with no border */
fieldset, h1 {
border: 2px solid #ff8197;
text-align: left;
margin: auto auto auto auto;
width: 60%;
}
/* Aligns texts and their fields */
label {
display: inline-block;
text-align: right;
}
<div class="mainD">
<h1>Interest Calculator</h1>
<fieldset>
<!---Input box for the amount--->
<label for="principal">Amount
<input type="number" id="principal"></label>
<br><br>
<!---Slider for the interest rate--->
<label for="rate" onchange="updateRate()">Interest Rate
<input type="range" id="rate" min="1" max="10" step="0.25" value="5.25">
<span id="rate_val">5.25</span>%</label>
<br><br>
<!---Dropdown box for the years--->
<label> No. of Years
<select id="years">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option></select></label> (additional code..)
</fieldset>
</div>
Related
For the code below, I have two problems:
The background image cereal.jpeg was working, now for some reason, it's disappeared and I can't figure why. Because it used to work, I know the file name and location are correct.
My controls refuse to remain sticky!
div.container {
border-style: solid;
border-width: thin;
border-color: grey;
box-shadow: 10px 15px 8px black;
height: 1500px;
width: 700px;
}
div.container::before {
background-image: url("cereal.jpeg");
background-repeat: repeat;
opacity: 0.05;
}
div.controls {
float: left;
width: 140px;
border: 1px solid lightgrey;
position: -webkit-sticky; // Safari
position: sticky;
top: 0;
}
div.thePlots {
float: left;
border: 1px solid lightgrey;
width: 400px;
height: 1400px;
}
<div id=container class=container>
<div class=controls id=controls>
<label id="brand">Brand:</label>
<br>
<select size=7 id="brandOptions" multiple>
<option value=0 selected>All</option>
<option value=1>Kellog</option>
<option value=2>Post</option>
<option value=3>Quaker</option>
<option value=4>General Mills</option>
<option value=5>Tree House</option>
<option value=6>CPW</option>
</select>
<br><br><br>
<label id="barsLabel">Error Bars:</label>
<input id=barsPresent type=checkbox value="Error Bars" checked>
<br><br><br>
<label id="label">Elasticity<br>Means:</label>
<br>
<select id="plotY">
<option value="efm" selected="selected">efm</option>
<option value="enm">enm</option>
</select>
<br><br><br>
</div>
<div class=thePlots id=thePlots>
The Plots<br>
<div class="tooltip" id="tooltip"></div>
<div id="plotshare" class="plotshare"></div>
<div id="plotprice" class="plotprice"></div>
<div id="plotsugar" class="plotsugar"></div>
<div id="plotmushy" class="plotmushy"></div>
</div>
</div>
opacity is too low.
Remove float:left;
Your div.controls element is sticking just fine, but it's sticking to the top of your container element. If you put it outside out your container element you will see it stick to the top of the page as desired.
Also, you will need to remove the // safari from your css. The correct syntax for commenting in css is /* comment */
For the background issue, there is no content for your background to display behind. You will need to add something to the before in order for the background to show.
Check this working fiddle which solves both problems
: https://jsfiddle.net/c5vrhf7t/1/
I wish to move my radiobuttons under each of the selections. Radiobutton "test1" under the first selection and radiobutton "test2" under the second selection. That is, the radiobuttons are below the selection, but next to each other.
How do I do that? I do not wish to use tables or br. I would like to use CSS; how should I style my CSS?
<form name="test">
<label style="float:left;">test</label>
<input style="display:block" />
<label style="float:left;">test</label>
<input style="display:block" />
<label style="float:left;">test</label>
<select>
<option>1</option>
<option>2</option>
</select>
<select>
<option>a</option>
<option>b</option>
</select>
<input type="radio" name="test" value="test1">
<input type="radio" name="test" value="test2">
</form>
If I add more than one radio box:
I tried using + (adjacent sibling selector) in CSS, but I think it is because the position is absolute. So the next "children" won't align horizontally. I think this is the easiest/"neat" way of doing it.
So I tried to position it relatively (the first radio box) and float:left. But it positions itself too high.
Here is each radio button underneath each select element using only CSS without changing your HTML.
form {
display: block;
position: relative;
padding-bottom: 30px;
}
input[type="radio"] {
display: inline-block;
position: absolute;
bottom: 0;
left: 30px;
}
input[type="radio"]:last-child {
left: 70px;
}
JSFiddle Demo: https://jsfiddle.net/3w16q179/1/
More than 2 radio buttons:
JSFiddle Demo: https://jsfiddle.net/3w16q179/6/
This will align the radio buttons below the select inputs, regardless of width of their widths.
input[type="radio"] {
display: block;
}
<form name="test">
<label style="float:left;">test</label>
<input style="display:block" />
<label style="float:left;">test</label>
<input style="display:block" />
<label style="float:left;">test</label>
<div style="float:left;">
<select>
<option>1</option>
<option>2</option>
</select>
<input type="radio">
</div>
<div style="float:left;">
<select>
<option>2</option>
<option>3</option>
</select>
<input type="radio">
</div>
</form>
AHA! I just figured it out what you're looking for... Each radio button is centered under the select. I got it.
<!doctype html>
<html>
<style type = "text/css">
form * {
box-sizing: border-box;
}
form {
background: White;
width: 420px;
padding: 8px;
border: 1px solid grey;
}
fieldset {
text-align: center;
border: 0;
padding: 0;
}
label {
width: 33.33%;
float: right;
padding: 6px;
}
legend {
display: block;
float: left;
width: 33.33%;
margin: 0;
}
</style>
<form name="test">
<fieldset>
<label>
<select>
<option>a</option>
<option>b</option>
</select>
</label>
<label>
<select>
<option>a longer ooption</option>
<option>b</option>
</select>
</label>
</fieldset>
<fieldset>
<label>
<input type="radio" name="radio">
</label>
<label>
<input type="radio" name="radio">
</label>
<legend>
Choose One:
</legend>
</fieldset>
</form>
</html>
jsFiddle: https://jsfiddle.net/3jbqjj0f/
You can use display: block or display: inherit;.
input[type="radio"] {
display: block;
}
JSFiddle
Though your question is not that clear, I think you can use class. Just add any class to the input you want to move.
For example:
HTML
<input type="radio" class="myinput" />
CSS
input.myinput {
display:block;
margin-top:20px; /* For top spacing */
}
I hope that helps!
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>
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;
}
I have a basic css question. I'm trying to add css to my form, the code is below:
<div id="content">
<form id="form1" method="post" action="">
<p>
<label for="name" class="title">Your name:</label>
<input name="name" type="text" class="widebox" id="name">
</p>
<p>
Colour: <select name="colour_pref" size "1">
<option value="1">Pink</option>
<option value="2">Blue</option>
<option value="3">White</option></select>
</p>
<p class="submit">
<input type="submit" name="add" value="add_colour" id="add">
</p>
</form>
</div>
and this is the css:
#content p {
border-bottom: 1px solid #efefef;
margin: 10px;
padding-bottom: 10px;
width: 260px;}
.title {
float: left;
width: 100px;
text-align: right;
padding-right: 10px;}
.submit {
text-align: right;}
The problem is the select element is not aligned with the name field, I tried to add class="title" to it but it made it even messier.
Would really appreciate if you could help me align this select element VERTICALLY with the text field. thanks
Something like this DEMO http://jsfiddle.net/kevinPHPkevin/XzHG2/1/
.submit input {
margin-left:110px;
}
Just add display inline to your pararaph:
p {
display: inline;
}
Demo