The Box Model can be compared to a house on a lot with a fence around it.
Box Model Example Complete
LATER: Create Shipping Package and Arm Ananolgy (e.g., bone is teh element, muscle is the padding, skin (thin skin or thick skin) is the border, and space between another person is the margin.)
First, let's create a lot using property values for our house that we will create later and then take measurements of it.
<body> <div class="myLot"></div> </body>
<style>
.myLot{
height: 200px;
width: 200px;
background-color: #2B9E0F;
}
</style>
</head>
Now that we have picked out our lot, let's add a fence to "border" it in.
<style>
.myLot{
height: 200px;
width: 200px;
background-color: #2B9E0F;
border: 5px solid black;
}
</style>
Now that we have the lot measured, let's define the area where the house will be placed.
<style>
.myLot{
height: 200px;
width: 200px;
background-color:#2B9E0F;
border: 5px solid black;
padding: 50px;
}
</style>
Now that we have defined the lot, let's add a safe zone (easement) around the lot.
<style>
.myLot{
height: 200px;
width: 200px;
background-color:#2B9E0F;
border: 5px solid black;
padding: 50px;
margin:50px;
}
</style>
Now let's add the house itself.
<body>
<div class="myLot"><div class = "myHouse"></div></div>
</body>
<style>
.myLot{
height: 200px;
width: 200px;
background-color:#2B9E0F;
border: 5px solid black;
padding: 50px;
margin:50px;
}
.myHouse{
height: 200px;
width: 200px;
background-color:antiquewhite;
}
</style>
Now let's add another lot.
<body>
<div class="myLot"><div class = "myHouse"></div></div>
<div class="myLot"><div class = "myHouse"></div></div>
</body>
<style>
.myLot{
height: 200px;
width: 200px;
background-color:#2B9E0F;
border: 5px solid black;
padding: 50px;
margin:50px;
}
.myHouse{
height: 200px;
width: 200px;
background-color:antiquewhite;
}
.myHouse2{
margin-top:150px;
}
</style>
</head><body>
<div class="myLot"><div class = "myHouse"></div></div>
<div class="myLot myHouse2"><div class = "myHouse"></div></div>
</body>
NOTE: Notice the even though the first house has a margin of 50 pixels all around it and the second house has a top margin of 150 pixels, the total margin is not 200 pixels but the largest of the two margins. This is due to the fact that vertical margins collapse and the result is the largest of the two margins. See CSS User Manual: Box Model
There is a new modern box model called box-sizing: See CSS Tricks web site for details
The box-sizing property defines how the width and height of an element are calculated: should they include padding and borders, or not.Width and height only apply to the content of the element:
Width and height apply to all parts of the element: content, padding and borders:
The total width of this box is 300px regardless of the size of the padding and border because they are automatically substracted to keep the box at 300px