Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including margin for width and height

Tags:

css

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.

like image 283
sawa Avatar asked Jan 19 '13 17:01

sawa


People also ask

Is margin included in width?

The width and height properties include the content, padding, and border, but do not include the margin.

How do you add height margin in CSS?

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.

Is margin included in border-box?

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.

What is margin width?

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.


2 Answers

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.

like image 165
ExcellentSP Avatar answered Sep 24 '22 04:09

ExcellentSP


if your margin is for example 10px you can use

width: calc(100% - 20px); box-sizing: border-box; 

browser support

w3schools.com reference

like image 22
Sawyer Avatar answered Sep 26 '22 04:09

Sawyer