I am trying to figure out how to align the left side of all of the inputs together. Right now the entire .element row is aligned to the left, so all of the input fields are staggered. I don't want the inputs stagger I want the labels text aligned to the right and the inputs all aligned together. I've tried a bunch of random CSS, but I can't seem to figure it out. Any ideas?
<form>
<div class="element"><label>Name:</label><input name="Name" type="text" /></div>
<div class="element"><label>Email:</label><input name="Email" type="text" /></div>
<div class="element"><label>Phone:</label><input name="Phone" type="text" /></div>
<div class="element"><label>Address Line 1:</label><input name="Address1" type="text" /></div>
<div class="element"><label>Address Line 2:</label><input name="Address2" type="text" /></div>
<div class="element"><label>City:</label><input name="City" type="text" /></div>
<div class="element"><label>State:</label><input name="State" type="text" /></div>
<div class="element"><label>Zip Code:</label><input name="ZipCode" type="text" /></div>
<div class="element"><label>Birth Date:</label><input name="Bdate" type="text" /></div>
<div class="element"><label>Class Selection:</label><input name="Class" type="text" /></div>
<div class="element"><label>Preferred Meeting Time:</label><input name="Time" type="text" /> </div>
</form>
#registerwrapper .element {
margin-bottom: 15px;
vertical-align: middle;
}
#registrationwrapper label * {
float: left;
font-size: 16px;
}
#registrationwrapper input {
padding: 5px;
margin-left: 10px;
}
Hey now used to this stylesheet Give to your label Display inline-block; and give to width according to your layout
Live demo
The direct answer to your question is this:
label {
display: block;
margin-right: 5px; /* give some breathing room between your label and inputs */
padding-top: ?px; /* add this if you're v-aligning to top to adjust the label alignment to the input */
text-align: right;
vertical-align: top; /* add this when you've got larger fields like textareas bumping things out of alignment */
width: ?px;
}
But, I feel compelled to ask why you're wrapping all your label/field combos with divs (layers)? I recommend using paragraphs instead, like so:
<form>
<p>
<label for="">Field</label>
<input id="" />
</p>
</form>
And you also don't need to add a class to each row. Just do this:
form p {
}
form p label {
}
form p input {
}
Now the amount of bytes you're sending to the client is far less, and the HTML is far easier to read.
Did Some changes your code,
See the link : http://jsfiddle.net/anglimass/V4HAM/8/
Related
I have following html and my goal is to align the labels for the text boxes so that they are aligned with left edges of their text boxes. Right now they are center aligned due to css on outermost div. Also, I do not want to lose the center alignment of the outermost div.
I tried setting text-align:left for each of the labels but it did nothing.
A demo for this question is at following URL: Demo
Question : What css I need to use to left align the labels?
<div style="margin: 0 auto; text-align: center; margin-top: 20px; width: 800px;">
<div>
<fieldset>
<label for="txtName">Full Name</label><br>
<input id="txtName" type="text" style="width:80%" /><br>
<label for="txtCity">City</label><br>
<input id="txtCity" type="text" style="width:80%" /><br>
</fieldset>
</div>
</div>
You need to use:
display: block for label as it is an inline element.
You haven't closed <div> correctly.
Remove the <br> and update this way:
See the updated snippet, that works:
label {
text-align: left;
display: block;
text-indent: 10%;
}
<div style="margin: 0 auto; text-align: center; margin-top: 20px; width: 800px;">
<div>
<fieldset>
<label for="txtName">Full Name</label>
<input id="txtName" type="text" style="width:80%" /><br>
<label for="txtCity">City</label>
<input id="txtCity" type="text" style="width:80%" /><br>
</fieldset>
</div>
</div>
Preview:
i'm trying to put a text field of fixed height 100px and then a subsequent input field into a page, all floated left. If i were to have two text fields placed in the page one after the other, and I wanted to provide spacing between the two fields, I would traditionally use a tag to provide a line space between two elements.
<input type="text" class="formInput" name="item1" placeholder="item
<br>
<input type="text" class="formInput" name="item2" placeholder="item2">
When I try to do the same thing after my text field, no spacing is made until I hit about the 4th or 5th tag.
<textarea class="formInput" name="item3" placeholder="item3" style="height: 100px">
</textarea>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<input type="text" class="formInput" name="item4" placeholder="item4" style="clear: both; float:left;">
I'm assuming that is because the tag indexes the line break size from the start of the last element in the page and not the end height of where it is displayed on the page. To that effect, can anyone suggest an appropriate way to structure my html or subsequent styling so i don't have to use 5 tags to space out the text and input fields appropriately? Perhaps there is a more effective HTML element or trick in css?
EDIT: here is the css associated with these elements of class
.formInput{
float: left;
border: 1px solid #999999;
border-radius: 3px;
width: 200px;
height: 20px;
font-size: 1.2em;
}
it was made apparent that this is necessary to answer the question more fully.
First off, if you want them both to float left, surround both inputs with a div that has the float:left style and use a single line break between them.
As for separating the fields, you'd be better off using a top (or bottom) margin on either input set to a value to give them appropriate separation.
<div style="float:left;">
<textarea class="formInput" name="item3" placeholder="item3" style="height: 100px;margin-bottom:50px;"></textarea>
<br/>
<input type="text" class="formInput" name="item4" placeholder="item4"/>
</div>
Example JSFiddle: http://jsfiddle.net/ncr61xvL/
<html>
<head>
<style>
.container{
width: 300px;
}
input[type='text'],textarea{
width: 200px;
float: left;
padding: 5px;
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<input type="text" class="formInput" name="item1" placeholder="item1">
<input type="text" class="formInput" name="item2" placeholder="item2">
<textarea class="formInput" name="item3" placeholder="item3" style="height: 100px"> </textarea>
<input type="text" class="formInput" name="item4" placeholder="item4" style="clear: both; float:left;">
</div>
</body>
</html>
https://jsfiddle.net/kjtn1eht/
I do not want to use hardcoded widths and table tags to generate the layout I want to achieve, but my CSS skills aren't developed quite yet. My layout is as follows:
<div id="container">
<div id="DataType">
Type of Data Request <input type="text" />
</div>
<div id="Arguments" >
Arguments <textarea name="arguments" rows="4" cols="45" ></textarea>
</div>
<div id="TargetEnvironment" >
Target Environment <input type="text" />
</div>
<div id="SubmitButton">
<button id="btnSubmit">Submit Request</button>
</div>
</div>
and I would like to have it behave a certain way with CSS. I can achieve it with tables and hard coded margins, but not with proper CSS. Here is what it currently looks like, and what I want it to look like
Here's a solution using float.
#container {
width: 500px;
}
label {
margin-top: 1em;
display: block;
vertical-align: top;
clear: right;
overflow: auto;
}
input, textarea {
width: 300px;
float: right;
padding: 2px;
}
button {
float: right;
}
<div id="container">
<label id="DataType">
Type of Data Request <input type="text" />
</label>
<label id="Arguments" >
Arguments <textarea name="arguments" rows="4" cols="45" ></textarea>
</label>
<label id="TargetEnvironment" >
Target Environment <input type="text" />
</label>
<label id="SubmitButton">
<button id="btnSubmit">Submit Request</button>
</label>
</div>
You can set the width and use float:right on your inputs.
I don't see any problem in using margins or paddings to separate the lines.
Another tip: use the label tag for the labels of your form.
I want to have two elements stay on the same row.
Right now I have this code:
<fieldset data-role="controlgroup" data-type="horizontal">
<label for="textinput">Text:</label>
<input type="text" id="textinput"/>
<input type="button" id="searchbutton" data-icon="search" data-iconpos="notext" onclick="function()"/>
</fieldset>
This works. The label, the input field and the button will all be on the same row as long as you view it in fullscreen in your computer browser. But if we make the window smaller, all three elements will be shown on one row each. Is there any way to make the label appear on one row, and the input field + button on the row below?
You need to override the jQM enhancements:
http://jsfiddle.net/E4EVT/10/
http://jsfiddle.net/E4EVT/36/ (Using the grid layout)
http://jsfiddle.net/E4EVT/42/ (Using the table layout)
JS
$('#textinput2').css('width','60%').css('display','inline');
HTML
<div>
<!-- use span instead of label -->
<span>Text:</span>
<input type="text" id="textinput2"/>
<br />
<input type="button" id="searchbutton2" data-icon="search" data-iconpos="notext" onclick="function()"/>
</div>
I think you might want to look into the grid layout jQM offers
http://jquerymobile.com/demos/1.0rc1/docs/content/content-grids.html
For Jquery Mobile 1.2.0
<div class="ui-grid-a" >
<div class="ui-block-a"><input type="text" /></div>
<div class="ui-block-b"><input type="text" /></div>
</div>
you need to add attribute data-inline="true" to the input elements.
CSS:
label {
display: block;
}
input {
padding: 2px;
width: 100px;
}
.wrap {
width: 212px; /* the width of twice your input (plus borders) */
}
And your HTML:
<fieldset data-role="controlgroup" data-type="horizontal">
<label for="textinput">Text:</label>
<div class="wrap">
<input type="text" id="textinput"/>
<input type="button" id="searchbutton" data-icon="search" data-iconpos="notext" onclick="function()"/>
</div>
</fieldset>
http://jsfiddle.net/ahallicks/BWsdk/
Edit:
Sorry, misread your question! If you want them all on the same line to start with use the following CSS:
label {
float: left;
margin-right: 12px;
}
input {
padding: 2px;
width: 100px;
}
.wrap {
float: left;
width: 212px; /* the width of twice your input (plus borders) */
}
http://jsfiddle.net/ahallicks/E4EVT/
Let's say I have an html snippet like this:
<div style="width:300px;">
<label for="MyInput">label text</label>
<input type="text" id="MyInput" />
</div>
This isn't my exact code, but the important thing is there's a label and a text input on the same line in a fixed-width container. How can I style the input to fill the remaining width of the container without wrapping and without knowing the size of the label?
Here is a simple and clean solution without using JavaScript or table layout hacks. It is similar to this answer: Input text auto width filling 100% with other elements floating
It is important to wrap the input field with a span which is display:block. Next thing is that the button has to come first and the the input field second.
Then you can float the button to the right and the input field fills the remaining space.
form {
width: 500px;
overflow: hidden;
background-color: yellow;
}
input {
width: 100%;
}
span {
display: block;
overflow: hidden;
padding-right:10px;
}
button {
float: right;
}
<form method="post">
<button>Search</button>
<span><input type="text" title="Search" /></span>
</form>
A simple fiddle: http://jsfiddle.net/v7YTT/90/
Update 1: If your website is targeted towards modern browsers only, I suggest using flexible boxes. Here you can see the current support.
Update 2: This even works with multiple buttons or other elements that share the full with with the input field. Here is an example.
as much as everyone hates tables for layout, they do help with stuff like this, either using explicit table tags or using display:table-cell
<div style="width:300px; display:table">
<label for="MyInput" style="display:table-cell; width:1px">label text</label>
<input type="text" id="MyInput" style="display:table-cell; width:100%" />
</div>
I suggest using Flexbox:
Be sure to add the proper vendor prefixes though!
form {
width: 400px;
border: 1px solid black;
display: flex;
}
input {
flex: 2;
}
input, label {
margin: 5px;
}
<form method="post">
<label for="myInput">Sample label</label>
<input type="text" id="myInput" placeholder="Sample Input"/>
</form>
Please use flexbox for this. You have a container that is going to flex its children into a row. The first child takes its space as needed. The second one flexes to take all the remaining space:
<div style="display:flex;flex-direction:row">
<label for="MyInput">label text</label>
<input type="text" id="MyInput" style="flex:1" />
</div>
Easiest way to achieve this would be :
CSS :
label{ float: left; }
span
{
display: block;
overflow: hidden;
padding-right: 5px;
padding-left: 10px;
}
span > input{ width: 100%; }
HTML :
<fieldset>
<label>label</label><span><input type="text" /></span>
<label>longer label</label><span><input type="text" /></span>
</fieldset>
Looks like : http://jsfiddle.net/JwfRX/
Very easy trick is using a CSS calc formula. All modern browsers, IE9, wide range of mobile browsers should support this.
<div style='white-space:nowrap'>
<span style='display:inline-block;width:80px;font-weight:bold'>
<label for='field1'>Field1</label>
</span>
<input id='field1' name='field1' type='text' value='Some text' size='30' style='width:calc(100% - 80px)' />
</div>
you can try this :
div#panel {
border:solid;
width:500px;
height:300px;
}
div#content {
height:90%;
background-color:#1ea8d1; /*light blue*/
}
div#panel input {
width:100%;
height:10%;
/*make input doesnt overflow inside div*/
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
/*make input doesnt overflow inside div*/
}
<div id="panel">
<div id="content"></div>
<input type="text" placeholder="write here..."/>
</div>
The answers given here are a bit outdated. So, here I'm with the easiest solution using modern flexbox.
.input-container{
display:flex;
}
input{
flex-grow: 1;
margin-left: 5px;
}
<div style="width:300px;">
<div class="input-container">
<label for="MyInput">label text: </label>
<input type="text" id="MyInput"/>
</div>
<div class="input-container">
<label for="MyInput2">Long label text: </label>
<input type="text" id="MyInput2" />
</div>
</div>
If you're using Bootstrap 4:
<form class="d-flex">
<label for="myInput" class="align-items-center">Sample label</label>
<input type="text" id="myInput" placeholder="Sample Input" class="flex-grow-1"/>
</form>
Better yet, use what's built into Bootstrap:
<form>
<div class="input-group">
<div class="input-group-prepend">
<label for="myInput" class="input-group-text">Default</label>
</div>
<input type="text" class="form-control" id="myInput">
</div>
</form>
https://jsfiddle.net/nap1ykbr/