Padding overrides height of the element with box-sizing: border-box - html

In snippet below padding-top overrides the height and max-height properties of container:
I want this <div> to be 10px high, but its 100px because of padding-top
as far as I understand this should be solved by box-sizing: border-box but this doesn't help
w3schools - border-box: the width and height properties (and min/max properties)
includes content, padding and border, but not the margin
.padding-test {
background: linear-gradient(109deg, #3adffd, #00abfb);
outline: 1px solid #3b3c6d;
width: 100%;
padding-top: 100px;
max-height: 10px;
height: 10px;
box-sizing: border-box;
}
<div class='padding-test'></div>
Can someone explain why is this happening and how to fix this?
Same happens for width and padding-left
UPD: I faced this issue when tried to change max height for box sized by aspect-ratio approach. I solved initial issue by setting parent size, but I still want to understand how border-box works with the padding - does it shrinks only content? is this correct behavior? is there any solution for this exact situation - can I override padding somehow?

I had run into the same doubt. According to MDN:
border-box
The width and height properties include the content, padding, and border, but do not include the margin. Note that padding and border will be inside of the box. For example, .box {width: 350px; border: 10px solid black;} renders a box that is 350px wide, with the area for content being 330px wide. The content box can't be negative and is floored to 0, making it impossible to use border-box to make the element disappear.
so box-sizing: border-box doesn't mean you can set the "border box" directly, but only affects how "content box" is calculated, which cannot be negative.
And my solution is: avoid the paddings, use a height-holding div or ::before pseudo element with designated height instead. (may also need overflow: hidden.) For example:
.padding-test {
height: 10px;
box-sizing: border-box;
overflow: hidden;
}
.padding-test::before {
height: 100px;
content: '';
display: block;
}

Related

How to have a div as a box not span past/off the screen

I have the following block of code for the styling on a div supposed to be a box:
.newsBox{
width: 100%;
text-align: center;
border-style: solid;
border-width: medium;
padding-left: 10px;
padding-right: 10px;
border-radius: 25px;
}
I am using this code in a PWA, and so it is going to be used on a mobile screen.
Instead of my div just spanning the entire screen of my phone, it spans off the screen as well (and so I have to move the screen left and right to view the whole box), but I can't understand why when my width is set to 100%. I have tried everything I could find on stack overflow and other websites; I think it may just be my code that's wrong.
Any help is appreciated. Thanks
After setting the the width to 100% and adding extra padding, it will show annoying scrollbars. Try to add the box-sizing: border-box; property to your div and it will be fixed.
Without the box-sizing property, an element with padding will actually be bigger than its width and height. You should try the following:
.newsBox{
width: 100%;
text-align: center;
border-style: solid;
border-width: medium;
padding-left: 10px;
padding-right: 10px;
border-radius: 25px;
box-sizing: border-box;
}
You can read more about box sizing here.
Aditionally you must take into account the width of your newsBox's parent container. Setting newsBox's width to 100% means it will be full width within its parent.
The scroll probably appear because there is margin or padding property set up somewhere (browsers add them to html and body by default). You can turn it off with:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
box-sizing changes the way browser calculate the width of the element ( 100% - padding of the parent is used instead of 100%).
But it is hard to guess without an actual code.

box-sizing border-box does not work on max-width with padding [duplicate]

I’ve got a <div> with padding. I‘ve set it to height: 0, and given it overflow: hidden and box-sizing: border-box.
div {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
height: 0;
overflow: hidden;
padding: 40px;
background: red;
color: white;
}
<div>Hello!</div>
As I understand, this should make the <div> disappear.
However, it’s still visible (in Chrome 31 and Firefox 25 on my Mac). The height declaration doesn’t appear to be applying to the padding, despite the box-sizing declaration.
Is this expected behaviour? If so, why? The MDN page on box-sizing doesn’t seem to mention this issue.
Nor, as far as I can tell, does the spec — it reads to me like both width and height should include padding when box-sizing: border-box (or indeed padding-box) are set.
The exact definition of border-box is:
That is, any padding or border specified on the element is laid out and drawn inside this specified width and height. The content width and height are calculated by subtracting the border and padding widths of the respective sides from the specified ‘width’ and ‘height’ properties.
So you can modify the height and width properties, but padding and border never change.
As the content width and height cannot be negative ([CSS21], section 10.2), this computation is floored at 0.
Then, if height is 0, you can't make the padding be inside, because that implies the height will be negative.
The declaration of height: 0; is applied. If you leave it at height: auto;, you would see a 20px difference (the height of the line with "Hello!"), making it a total 100px high. With height set to zero, it's only 80px high: padding-top + padding-bottom = 80px
So the answer is: Yes, it's expected behavior.
You could set width and height to any value between 0 and 80px (if you have 40px of padding) and still get the same dimensions.
Update: As Hardy mentioned, using an additional wrapper div gets around this issue.
Demo
HTML:
<div class="div-1">
<div class="div-2">
Hello!
</div>
</div>
CSS:
.div-1 {
padding: 40px;
/* This is not visible! */
border: 1px solid tomato;
}
.div-2 {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
height: 0px;
width: 0px;
background: red;
color: white;
overflow: hidden;
}

Specific height and width, but also use padding/margin?

So I'm trying to create an A4 page. Let's just say the margin of the page is 50px for now. The whole document (A4) is 300x300 pixels in my example:
https://jsfiddle.net/pfs01ucw/
What I get is this:
What I want is something like this:
I simply want to set a fixed container's width and height, add some margin and make the wrapper inside fill the entire space. If I add padding: 50px to the #container DIV, the height and size will increase by 50px on all sides (basically making it 400x400 pixels instead).
How do I achieve this?
box-sizing: border-box;
The width and height properties include the content, the padding and border, but not the margin. This is the box model used by Internet Explorer when the document is in Quirks mode. Note that padding and border will be inside of the box e.g. .box {width: 350px; border: 10px solid black;} leads to a box rendered in the browser of width: 350px. The content box can't be negative and is floored to 0, making it impossible to use border-box to make the element disappear.
Here the dimension is calculated as, width = border + padding + width of the content, and height = border + padding + height of the content.
MDN - box-sizing - CSS
Fiddle
<div id="container">
<div id="wrapper">
</div>
</div>
<style>
#container {
border: 1px solid #000;
height: 300px;
padding: 50px;
width: 300px;
}
#wrapper {
border: 4px solid #000;
height: 100%;
}</style>

