<button> padding / width problem - css

I'm using <button> to make a post request in a form. I also styled a a.button exactly like the <button> (I need the a.button to make some JS stuff).
The button has some padding, a fixed height. When I do specify the width of the button / a they both look the same. But when I add width to the <button> it ignores the padding.
I'm having this problem in Chrome, Firefox and Opera, so I guess it's not a rendering fault. Also same issue with <input type="submit" />
Here is the basic CSS:
.button, button {
margin: 0;
display: inline-block;
border: 0;
text-decoration: none;
cursor: pointer;
color: black;
background: #ddd;
font: 12px Verdana;
padding: 40px; /* 40px, so you can see that it won't be rendered with width */
text-align: center;
}
The HTML:
Some text
<button>Some text</button>
<!-- Works fine till here -->
<br /><br />
Some text
<button style="width:200px">Some text</button>
Fiddle: http://jsfiddle.net/9dtnz/
Any suggestions why the browsers are ignoring the padding? (top and bottom when using height / left and right when using width).

Very weird, I've seen my Chrome has a box-sizing: border-box; rule for input elements, so padding is included in width...
So to avoid that just specify box-sizing: content-box; (some prefix can be necessary).

It looks fine to me, so it might be a style sheet conflict issue. Try using !important to override whatever it may be and that could solve your problem.
.button, button {
margin: 0;
display: inline-block;
border: 0;
text-decoration: none;
cursor: pointer;
color: black;
background: #ddd;
font: 12px Verdana;
padding: 40px!important; /* 40px, so you can see that it won't be rendered with width */
text-align: center;
}
Hope this helps.
Michael.

Related

placeholder fully not visible for input type number on firefox when using certain font family

