HTML page is a collection of HTML elements (ex: P, DIV, H1). Each element is considered as a box. Each box has four areas/layers.
Each element’s content, padding, border and margin properties can be adjusted. All the time, padding, border and margin properties are applied to each element implicitly or explicitly.
HTML elements are categorized as block-level elements and inline elements.
Block-level elements by default start on a new line and occupies full page width. Width and height properties are respected to block-level elements. Using display property, block-level elements can be converted to block-inline, inline, etc., elements, Few block-level elements are <div>, <p>, <pre>, <h1>,<h2>,<ul>,<ol>.
Inline elements by default occupies only required width based on content size. Width and height properties are not respected/applicable to inline elements. Using display property, inline elements can be converted to block, block-inline, etc., elements. Few inline elements are <a>, <span>, <img>, <i>.
Remaining padding and margin properties only affects inline elements.
Following example shows box model (on right side top) diagram for left side specified code.
In the above example,
Content is the first area in the box model. Content denotes text, image, etc., inside the HTML elements.
h1 {
background-color:lightgray;
}
<body>
<h1><img src="task-ok.png" /> Box Model</h1>
</body>
In the above example,
Padding is the second area around the content area. Padding is a space around the HTML element content. Padding can set all sides, or one or more sides of the element.
Padding property shorthand syntax
padding: <padding top value> || <padding right value> || <padding bottom value> || <padding left value>
Padding property longhand syntax
padding-<top || right || bottom || left>:<value>
h1 {
background-color:lightgray;
padding:20px;
}
In the above example,
Border is the third area around the padding area. Border property is used to style the border of the element. Border property sets the value of border-width, border-style and border-color.
Border property shorthand syntax
border: [<border width>] <border style - required> [<border color>]
System takes default values for border width and border color when no values specified.
Border property longhand syntax(core)
border-<width || style || color>:<value>
h1 {
background-color:lightgray;
padding:20px;
border:1px solid red;
}
In the above example,
Margin is the fourth area around the border area. Margin is a space around the HTML element border. Margin can set all sides, or one or more sides of the element.
Note:
1. There is no margin color property.
2. By default, when parent element has background color then the same color applies to child element margin also else no background color applies.
Margin property shorthand syntax
margin: <margin top value> || < margin right value> || < margin bottom value> || < margin left value>
Margin property longhand syntax
margin-<top || right || bottom || left>:<value>
h1 {
background-color:lightgray;
padding:20px;
border:1px solid red;
}
img {
margin-bottom:-8px;
}
In the above example,
When vertical margins of two elements touch, margin collapse happens (both margins are not applied/honored as specified). Details are explained below.
p {
height:80px;
margin-bottom:20px;
border:1px red solid;
background-color:#C3D69B;
width:200px;
}
div {
height:70px;
margin-top:30px;
border:1px red solid;
background-color:#F79646;
width:200px;
}
<body>
<p> Line one </p>
<div> Line Two </div>
</body>
In the above example, bottom margin 20px is applied to P element and top margin 30px is applied to DIV element. Since margin collapse happened, only margin 30px is applied in between P and DIV elements not 50px is applied.
Set margin property with value auto to block-level element helps to horizontally center the block-level element (not text inside the element).
p {
width:600px;
margin: 0 auto;
background-color:orange;
}
<body>
<p> Line one </p>
</body
In the above example,
Note:
CSS shorthand (ex: padding:20px) properties can be used instead of longhand version properties (ex: padding-top:20px, padding-right:20px, padding-bottom:20px, padding-left:20px).
/*CSS Shorthand version*/
div {
border:1px solid blue;
padding:20px;
}
/*CSS Longhand version*/
div {
border-width:1px;
border-style:solid;
border-color:blue;
padding-top:20px;
padding-right:20px;
padding-bottom:20px;
padding-left:20px;
}
1. Shorthand properties applicable order to the HTML element is Top, Right, Bottom and Left. If only one value is specified, then that value is applied to all four sides of the element.
p {
padding:20px;
}
In the above example, padding 20px is applied to all four sides of the element.
2. If two values are specified, then first value is applied to top and bottom sides of the element. Second value is applied to right and left sides of the element.
p {
padding:20px 10px;
}
In the above example, padding 20px is applied to Top and Bottom sides of the element. Padding 10px is applied to right and left sides of the element.
3. If three values are specified, then first value is applied to top side of the element. Second value is applied to right and left sides of the element. Third value is applied to bottom side of the element.
p {
padding:20px 10px 5px;
}
In the above example, padding 20px is applied to top side of the element. Padding 10px is applied to right and left sides of the element. Padding 5px is applied to bottom side of the element.
4. If four values are specified, then first value is applied to top side of the element. Second value is applied to right side of the element. Third value is applied to bottom side of the element. Fourth value is applied to left side of the element.
p {
padding:20px 10px 5px 2px;
}
In the above example, padding 20px is applied to top side of the element. Padding 10px is applied to right side of the element. Padding 5px is applied to bottom side of the element. Padding 2px is applied to left side of the element.
Apply padding/margin/border to one or more sides of the element is also possible. Every element has four sides, those are Top, Right, Bottom and Left.
<padding/border/margin>-<top/right/bottom/left>:<value>
h1 {
background-color:lightgray;
padding-bottom:10px;
border-bottom:1px solid red;
margin-bottom:5px;
padding-top:30px;
border-top:2px solid #0026ff;
margin-top:20px;
}
<h1>Box Model</h1>
In the above example, padding bottom(10px), border bottom (1px solid red) and margin bottom(5px) properties are applied to bottom side of the H1 element. Padding top(30px), border top(2px solid #0026ff) and margin top(20px) properties are applied to top side of the H1 element. No styles are applied to right and left side of the H1 element.
Element is considered as a box and wrapped with multiple areas/layers (padding, border and margin). Using below example, let us see how to calculate an element height and width.
p {
width:150px;
height:60px;
padding:40px;
border:15px solid green;
margin:20px;
}
Above P element total height and width are calculated following way.
Total height = 60px (content height) + 40px (padding left) + 40px (padding right) + 15px (border left) + 15px (border right) + 20px (margin left) + 20px (margin right) = 210px.
Total width = 150px (content height) + 40px (padding top) + 40px (padding bottom) + 15px (border top) + 15px (border bottom) + 20px (margin top) + 20px (margin bottom) = 300px.
Total height of the above P element is 210px.
Total width of the above P element is 300px.
p {
/*padding:10px;*/
border:1px solid red;
margin:20px;
}
p {
padding:10px;
/*border:1px solid red;*/
margin:20px;
}
p {
padding:10px;
border:1px solid red;
/*margin:20px;*/
}
p {
border:1px solid red;
/*width:150px;*/
}
p {
border:1px solid red;
width:150px;
height:60px;
}
span {
border:1px solid red;
width:150px; /*Not applicable*/
height:60px; /*Not applicable*/
}
<p>Block level element</p>
<span>Inline element</span>
div {
background-color:orange;
height:100px;
width:250px;
border:1px solid red;
margin:20px;
}
p {
height:50px;
width:100px;
border:1px solid green;
margin:20px;
}
<div>DIV element
<p>P element</p>
</div>
div {
background-color:orange;
height:50px;
width:250px;
border:1px solid red;
margin:30px;
}
<div>
DIV element
</div>
Note:Outside border is manually added to show the margin space area.
p {
padding-top:20px;
}
p {
margin-top:10px;
}
p {
border-color:red;
}
p {
border:1px solid green;
}
For Chrome, it starts with “-webkit-“
For IE, it starts with “-ms-“
For Firefox, it starts with “-moz-“
p {
border-radius:74px 74px 74px 74px;
-webkit-border-radius: 74px 74px 74px 74px;
-moz-border-radius:74px 74px 74px 74px;
-ms-overflow-style:scrollbar;
}
Example: Apply padding, border and margin to P element content.
<!DOCTYPE html/>
<html>
<head>
<title>Box Model</title>
<style>
p {
padding:40px;
border:2px solid green;
margin:100px;
width:50px;
float:left;
text-align:center;
}
</style>
</head>
<body >
<p>One</p>
<p>Two</p>
</body>
</html>
Result:
In the above example,
Using Chrome browser’s developer tool, one P element is explained.
Related topics:
In the above examples different CSS units are used. Please refer the following links to know better about CSS units.