Padding not working as expected, CSS, HTML [duplicate]

This question already has answers here:
Why does CSS padding increase size of element?
(6 answers)
Closed 7 years ago.
I have a div that's 500px in width, and 500px in height. The max-width and max-height are also set to 500px. I'm trying to make the padding-left of the div to be 100px so I can move the word "Hello" 100px from the left without increasing the overall width of the div. When I set the padding-left to 100px, the overall width of my div increased to 600px, even though I set the max-width of the div to be only 500px. Is there a way for me to move the word "Hello" 100px from the left without having the width of the div being increased, or wrapping another element around the word "Hello"?
div {
border: 1px solid red;
width: 500px;
height: 500px;
max-width: 500px;
max-height: 500px;
padding-left: 100px;
}
<div>
Hello
</div>
You need to adjust your box model. Put this in your CSS:
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
Paul Irish explains how the box model works and why we have to define it this way.
Essentially, when we use padding it adds itself to the value of the inner width/height. By adjusting your box-sizing to border-box you're saying that if your box is 500px wide, and you apply padding-left: 100px, it will simulate the box being only 400px to compensate for the padding - to give a total of 500px.
Browser support is universal at this point, and it performs well (despite the use of the * selector, which has as much of an impact as use HTML tags like h1 and p in CSS).
UPDATE: Note that the CSS above affects all elements that use the box-model. Whenever I update one box-model on a stylesheet, I update them all to prevent overlooking this detail later in the game when I am attempting to maintain continuity.
You can try using box-sizing: border-box, so as to force the declared height to take into account your padding(s).
div {
border: 1px solid red;
width: 500px;
height: 500px;
max-width: 500px;
max-height: 500px;
padding-left: 100px;
box-sizing: border-box;
}
<div>
Hello
</div>

Input element with padding and percentage width exceeds parent div

I have this:
<div class="wrapper">
<input type="text"/>
</div>
div{
border: 1px solid red;
width: 300px;
}
input{
width: 100%;
padding: 10px;
}
I need the input to be 100% of the parent div, but also I need this input to have 10px padding. Results can be seen here: http://jsfiddle.net/pdJYF/
How can I achieve this?
Add box-sizing to your input field:
input{
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
padding: 10px;
}
Example Fiddle
Browser Compatibility
The underlying "problem" is the box model of HTML/CSS. As you can see in the illustration of the respective MDN article each element's box has 4 different areas: margin box, border box, padding box and content box.
When you assign measures (width or height) to the element, this is applied to one of these areas. If the area is, e.g., the content box, then for the total size of the element margin, border and padding is added. So you wont set the total dimensions of the box, but one of its contained boxes.
The CSS property box-sizing tells the browser, which box to use, when calculating the element's dimensions. The default value is content-box. So in the above example values for margin, border and padding get added and hence the element is too big. By setting the box model to border-box, only the margin gets added to the dimensions (which is zero here) and the elements fits.
Why you don't put the padding in percentage too:
input{
width: 94%;
padding: 0 3%;
border:0;
}
You must decrease 2x padding from left and right, and decrease 2x for any pixel you want use like this and insert your input's width.
For example: 20px (paddings left & right) + 2px (borders left & Right )= 22px
300px - 22px = 278px
div { border: 1px solid red; width:300px; }
input { width: 278px;padding: 10px; }