Sounds easy but say I'm using bootstrap and the input box has a label, when I resize the text underneath is not centered.
Here's some simple html to demo:
<form action="http://google.com">
<div>
<label for="inputBox">Email address</label>
<input name="email" type="text" id="inputBox"/>
<br>
(Center under input)
</div>
</form>
The label should be associated with the input textbox, the text '(Center under input) should appear under the input box in the middle.
So ideally the (Center under input) text should be glued centrally underneath the input textbox while the label acts normally.
Is this possible? I'm trying with a table (of all things) at the moment but can't get it to behave, I've tried positioning etc. still no luck.
Any help is much appreciated.
If you want to centre things relative to each other, then they should generally be placed in a container.
div,
label {
display: inline-block;
vertical-align: top;
}
div {
text-align: center;
}
<form action="http://google.com">
<div>
<label for="inputBox">Email address</label>
<div>
<input name="email" type="text" id="inputBox" />
<br>(Center under input)
</div>
</div>
</form>
Your example content is too abstract to tell, but your centred content should probably be another label element if it is associated with the input.
You can kill 2 birds with one stone - have an accessible input that is compatible with screen readers, and center your text, using this markup structure:
label {
max-width:300px;
display:block;
}
label span {
text-align:left;
display:block;
}
label input {
width:100%;
}
<form action="http://google.com">
<label>
<span>Email address</span>
<input name="email" type="text" id="inputBox"/>
<span style="text-align:center">center me</span>
</label>
</form>
Notice you don't need the for attribute. It's a clean way to accomplish accessibility. Some simple CSS aligns the text. If you don't want to use inline styles, you could always write another class.
Related
I'm using Foundation 6 framework to construct my pages. I have a separate style sheet called effects.css that I use to customize text and tables and inputs and such.
I actually have to style it, because without the custom styles the input fields are huge and grotesque. Must be some scaled styles in the foundation.css that are responsible.
I made a short, thin text input field, but the text inside doesn't seem to want to vertically line up correctly. The cursor blinks up and sort of half way out of the white text area.
Here's the html:
<form name="quick-connect" id="quick-connect" method="post" action="/ajax/quick-connect-action/" onsubmit="return false;">
<span class="med-text white-text">Your name:</span><input type="text" name="name" id="name" class="small-input" />
<span class="med-text white-text">Your phone #:</span><input type="text" name="number" id="number" class="small-input" />
<input type="submit" name="submit" id="quick-connect-submit" value="Submit" />
<input type="hidden" name="page" value="LCHG" />
</form>
Here's my attempt at css'ing the input fields:
input.small-input{
width:10em;
height:1em;
line-height:1em;
font-size:.9em;
color:#000;
}
Here's what it looks like:
The blinking text cursor isn't vertically align within the text field.
It was a padding problem. I set the padding to 0 within the input style, and the text properly vertically aligns now.
input.small-input{
width:10em;
height:2em;
line-height:2em;
font-size:.8em;
color:#000;
padding:0;
}
I'm putting together a simple form to deposit a set amount of money. The form has 4 radio buttons.
<form action="deposit.php" method="post">
<fieldset>
<div class="form-group">
<input type="radio" name="deposit" value="100"> $100
<br>
<input type="radio" name="deposit" value="250"> $250
<br>
<input type="radio" name="deposit" value="500"> $500
<br>
<input type="radio" name="deposit" value="1000"> $1,000
<br>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Deposit</button>
</div>
</fieldset>
</form>
Here is the output:
You can see that the last radio button is not vertically aligned. I realize that there are 2 extra characters in the text after the button. I'm no CSS wizard and haven't really found the answer to making these buttons straight. Any ideas?
EDIT: CSS code here:
.container {
/* center contents */
margin-left: auto;
margin-right: auto;
text-align: center;
}
input[type='radio'] {
width: 20px;
}
The form is in the container
In your CSS file or within your HTML code you are using the "align centre" setting. Thats why is not aligned.
This is the output of your above code with the align centre setting:
And this is the output of your code without the align centre setting:
Somewhere in your code you are setting the below div class to align centre.
// Removing the align setting will solve the issue. If its not in-line CSS then check you external CSS file.
<div class="form-group" align="center">
Hope this helps.
Why not use tables to make the buttons align (its easier with dreamweaver as your editing tool)
I get the following snippet of html from a CMS:
<label title="">
Land
<input id="Land" name="Land" size="20" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="Land" data-valmsg-replace="true"></span>
</label>
I need to center the text Land, but I can't seem to figure it out. I've tried setting text-align: center on the label, but it doesn't work. I can't modify the markup. Is it something simple I'm missing?
EDIT:
I want to center it over this input:
Don't mind the right edge of the input field. The label span 100% of the width of the input field.
There is two ways to deal with that .
1.The display property specifies if/how an element is displayed so label must be displayed as block or inline-block to center it
label {
display: inline-block;
text-align: center;
}
2.you can use html tag center
<center>
<label title="">
Land
<input id="Land" name="Land" size="20" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="Land" data-valmsg- replace="true"></span>
</label>
</center>
And if you want to center the word Land over the centered Label , you can just add this
<center>Land</center>
the tag is an inline element and therefore exactly the same width of its content.
You would need a div or something to do this
Doing this without being able to alter the markup itself would be messy at best and will probably consist of some javascript/jquery
the label is not expanding on 100% width because it's an inline element. to enable this - set on the label css:
label {
display: block;
text-align: center;
}
or insert the text directly into a block element like: div, etc.
Edit:
Simply add the following CSS to achieve the desired result:
label input, label span {
display: block;
}
See a live demo: http://jsbin.com/sebinuje/1/edit?html,css,output OR http://jsfiddle.net/mayank_agarwal/5yUQr/
Enclose the snippet with a div element and apply the text-align property on the div.
HTML:
<div id="label-enclosure">
<label title="">
Land
<input id="Land" name="Land" size="20" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="Land" data-valmsg-replace="true">
</span>
</label>
</div>
CSS:
#label-enclosure{
text-align:center;
}
Although I'm loath to recommend using a <br/> tag here is one way given your current structure
JSfiddle
HTML
<label title="">
Land
<br/>
<input id="Land" name="Land" size="20" type="text" value=""/>
<span class="field-validation-valid" data-valmsg-for="Land" data-valmsg-replace="true"></span>
</label>
CSS
label {
text-align: center;
display: inline-block;
}
I'm really not that good at CSS, and I want to know how to correctly style a form in a manner that it puts each single text input and label in a line. like this :
<label for="input1">...</label>
<input type="text" id="input1"/>
<label for="input2">...</label>
<input type="text" id="input2"/>
<label for="input3">...</label>
<input type="text" id="input3"/>
<label for="input3">...</label>
<input type="text" id="input3"/>
and it would be shown in the webpage like :
(label)(input)
(label)(input)
(label)(input)
(label)(input)
<label>foo</label>
<input type="text"/>
<label>foo</label>
<input type="text"/>
<style>
input, label { float:left }
label { clear:left; }
</style>
http://jsfiddle.net/RpRS5/
I recommend this tutorial by A List Apart about Prettier Accessible Forms. You can also use a definition list with some custom styling, e.g.,
<dl><dt><label></label></dt>
<dd><input></dd></dl>
And something like:
dl dt {
float: left;
width: 8em;
}
Edit: to sum up the A List Apart article, they suggest you put form fields in an ordered list ol. Labels are displayed as inline-block so they appear horizontally next to their associated fields.
Put them in a list, or in a structure like a list (that is to say, wrap each "row" in a div).
Put your inputs inside the label element and then you can simply display: block them or float them, I prefer display but it would be easy enough to change.
<label>Hello <input type="radio" name="what" value="Hello" /></label>
http://jsfiddle.net/Bpxfp/
http://jsfiddle.net/ud7YE/1/
you can control the space between the label and input by varying the width of the wrapper. Just set the height of the label and the top margin of the input same in value but negative
I find enclosing label and input or select tags in a div or list. And the label and select tags should be of type inline-block
<div>
<label>Name: </label><input type="text" />
</div>
<div>
<label>Place: </label><input type="text" />
</div>
CSS:
label {
display: inline-block;
}
input {
display: inline-block;
padding: 2px;
}
div {
display: block;
margin: 2px 0;
}
This would work out well.
How do I correct the following E-mail textbox alignment: ?
To make it look like this:
I know I can use tables, but how do I solve this problem without using tables? CSS maybe?
HTML:
<form action="" name="contactform" method="post">
<p></p>
First name: <input type="text" class="contact" name="contactfirstname" value="">
<br/>
Last name: <input type="text" class="contact" name="contactlastname" value="">
<br/>
E-mail: <input type="text" class="contact" name="email" value="">
<p></p>
The most minimalized version I could think of...
<form>
<label>First Name: <input type="text" name="firstName"></label>
<label>Last Name: <input type="text" name="lastName"></label>
<label>Email Address: <input type="email" name="emailAddress"></label>
</form>
and
form {
width: 300px;
}
label {
display: block;
margin: 5px;
padding: 5px;
clear: both;
}
label input {
float: right;
}
Since OP has edited his question to include his markup, I'll expand the answer.
Some Points of Improvement:
Remove the empty <p> element, and the <br/> elements. They have no value inside a form.
Use <label>s, that's what they were made for. You can wrap the label and the input inside of the <label> tag, or you can use <label for="element_id">Label</label><input id="element_id">.
Be consistent. If you decided to go with the <br /> type of format for singular tags, stick with it to the <input />s as well.
Use correct input types for specific inputs, there is type="email" for the email field, which will optionally have the browser check for you if it's a valid email address or not!.
Use CSS for design and layout, not <p>s and <br>s.
Good luck!
I'm assuming your HTML is something like:
<p>
Email
<input />
</p>
Change this to:
<p>
<label>Email</label>
<input />
</p>
This means you can then apply a fixed width to all your labels, making them consistent:
label
{
width:100px;
float:left;
}
http://jsfiddle.net/zvWqk/1/
Or as #Zeta has pointed out, nest your input inside the label, and float right. This will prevent you needing to apply a for attribute to your label.
http://jsfiddle.net/tt8gx/
Use CSS to make the labels display as block elements and have a fixed width. Display the inputs as block elements and float them left. Put a clear:left on the labels so they'll each be on a new line.