I have input text's and label tags. I can't figure out the CSS to get the label text to align right below the input text. Here's a snippet of the HTML:
<form id="sg1">
<label for="member1">member 1</label>
<input name="member1" id="member1" value="jack" />
<label for="member2">member 2</label>
<input name="member2" id="member2" value="carter" />
<label for="member3">member 3</label>
<input name="member3" id="member3" value="jackson" />
<label for="member4">member 4</label>
<input name="member4" id="member4" value="tielk" />
</form>
Trying to get:
[input box 1] [input box 2]
label 1 label 2
etc, with all elements.
A quickly whipped up example that works:
input {
display: inline-block;
width: 6em;
position: relative;
top: -3em;
}
label {
display: inline-block;
width: 6em;
margin-right: .5em;
padding-top: 1.5em;
}
<form id="sg1">
<label>member 1 <input name="member1" id="member1" value="jack" /></label>
<label>member 2 <input name="member2" id="member2" value="carter" /></label>
<label>member 3 <input name="member3" id="member3" value="jackson" /></label>
<label>member 4 <input name="member4" id="member4" value="tielk" /></label>
</form>
Could be improved, but I find it cleaner than extraneous divs, and it degrades much nicer than the label-after-input-approach when CSS support is absent. Personally, I prefer to nest the inputs in the labels anyway.
Use a table (one input/label pair per cell) or left-floating divs (one input/label pair per div). Example:
<div class="pair">
<input type="text" name="foo" value="bar" /><br />
<label for="foo">shabba</label>
</div>
<div class="pair">
…
</div>
CSS:
div.pair {
float:left;
}
You'd make the job a lot easier by wrapping each field (in this case, each input/label pair) in a div.
You can use pure css to get this to achieve what you want, but it requires a lot of adhoc positioning stuff that you're better off not doing.
The simplest way is to put the label beneath the input on the html:
<form id="sg1">
<input name="member1" id="member1" value="jack" />
<label for="member1">member 1</label>
<input name="member2" id="member2" value="carter" />
<label for="member2">member 2</label>
<input name="member3" id="member3" value="jackson" />
<label for="member3">member 3</label>
<input name="member4" id="member4" value="tielk" />
<label for="member4">member 4</label>
</form>
Then you can wrap each input/label pair with a div, and set the div like so:
<form id="sg1">
<div class="wrap">
<input name="member1" id="member1" value="jack" />
<label for="member1">member 1</label>
</div>
<div class="wrap">
<input name="member2" id="member2" value="carter" />
<label for="member2">member 2</label>
</div>
<div class="wrap">
<input name="member3" id="member3" value="jackson" />
<label for="member3">member 3</label>
</div>
<div class="wrap">
<input name="member4" id="member4" value="tielk" />
<label for="member4">member 4</label>
</div>
</form>
#sg1 div
{
clear: both;
float: left;
}
Next you can put
#sg1 label
{
float: right;
}
input
{
display:block;
}
Related
What is the most elegant (ie. non-hacky) way to properly align a group of checkboxes, one or more of which have associated text inputs? Whatever I have tried, I end up with (i) unevenly spaced checkboxes, or (ii) misaligned checkboxes, or (iii) misaligned labels, or (iv) misaligned text boxes.
Since the use case of:
What is your favourite food?
(a) Apple
(b) Banana
(c) Carrot
(d) Other: _______
is surely very common, I wonder whether anyone has found a way to do this with Bootstrap standard classes, before I start forcing my own classes into the code.
Here is the code I have now:
<div class="form-group">
<label class="col-sm-2 control-label">
Favourite food
</label>
<div class="col-sm-10">
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox"/>
Apple
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox"/>
Banana
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox"/>
Other <input type="email" placeholder="someone#somewhere.com" class="form-control"/>
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox"/> Any fruit from one of these colour groups: Red, Green, Blue
</fieldset>
</label>
</div>
</div>
</div>
Screenshot attached showing output in Chrome:
The fourth checkbox is too far from the third, while the text and 'input type=text' on the third line are too low.
Yes, there is a simple bootstrap way, just change the code of the checkbox to this:
<div class="checkbox">
<div class="form-inline">
<label>
<input type="checkbox"/>
Other
</label>
<input type="email" placeholder="someone#somewhere.com" class="form-control"/>
</div>
</div>
Working Example
Here's one way to achieve what you're trying to do. The only change which I've made to your HTML structure is that I've wrapped the description for each checkbox inside a <span> tag and added used this CSS:
input[type="checkbox"] {
position: relative !important;
display: inline-block;
vertical-align: text-bottom;
}
span {
display: inline-block;
vertical-align: middle;
}
Here's a working demo:
input[type="checkbox"] {
position: relative !important;
display: inline-block;
vertical-align: text-bottom;
}
span {
display: inline-block;
vertical-align: middle;
}
.form-inline .form-control {
padding-top: 0px;
padding-bottom: 0px;
height: 20px;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="form-group">
<label class="col-sm-2 control-label">Favourite food</label>
<div class="col-sm-10">
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox" /> <span>Apple</span>
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox" /> <span>Banana</span>
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox" /> <span>Other</span>
<input type="email" placeholder="someone#somewhere.com" class="form-control" />
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox" /> <span>Any fruit from one of these colour groups: Red, Green, Blue</span>
</fieldset>
</label>
</div>
</div>
</div>
I try currently to beautify my sources a little bit. For this I want to change the width of buttons and radio buttons to 80% and change the alignment to center. For the buttons I made css settings that works quite fine, but I am not able to center the radio button group.
HTML:
<div data-role="page" id="testPage" data-theme="e">
<fieldset data-role="controlgroup">
<legend id="SettingsDifficulty"></legend>
<input class="rbgroup1" type="radio" name="test" id="radio-choice-1" value="3" />
<label for="radio-choice-1" id="label1" class="activeOnce">Radio 1</label>
<input class="rbgroup1" type="radio" name="test" id="radio-choice-2" value="4" />
<label for="radio-choice-2" id="label2" class="activeOnce">Radio 2</label>
<input class="rbgroup1" type="radio" name="test" id="radio-choice-3" value="5" />
<label for="radio-choice-3" id="label3" class="activeOnce">Radio 3</label>
</fieldset>
</br>
<hr>
</br>
Button
</div>
CSS:
.ui-btn.activeOnce
{
width:80%;
margin-left: auto;
margin-right: auto;
text-align: center;
}
The single button at the end is now at 80% width and perfect centered. The radio buttons are also at 80% width but still left aligned. At the web I found some solutions for horizontal radio button groups, but this solutions does not work with data-type="vertical". Is there a way to center this, too?
Thank you very much for helping me :-).
JSFIDDLE: http://jsfiddle.net/7eKZb
Use jQuery Mobile grid system, ui-grid-b and 3 blocks ui-block-a, ui-block-b and ui-block-c.
Demo
<div class="ui-grid-b">
<div class="ui-block-left ui-block-a"><!-- Placeholder --></div>
<div class="ui-block-center ui-block-b">
<fieldset data-role="controlgroup">
<!-- Buttons go here -->
</fieldset>
</div>
<div class="ui-block-right ui-block-c"><!-- Placeholder --></div>
</div>
And override width of blocks a, b and c. I used extra custom classes in order not to override other blocks.
.ui-block-left, .ui-block-right {
width: 10% !important;
}
.ui-block-center {
width: 80% !important;
}
See this fiddle http://jsfiddle.net/bB2vM/
i wrapped your radio button group in a div and it worked see the fiddle
<div data-role="page" id="testPage" data-theme="e">
<fieldset data-role="controlgroup">
<legend id="SettingsDifficulty"></legend>
<div id="centregroup">
<input class="rbgroup1" type="radio" name="test" id="radio-choice-1" value="3" />
<label for="radio-choice-1" id="label1" class="activeOnce">Radio 1</label>
<input class="rbgroup1" type="radio" name="test" id="radio-choice-2" value="4" />
<label for="radio-choice-2" id="label2" class="activeOnce">Radio 2</label>
<input class="rbgroup1" type="radio" name="test" id="radio-choice-3" value="5" />
<label for="radio-choice-3" id="label3" class="activeOnce">Radio 3</label>
</div>
</fieldset>
</br>
<hr>
</br>
Button
and here is the css
.ui-btn.activeOnce
{
width:80%;
margin-left: auto;
margin-right: auto;
text-align: center;
}
#centregroup
{
text-align:center;
}
See screeshot.
U can group your radio buttons inside a div with fixed width and height:
<fieldset data-role="controlgroup" data-type="vertical" style="text-align: center">
<div class="centerRadio">
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" />
<label for="radio-choice-1">A</label>
<input data-theme="e" type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2" />
<label for="radio-choice-2">B</label>
</div>
</fieldset>
CSS:
.centerRadio
{
width: 80%; margin: 0 auto;
}
DEMO
I have a form with two fieldset elements
<form>
<fieldset>
</fieldset>
<fieldset>
</fieldset>
</form>
I need to make to be horizontally?
Just float them and add a width.
fieldset {
float: left;
width: 50%;
}
You could put them in a row fluid span6. You can do some other things, but the well will cause issues with row.
<form class="row-fluid well">
<fieldset class='span6'>
<legend>Legend 1</legend>
<label class="radio">
<input type="radio" name="option1"/> Option1
</label>
<label class="radio">
<input type="radio" name="option2"/> Option2
</label>
</fieldset>
<fieldset class='span6'>
<legend>Legend 2</legend>
<select>
<option> option1 </option>
</select>
</fieldset>
</form>
you can do that by add to the mother:
overflow: hidden;
and add to the fieldset:
display: block; float: left;
See the example: http://jsfiddle.net/JXf8g/25/
Im using jQuery Mobile and I have a horizontal toggle set.
I would like to center it in the page, or I would like to stretch it so that it takes up 100% of the width.
Either would be good for what I am doing.
I have tried a lot of different approaches but none have worked so far.
Using jQuery Mobile v1.0
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="checkbox" name="monday" id="monday" class="custom"/>
<label for="monday">Monday</label>
<input type="checkbox" name="tuesday" id="tuesday" class="custom" />
<label for="tuesday">Tuesday</label>
<input type="checkbox" name="wednesday" id="wednesday" class="custom" />
<label for="wednesday">Wednesday</label>
<input type="checkbox" name="thursday" id="thursday" class="custom" />
<label for="thursday">Thursday</label>
<input type="checkbox" name="friday" id="friday" class="custom" />
<label for="friday">Friday</label>
<input type="checkbox" name="saturday" id="saturday" class="custom" />
<label for="saturday">Saturday</label>
<input type="checkbox" name="sunday" id="sunday" class="custom" />
<label for="sunday">Sunday</label>
</fieldset>
I just had this same problem and I solved it by adding by putting the fieldset within a div which has text-align: center and then setting the display of the fieldset to inline.
E.g.
HTML
<div class="custom">
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="checkbox" name="monday" id="monday" />
<label for="monday">Monday</label>
...
</fieldset>
</div>
CSS
.custom {
text-align: center;
}
.custom fieldset {
display: inline;
}
Setting the display of the fieldset to inline may have some other effects, for example I noticed that the margins/padding changed slightly, but apart from that it did what I wanted.
i have this html structure:
<form id="signUpForm" action="login.php" method="post">
<h3>Please enter your details below:</h3>
<br />
<p>Name: </p>
<input id="name" name="name" type="text"></input>
<br />
<p>Email: </p>
<input id="email" name="email" type="text"></input>
<br />
<p>Company: </p>
<input id="company" name="company" type="text"></input>
<br />
<p id='required'>* All fields are required</p>
<p>Enquiry Details</p>
<textarea></textarea>
<input id="signUpSubmit" type="submit" value="Send"></input>
</form>
This form is underneath a div element in the html but the div element has float: left on it, so the div and form sit side by side. The thing i want to do is have the 3 text inputs vertically, then have the textarea top right of the form. so the inputs and the textarea are then side by side?
Does that make sense?
You need to make some adjustments to your markup, adding two divs for the left and right columns would make things far easier:
<form>
<div class="left-column">
<label for="name">Name: </label>
<input id="name" name="name" type="text"/>
<label for="email">Email: </label>
<input id="email" name="email" type="text"/>
<label for="company">Company: </label>
<input id="company" name="company" type="text"/>
</div>
<div class="right-column">
<label for="ta">Enquiry Details: </label>
<textarea id="ta" name="ta"></textarea>
</div>
</form>
Then using some simple CSS, you may create the two column effect:
form {
position: relative;
}
form div.left-column {
position: absolute;
width: 50%;
left: 0;
}
form div.right-column {
position: absolute;
width: 50%;
right: 0;
}
Then using correct CSS styling on your inputs in order to allow them to display vertically without using <br /> tags in your markup:
form input, form label {
float: left;
clear: left;
}