How to justify form input fields with css? - html

I have a simple signup input form (haml for brevity here:)
%form#signup
%fieldset
%input.email{type:'text'}
%br
.name
%input.first-name{type:'text'}
%input.last-name{type:'text'}
and css:
#signup { width: 350px; }
fieldset { width: 100%; }
.name { width: 100%; }
.first-name { width: 30%; }
.last-name { /* occupy remainder of 'name' line */ }
How to style this so that the .email field is the full width of the fieldset and the .last-name and/or .first-name fields expand to also fill the entire width of the fieldset and with the right edge aligned with the .email field?
Yes it might be easier to use a table here but is there a simple way with css? It need only work for css3 compliant browsers and degrade reasonably for IE8 and 9.
fiddle link: http://jsfiddle.net/3UP9H/1

Original answer appears below the hr; the answer to the question, for clarity, appears to be a combination of box-sizing (and its vendor-previxed variants), in order to include the border-width and padding in the defined width of the elements(s) (rather than their width being defined-width + border-width + padding) and font-size: 0 for the parent element, which removes the errant space between the two input elements (although the space is, technically, still there; it just doesn't have any size to influence the position of the surrounding elements).
So, the CSS is that from the second example below:
fieldset input[type=text] {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-o-box-sizing: border-box;
box-sizing: border-box;
width: 350px;
}
fieldset div input[type=text] {
width: 105px;
margin: 0;
}
fieldset div input[type=text] + input[type=text] {
float: right;
width: 245px;
}
div.name {
font-size: 0;
}​
JS Fiddle demo.
Original answer follows:
One way seems to be:
form {
width: 350px;
}
fieldset {
width: 100%;
}
​fieldset input[type=text] {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-o-box-sizing: border-box;
box-sizing: border-box;
display: inline-block;
width: 350px;
}​​
fieldset div input[type=text] {
width: 105px;
margin: 0;
}
fieldset div input[type=text] + input[type=text] {
float: right;
width: 241px;
}​
JS Fiddle demo.
The use of box-sizing (and the vendor-prefixed variants) is to simply include the border of the element, and any assigned padding within the defined width of the element.
I've used self-closing input tags in the linked demo, since input elements, so far as I know, don't have closing tags </input>.
I've amended the above, slightly, to remove the issue of the errant space (between the sibling input elements in the .name element from requiring arbitrary corrections to allow them both on the same line (hence the strange width: 241px in the above CSS):
form {
width: 350px;
}
fieldset {
width: 100%;
}
fieldset input[type=text] {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-o-box-sizing: border-box;
box-sizing: border-box;
display: inline-block;
width: 350px;
}
fieldset div input[type=text] {
width: 105px;
margin: 0;
}
fieldset div input[type=text] + input[type=text] {
float: right;
width: 245px;
}
div.name {
font-size: 0;
}​
JS Fiddle demo.
Edited to remove the fixed-width measurements, and replaced with relative, percentage, based units (as in the original question):
form {
width: 350px;
}
fieldset {
width: 100%;
}
fieldset input[type=text] {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-o-box-sizing: border-box;
box-sizing: border-box;
display: inline-block;
width: 100%;
}
fieldset div input[type=text] {
width: 30%;
margin: 0;
}
fieldset div input[type=text] + input[type=text] {
float: right;
width: 70%;
}
div.name {
font-size: 0;
}​
JS Fiddle demo.
Unfortunately there's no way to set the width of the input elements to 100% by default, while still allowing sibling input elements to have differing widths. Or, there is, but it's substantially more awkward and requires you to explicitly identify both siblings as, although it's possible to select the second, or later, sibling with the + or ~ combinators it's not possible to select the first sibling based on its having subsequent siblings (without JavaScript, or other scripting language, whether client-, or server-, side).

#signup { width: 350px; }
fieldset { width: 100%; border: 1px solid grey; padding:2px;}
.email { width: 99%;margin-bottom:2px; }
.name { width: 100%; }
.first-name { width: 30%; }
.last-name { width : 67% }
DEMO
Update
#signup { width: 350px; }
fieldset { width: 100%; border: 1px solid grey; padding:2px;}
.email { width: 99%;margin-bottom:2px; }
.name { width: 100%; }
.first-name { width: 30%; }
.last-name { width : 67%; float:right; }
DEMO
Firefox Screenshot.

Change these two lines to look like this:
.email { width: 99%; float: right; }
.last-name { width: 65%; float: right;}​
Fiddle Link
EDIT The odd thing is that about width in Chrome and IE is that there is an extra 4px width that isn't there in Firefox. The problem is that Chrome and IE add the border to the width of the box, while Firefox compensates the width of the internal textfield to make it fit the border within the bounds specified. See this version of the fiddle for a demonstration.
EDIT2 Check this updated fiddle.

Related

Remove HTML form submit button padding-like vertical space

I have a problem with HTML forms.
#subscription {
display: block;
margin: auto;
background-color: blue;
width: 550px;
height: auto;
}
form#subscription input#subscription-text {
width: 200px;
height: 30px;
background: orange;
border-style: none;
}
form#subscription input#subscription-submit {
width: 200px;
height: 30px;
background-color: rgb(208, 225, 125);
border-style: none;
padding: 0;
margin: 0;
}
<form id="subscription" action="subscription">
<input id="subscription-text" type="text" placeholder="INPUT">
<input id="subscription-submit" type="submit" value="SUBMIT">
</form>
Despite the fact, that I have removed all the margins and paddings for a submit button, it still has a padding-like VERTICAL spacing:
Why is that so, and how could I remove this spacing?
In fact there are TWO issues here...
The horizontal spacing is cause by whitespace in the HTML which affects inline/inline-block elements.
That subject is covered extensively in How to remove the space between inline-block elements?
The second issue is the disparity in the sizes of the two inputs.
This is caused by the fact the the two input types have different box-sizing default properties.
So we apply an overriding default reset:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
and a solution to the whitespace (here I used floats and a quick overflow clearfix)
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
form#subscription {
display: block;
margin: auto;
background-color: blue;
width: 550px;
overflow: auto;
}
form#subscription input#subscription-text {
width: 200px;
height: 30px;
background: orange;
border-style: none;
float: left;
}
form#subscription input#subscription-submit {
width: 200px;
height: 30px;
background-color: rgb(208, 225, 125);
border-style: none;
float: left;
}
<form id="subscription" action="subscription">
<input id="subscription-text" type="text" placeholder="INPUT">
<input id="subscription-submit" type="submit" value="SUBMIT">
</form>
you have two problem.
1. positioning of element and
2. box-sizing problem.
add the two line of code in the respective section of your css code as shown below.
form#subscription input#subscription-text {
width: 200px;
height: 30px;
background: orange;
border-style: none;
float: left; // this line solves prob-1
box-sizing: border-box; // this line solves prob-2
}
Learn about box-sizing here: https://css-tricks.com/box-sizing/
Learn about float here: https://developer.mozilla.org/en/docs/Web/CSS/float

