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
Related
I want to make 3/4 input fields next to each other, with a margin in between. However, using a margin makes the last inputfield too big/too short(using calc).
Been trying to figure out how to do this but can't seem to find a solution
How my current CSS code looks like:
input {
background-color: darkgreen;
border: none;
color: white;
padding: 5px;
border-radius: 8px;
width: calc(33% - 20px);
box-sizing: border-box;
margin-right: 20px;
}
Background has a 75% width and padding
Current result
Wanted result
Hope this work but it will work for one row only
input {
background-color: darkgreen;
border: none;
color: white;
flot:left:
padding: 5px;
border-radius: 8px;
width: calc(33.333333% - 20px);
box-sizing: border-box;
}
input + input{
margin-left: 30px;
}
If you want to use for multiple rows then you can use below the structure.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
:after,
:before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.form-row {
margin-right: -15px;
margin-left: -15px;
}
.form-row:before,
.form-row:after {
display: table;
content: " ";
}
.input-box {
position: relative;
min-height: 1px;
padding-right: 15px;
padding-left: 15px;
width: 33.3333333%;
float: left;
}
.input-field {
display: block;
width: 100%;
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: white;
background-color: darkgreen;
border: none;
border-radius: 8px;
}
<div class="form-row">
<div class="input-box">
<input type="text" class="input-field" value="input 01">
</div>
<div class="input-box">
<input type="text" class="input-field" value="input 02">
</div>
<div class="input-box">
<input type="text" class="input-field" value="input 03">
</div>
</div>
The problem is that all three of your elements have the full margin-right applied to them. If you want your final <input> element to stretch to the edge of the container, you'll want to only apply the margin-right to the first two <input> elements.
The best way to do this would be to combine the :not and :last-of-type pseudo-classes, as can be seen in the following:
input {
background-color: darkgreen;
border: none;
color: white;
padding: 5px;
border-radius: 8px;
width: calc(33% - 20px);
box-sizing: border-box;
}
input:not(:last-of-type) {
margin-right: 20px;
}
<input />
<input />
<input />
Note that this will have the added effect of making all three <input> elements slightly wider, as their width calculation is now derived from a larger container.
I am trying to create a custom div with input text and two buttons inside it as shown below.
But when i resize the screen it becomes like this
Is there a way to avoid the two buttons to come down ? Instead it should remain inside the original div.
Here's the code i tried:
.searchBar {
background: #DDDDDD;
width:100%;
height:50px;
padding: 10px;
position: relative;
}
.search_field {
display: inline-block;
border-radius:4px ;
background: #FFFFFF;
width: 70%;height: 32px;
position: relative;
left: 60px;
overflow: inherit;
}
.search_field input {
width: 89%;
padding: 0;
border-top-left-radius: 4px;
padding-left: 10px;
padding-top: 5px;
padding-bottom: 5px;
border:1px inset red;
}
.search_field input:focus {
outline-color: transparent;
outline-style: none;
}
.search_field button {
border: none;
background: none;
}
<div id="searchBar" class="searchBar">
<div class="search_field">
<input type="text" id="searchInput" placeholder="Search" oninput="showSearchButtons()"/>
<button id="btn1" hidden="true" onclick="alert()"><img src="assets/images/search.png"></button>
<button id="btn2" hidden="true" onclick="alert()"><img src="assets/images/saveBtn.png"></button>
</div>
</div>
Any help is appreciated. Thanks
You can use calc to calculate the width of your input element relative to your buttons:
width: calc(100% - 100px);
Just make sure the width of your buttons is taken of the 100%. In SASS it could look like this:
$buttons: 50px;
width: calc(100% - #{$buttons * 2});
Below is a simplified implementation. I still have the % values as a fallback for older browsers - but that's more a habit than necessity as every major browser supports calc, even IE9 and onward.
input, button {
float: left;
height: 50px;
display: block;
box-sizing: border-box;
margin: 0;
padding: 0;
border: none;
}
input {
width: 70%;
width: calc(100% - 100px);
padding: 10px;
}
button {
/* Note that this width is equal to 100%
/* minus the percentage width of the input
/* divided by the amount of buttons. */
width: 15%;
width: 50px;
line-height: 50px;
}
/* This rule is just to make sure your images don't decide the buttons width */
button img {
max-width: 100%;
display: inline-block;
}
<input type='text' placeholder='search' />
<button><img src="http://placehold.it/50x50" /></button>
<button><img src="http://placehold.it/50x50" /></button>
Please try this instead of your styles:
.searchBar{
background: #DDDDDD;
width:100%;
height:50px;
padding: 10px;
position: relative;
}
.search_field {
border-radius:4px ;
background: #FFFFFF;
position: relative;
padding-right: 100px; /* You can change as button width */
}
.search_field input {
width: 100%;
padding: 0;
padding-left: 10px;
padding-top: 5px;
padding-bottom: 5px;
border: solid 1px #FF0000;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box;
}
.search_field button {
border: none;
background: none;
position: absolute;
top: 0;
}
.search_field button#btn1 {
right: 50px; /* Change as your requirement */
}
.search_field button#btn2 {
right: 0; /* Change as your requirement */
}
I am trying to make a validation box after an input field, however, across different browsers the sizing goes wrong e.g. there is a big difference between firefox and chrome.
Is there a better way to make this box so that the sizing is equal across all browsers? Here is the code and a jsfiddle of how I am doing it at the moment : http://jsfiddle.net/wPS7t/
And here is an image of the problem:
HTML
<form id="formStyles">
<div id="inputWrapper">
<input type="text"/>
<label id="xlabel">x</label>
</div>
</form>
CSS
body{
margin: 0;
padding: 0;
height: 100%;
width: 100%;
font-family: Calibri;
}
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
#formStyles{
width: 800px;
margin: 0 auto;
top: 200px;
position: relative;
}
input{
font-size: 18px;
padding: 5px;
}
#inputWrapper{
position: relative;
margin-left:auto;
margin-right:auto;
width:400px;
}
#xlabel{
background-color: red;
padding: 7.2px;
color: white;
left: -6px;
position: relative;
top:-1px;
}
Maybe try padding 7px? I'm not positive but I don't think both browsers except 7.2px as a result and probably is rounding it.
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!
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.