input[type="number"] field does not showing the placeholder text in latest firefox for certain font family like font-family: 'Open Sans';. The current version is Firefox Quantum 57.0.2 (64-bit)
Still don't know some font family have no such issue
Please check on the demo link https://codepen.io/anon/pen/zpqzEB
body {
padding: 2rem;
}
input {
display: block;
box-sizing: border-box;
width: 100%;
height: 40px;
padding: 0 10px;
line-height: 40px;
background: #fafafa;
border: 1px solid tomato;
padding: .5rem 1rem;
font-family: 'Open Sans';
background-clip: content-box;
background-color: deepskyblue;
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<input type="text" placeholder="Text field" />
<hr>
<input type="number" placeholder="Number field" />
It's your border-box property.
I'm not sure why it's happening on CodePen because it looks fine on JSBin and here on StackOverflow in the snippet you posted. Maybe it's CodePen not working great with Quantum.
Anyway, here's what's happening:
The border-box property will make it so the padding and border are included in total width and height of the element (w3schools.com).
Your input height is set to 40px. That 40px has to include the border, padding-top, padding-bottom, and the height of the element itself. Your line-height is also set to 40px. Your padding styles (you have two rules set, so it picks up the second one) is padding: .5rem 1rem;.
There isn't enough room for the input text in these 40 allocated pixels with.
Issue: 40px line height + top padding + bottom padding > 40px
As for a fix, I'm assuming you want to keep your padding and have your inputs the same size. You may need to make your font size smaller or make your inputs larger. 40px isn't enough for the padding and a 40px line height. Or you can remove that border-box property.
It kinda looks to me that the difference between this property on Chrome and on Firefox is that Chrome is ignoring the line-height. In this screenshot on Chrome, the height of the input is 22px, even though your line-height is 22px.
The root cause of the issue is that font-size is in excess of the number input controls.
Your example can be fixed preciesely by using:
padding: 0.46rem 1rem;
EXPLAINED
When padding is applied to a number type input the padding is applied to the boundaries of the box in the normal way however in some browsers cropping occurs relative to the amount of padding applied.
The cropping effect is calculated from the inside boundaries of the input arrow controls.
The cropping only affects placeholders because they are behind the input layer and become hidden when the interior boundaries of the input field are moved to cover it.
https://codepen.io/anon/pen/qpZPKd
There are various ways to avoid this however my recommendation is to avoid padding on input elements and to use alternate methods to create the desired effect.
Be an illusionist
Personally I don't see any reason to use vertical padding inside input fields. Line-height does a better job.
If you can't make the browsers do what you want make the user think the browser is doing what you want!
body {
padding: 2rem;
}
.Wrap{
box-sizing: border-box;
height: 40px;
border: 1px solid tomato;
padding: .5rem 1rem;
}
.Wrap input {
width: 100%;
height: 100%;
line-height: 40px;
border: none;
font-family: 'Open Sans';
background-color: deepskyblue;
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<div class="Wrap"><input type="text" placeholder="Text field" /></div>
<hr>
<div class="Wrap"><input type="number" placeholder="Number field" /></div>
Codepen example
https://codepen.io/anon/pen/QaNOdo
For some reason, when I change to height of your input field to anything above 40px it seems to work. Try this:
input {
display: block;
box-sizing: border-box;
width: 100%;
height: 41px;
padding: 0 10px;
line-height: 40px;
background: #fafafa;
border: 1px solid tomato;
padding: .5rem 1rem;
font-family: 'Open Sans';
background-clip:content-box;
background-color: deepskyblue;
}
I can't see why this fixes it though.
EDIT
You use two times PADDING in your css input declaration...
Just remove the first one : padding: 0 10px;
And keep : padding: .5rem 1rem;
body{
padding: 2rem;
}
input {
display: block;
box-sizing: border-box;
width: 100%;
height: 40px;
line-height: 40px;
background: #fafafa;
border: 1px solid tomato;
padding: .5rem 1rem;
font-family: 'Open Sans';
background-clip:content-box;
background-color: deepskyblue;
}
input[type="number"] {
// line-height: 1.5;
}
<input type="text" placeholder="Text field" />
<hr>
<input type="number" placeholder="Number field" />

:after pseudo element not working in Safari

I have a styled sentence with dynamic text. Sometimes the text is too long, and pushes the anchor at the end outside its box. I want the text to wrap after the span.price-difference, but the anchor button to be positioned against the right side of the p.
I added an :after pseudo element to .price-difference. I've got it set content: '' and display: block. Works in FF, Chrome, IE (including IE8, which I have to support), but not Safari.
There is an easy answer, wrapping the text following .price-difference with another span, and set it to block, but changing the HTML is a hassle, requiring a backend developer to make changes to a JSP file, and I'm hoping to avoid that. Looking for a CSS only solution, if it exists.
<p class="upsell"> Upgrade To
<span class="stateroom-upgrade"> Concierge Class </span>
for an additional
<span class="price-difference">$7.14 USD </span>
per person per day
<span>View Upgrades</span>
</p>
The CSS
.upsell {
background: none repeat scroll 0px 0px #FAFAFA;
border-top: 2px dashed #E8E8E8;
color: #666;
display: block;
font-size: 11.5px;
font-weight: 600;
margin: auto 19px 5px;
padding: 8px 0px 8px 8px;
position: relative;
text-transform: none;
white-space: nowrap;
width: 560px;
}
.upsell .price-difference {
color: #0C82C4;
font-size: 15px;
font-weight: 700;
margin-left: 5px;
display: inline-block;
}
.stateroom .upsell .price-difference::after {
content: "";
display: block;
}
.upsell .ccButtonNew {
position: absolute;
right: 10px;
top: 17px;
}
The p element has white-space: nowrap set on it, but when I turn it off, the problem doesn't go away.
I think it's related to the following link, but my situation isn't the same. In that question, the asker put a block level element div inside a p, which only takes inline elements. I have an inline element, span, inside the p. This should work.
:after pseudo-element working in FF, but not Safari or Chrome
.stateroom .upsell .price-difference:after {
content: "";
display: block;
position: absolute;
width: 30px;
border-top: 1px solid #000; /* placeholder border */
}
try adding a position to the after's css rules. i had a similar situation in which the after pseudo wouldn't display in old versions of safari but worked properly in all other browsers. I fixed adding a position rule to the css.
Hope this hepls.

Equal Height Responsive Search Form

I need an inline equal height search form that looks the same in all browsers.
http://codepen.io/mdmoura/pen/BcIFm
At the moment I have the following HTML markup:
<form method="get" action="#">
<input type="search"/><input type="submit" value="Search"/>
</form>
And the following CSS (LESS):
form {
box-sizing: border-box;
white-space: nowrap;
padding: 20px 116px 20px 0;
border: 1px solid red;
width: 40%;
input {
display: inline-block;
box-sizing: border-box;
&[type="search"] {
background-color: aqua;
border: 1px solid aqua;
font-size: 24px;
padding: 8px;
width: 100%;
}
&[type="submit"] {
background-color: #404040;
border: 1px solid #404040;
color: white;
font-size: 24px;
padding: 8px;
width: 100px;
}
&:-moz-focus-inner {
border: 0;
padding: 0;
}
} // input
} // form
THE PROBLEMS:
FIREFOX: The button seems to have one extra pixel on top and bottom;
IE 9, IE10: There is a padding on the right. No idea why ...
CHROME: The same as in IE 10 - There is a padding on the right.
SAFARI: Even worse. The Submit Input is higher then the Search Input.
And there is a right padding just as in IE 10 and Chrome.
All solved when using Normalize.css
Using overflow: hidden on the form also seems to better align the borders.
Could someone, please, help me out in improving this form and solving the problems?
Thank You,
Miguel
The padding issues you are having all stem from the way you have constructed the form div with a large right padding.
One idea would be to get rid of that right padding, and change the 'search' and 'submit' inputs so their widths equal 100% instead of both being 100%. If they were 80% and 20% for example you shouldnt need the large right padding on form. Then you can just set a margin around them or something to add space between them and the red line.

In-Field Labels Don't Line Up With Input Text

I'm using Viget's In-Field Labels plugin on a form, but even though the label and input have the same font size, same line height, same height, same padding, same everything, the input and label text does not line up. The input text is one pixel lower than the label text.
HTML:
<div class="fieldgroup">
<label for="name">Name</label>
<input type="text" id="name" name="name">
</div><!--/.fieldgroup-->
CSS:
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
font-family: Arial;
}
.fieldgroup {
position: relative;
}
input[type='text'],
label {
padding: 5px;
font-size: 16px;
line-height: 16px;
margin: 0;
height: 30px;
color: #fff;
display: block;
}
input[type='text'] {
border: none;
background: green;
}
The plugin sets the label's position to absolute and the top and left properties to 0.
Here's a fiddle.
What am I missing here?
It's possible to do this using almost 100% CSS and avoid all the layout flow issues caused by position: absolute;
jsFiddle demo
The trick is to wrap the LABEL around the text and INPUT element. Put the text inside of an element that can be given display: inline-block; margin-right: -100%;. This puts the text directly under the INPUT, which is given a transparent background so the text is visible through the INPUT.
Some Javascript is necessary to make the INPUT background opaque when the field has user input. There's no way around this at the moment… CSS can't "detect" a non-empty INPUT element.
http://jsfiddle.net/d8Apy/12/
Just size your labels & input via line-height. so text-overlap each others since they'll be sitting on same line-height
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
font-family: Arial;
}
.fieldgroup {
position: relative;
}
input[type='text'],
label {
padding: 5px;
margin: 0;
line-height:16px;
Font-size:16px;
color: #fff;
display: block;border:1px solid transparent
}
input[type='text'] {
border: none;
background: green;
}
I fixed the issue by tweaking one of the CSS rules:
input[type='text'], label {
padding: 5px;
margin: 0;
font-size: 1em;
line-height: 30px;
height: 30px;
color: #fff;
display: block;
}
The trick is to set the container height to be the same as the line-height.
You can see the effect at: http://jsfiddle.net/audetwebdesign/d8Apy/7/
Why this works???
My guess is that browsers compute the height of the anonymous line boxes differently between input and label elements. At least in FireFox, setting the line-height seems to do the trick.
I don't recall ever reading anything in the CSS3 spec related to these details.
Note - Cross-Browser Effects
I tested this in a few browsers and I discovered that if you set both height and line-height and use box-sizing, you get cross-browser issues. However, if you use line-height and box-sizing, the results are consistent. I have not tested with borders.

