I want box width and height to include all of the content, padding, border width, and margin by default. Is there a way to do this? Especially, I want to be able to specify something like width: 100%
including everything up to the margin.
The width and height properties include the content, padding, and border, but do not include the margin.
Set the CSS box-sizing property to border-box to make width and height include the content, border, and padding. This allows you to set width: 100% and still add padding or border . In other words box-sizing: border-box makes width include everything between the inner edges of the margin.
border-box: The width and height properties include the padding and border, but not the margin. This is the box model used by IE when the document is in Quirks mode.
The default margins for Microsoft Word from version 2007 onward have been 1 inch (25.4 mm) all around; in Word 2003, the default top and bottom margins were 1 inch (25.4 mm), but 1.25 inches (31.7 mm) were given at the left and the right.
This is a vague question, but I'll answer the best I can.
Margin, by definition, is the area around the outside of your box; meaning there's no way to include margins inside of your div.
If you would like more padding on the inside of your box, but you don't want the box to resize, then use: box-sizing:content-box;
Or if you would like to include padding and the border, use: box-sizing:border-box;
A workable solution that preserves the size of your divs and removes overflow would look something like this:
#parent{ box-sizing:border-box; width:100%; height:200px; padding:2em; } #child{ box-sizing:border-box; width:100%; height:100%; padding:1em; }
<div id="parent"> <div id="child"></div> </div>
Just place the div you want to give margins to inside of another div that has padding. Thus creating faux margins.
if your margin is for example 10px you can use
width: calc(100% - 20px); box-sizing: border-box;
browser support
w3schools.com reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With