How do I align 2 divs (each has its own padding) next to each other?

With the padding commented out as shown, the website works like I'd expect (2 div columns next to each other.
However, when I add padding, the #right div shifts downwards. How would I make it work as intended with padding?
HTML: Two divs contained directly in body
CSS:
#left {
background-color: green;
float: left;
margin-top: 0px;
width: 70%;
}
#right {
background-color: blue;
float: right;
margin-top: 0px;
width: 30%;
}
#left, #right {
//padding: 10px;
display: inline-block;
height: 800px;
}
add
box-sizing: border-box;
to your divs.
If you don't the padding is added outside the div width (or height).. same as borders
Edited: and
-webkit-box-sizing: border-box; -moz-box-sizing: border-box;
for a bit more browser compatibility
if you are using css3 , you can use box-sizing: border-box;
else, you can have another child div and apply padding to the child div instead of the parent div
#left {
background-color: green;
float: left;
margin-top: 0px;
width: 70%;
}
#right {
background-color: blue;
float: right;
margin-top: 0px;
width: 30%;
}
#left, #right {
padding: 10px;
display: inline-block;
height: 800px;
color:#fff;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
<div id="left"> left </div>
<div id="right"> right </div>

how to make div as child of another div?

I want to put three divs in order as following: input, break, ouput. And thier parent div is the container. I am facing problem in applying the box-sizing for these divs, here is my css:
html {
border: groove 8px red;
margin: 20px;
}
.container {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
height:75%;
}
.container .input {
width: 49%;
height: 100%;
border: solid 2px red;
float: left;
}
.container .break {
width: 2%;
height: 100%;
background: blue;
float: left;
}
.container .output {
width: 49%;
height: 100%;
border: solid 2px green;
float: right;
}
You have to apply box-sizing to the children as well:
.container > * {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
CSS's border-box property doesn't take into account margins. You'll need to set margin of 0 and adjust padding accordingly, but this may be undesired if you're using borders. You can try using percentages for margin so they (widths plus margins) add up to 100%. Also make sure that the child divs are inheriting box-sizing; you may need to define that specifically. I usually set this in CSS:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
Lastly, get rid of that .break div. Use margins instead.
Is your output div dropping below your input and break divs? That's because your border pixels are being added onto your widths.
49% + 2% + 49% + 8px of borders (2 on each side of input and 2 more on each side of output) > 100%
You'll have to try different things to get it to work, but dropping to 48% or even 45% might work. Since your area already floating left & right the extra space will just go in the middle.
this simple three-column layout DEMO
HTML
<div class="header">header</div>
<div class="layout">
<div class="col1">column 1</div>
<div class="col2">column 2</div>
<div class="col3">clolumn 3</div>
</div>
<div class="footer">footer</div>
CSS
.header, .footer { background: #D5BAE4; }
.layout { overflow: hidden; }
.layout DIV { float: left; }
.col1 { background: #C7E3E4; width: 20%; }
.col2 { background: #E0D2C7; width: 60%; }
.col3 { background: #ECD5DE; width: 20%; }
try this (compliments of _s). when you give it a % based width and then a px based border the default is to add them together, this should fix that.
*,
*:before,
*:after { /* apply a natural box layout model to all elements; see http://www.paulirish.com/2012/box-sizing-border-box-ftw/ */
-webkit-box-sizing: border-box; /* Not needed for modern webkit but still used by Blackberry Browser 7.0; see http://caniuse.com/#search=box-sizing */
-moz-box-sizing: border-box; /* Still needed for Firefox 28; see http://caniuse.com/#search=box-sizing */
box-sizing: border-box;
}
LIVE DEMO
HTML
<div class="container">
<div class="input">
</div>
<div class="break">
</div>
<div class="output">
</div>
</div>
CSS
html {
border: groove 8px red;
margin: 20px;
}
.container {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
height:75%;
}
.container div{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.container .input {
width: 49%;
height: 100%;
border: solid 2px red;
float: left;
}
.container .break {
width: 2%;
height: 100%;
background: blue;
float: left;
}
.container .output {
width: 49%;
height: 100%;
border: solid 2px green;
float: right;
}

css: let input field fill the space left

I'm trying to let a label and an input field fill the whole width of a from. Here is my attempt. This is the HTML
<form>
<p>
<label>Username:</label>
<input/>
</p>
</form>
and CSS
form {
widht: 400px height: 500px;
background-color: lightgrey;
}
label {
float: left;
}
input {
width: 100%;
box-sizing: border-box;
}
When I put a with: 100% in the input field it moves below the label and without the width it is too small. Any suggestions ?
Use a wrapper element around your input and set overflow: hidden; (Make sure you use a block level element, if you are using span than declare display: block; in your CSS)
Demo
<label>Blah Blah</label>
<div><input type="text" /></div>
label {
float: left;
}
div {
overflow: hidden;
}
input[type=text] {
width: 100%;
}
you have to set width attribute properly.
Live Demo
label {
float: left;
width: 15%;
}
input {
width: 83%;
box-sizing: border-box;
}
If you firebug it, you will know that Your border is actually putting some padding to the container p. So, if you just put border to none then you will be good to do.
If you want to use border and dont want the input to go outside the form, then you need to either shorten the width of the input a little and apply border or you give some padding to your form.
Try This CSS
form {
background-color: #D3D3D3;
height: 500px;
width: 400px;
}
form p{
float: left;
position: relative;
width: 100%;
}
label {
float: left;
position: relative;
width: 100%;
}
input {
border:0;
float: left;
height: 20px;
margin: 0 auto;
padding: 0;
position: relative;
width: 100%;
}
Here is the DEMO

How do make a variable-width input field

I am trying to make the label and input field appear on the same line, with a variable width input field that will expand based on the space available
http://jsfiddle.net/chovy/WcQ6J/
<div class="clearfix">
<aside>foo</aside>
<span><input type="text" value="Enter text" /></span>
</div>
.clearfix {
*zoom: 1;
}
.clearfix:before,
.clearfix:after {
display: table;
content: "";
}
.clearfix:after {
clear: both;
}
div {
border: 1px solid red;
}
aside {
display: block;
width: 100px;
background: #eee;
float: left;
}
span {
display: block;
width: 100%;
background: #ccc;
}
input {
width: 100%;
box-sizing: border-box;
border: 1px solid #000;
}
It works fine with a span, but when I add input it wraps to next line.
Here is some whacky solution. I honestly don't really understand why this works. I had it in an old codepen. Good luck!
http://jsfiddle.net/sheriffderek/DD73r/
HTML
<div class="container">
<div class="label-w">
<label for="your-input">your label</label>
</div>
<div class="input-w">
<input name="your-input" placeholder="your stuff" />
</div>
</div> <!-- .container -->
CSS
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.container {
width: 100%;
float: left;
height: 2em;
}
.label-w {
width: 8em;
height: 100%;
float: left;
border: 1px solid red;
line-height: 2em;
}
.input-w {
float: none; /* key */
width: auto; /* key */
height: 100%;
overflow: hidden; /* key */
border: 1px solid orange;
}
.input-w input {
width: 100%;
height: 100%;
}
You could use the CSS calc property to determine the width minus the borders and aside width:
input {
width: calc(100% - 102px); /* 100% minus (aside width (100px) + border width (2px)) */
box-sizing: border-box;
border: 1px solid #000;
}
FIDDLE
You could use display: table-*:
div {
display: table;
width: 100%;
background-color: #ccc;
}
aside {
display: table-cell;
width: 100px;
background: #eee;
}
span {
display: table-cell;
background: #bbb;
}
input {
display: block;
width: 100%;
background-color: #ddd;
}
http://jsfiddle.net/userdude/WcQ6J/5/
This is a little bit more compatible (and flexible) that display: inline-block, which is not supported in IE8.
You can set the width of the "aside" to pixels and the span to a percent, but, as you've seen, that will cause problems. It's easier to set both to a percent. Also, "inline-block" will put your elements in line. You can use this or "float: right;", but I prefer setting the display.
aside {
display: inline-block;
width: 9%;
}
span {
display: inline-block;
width: 90%;
}
See jsfiddle.
In case you want a truly variable width input field, so that you can manually adjust its width to fill the entire page, or to any other convenient width, why not make that input element fill 100 % of a resizable div?
CSS:
<style>
div.resize {
width: 300px; /*initial width*/
resize: horizontal;
overflow: auto;
}
HTML:
<div class="resize">
<input style="width: 100%" />
The little triangle to drag to resize the div will appear in the lower-right corner of the input element!