CSS input field styling magic

I am trying to style an input field and some buttons in a consistent way, but there seems to be some magic going on. Event though the input has the exact same classes as the buttons it is slightly higher. Also the placeholder text is vertically aligned differently the the inputted text (one pixel higher). Can this be solved with a cross-browser solution?
Edit: As pointed out by Jan Turoň the line height fixed the problem in Webkit. Now, if you check the codepen in Firefox you'll notice that the element still has a 1px border. How to get rid of that?
Thx, PS
Codepen
HTML
<form action="">
Button
<input type="text" class="btn" placeholder="input"/>
<button class="btn">Login</button>
Button
</form>
CSS
.btn, input, button {
display: inline-block;
vertical-align: middle;
line-height: 15px;
font-size: 15px;
font-family: sans-serif;
border: 0;
padding: 0;
margin: 0;
cursor: pointer;
cursor: hand;
text-decoration: none;
color: #f2f2f2;
background-color: #1a1a1a;
padding: 7px 12px 8px 12px;
margin-right: 1px;
margin-bottom: 1px;
}
The line-height doesn't shrink the input height below font-size plus some pixels see the MDN info:
On replaced inline elements, like buttons or other input element, line-height has no effect.
Just remove the line-height and you should get what you want even without applying heightstyle. Setting line-height to 130% also does seem to work.
I am trying to solve this for days and every solution is not whole, and not working in different browsers.
I find that this code do the work:
float: left;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
height: 33px;
I have updated Codepen - http://codepen.io/anon/pen/pvqRwE

Resources