Margin sets space between objects. Similarly to padding, margin is very important for design layout.
You can specify a margin size using one of the length units or keywords. The default margin value is 0
. You can use the auto
keyword, which is useful for block element layout. We'll explain how to use auto
later.
px
em
rem
%
vw
vh
auto
initial
inherit
Like padding
, behaviors of margin
can differ by type of element. You can set margins for block elements horizontally and vertically; however, you cannot set vertical margins for inline elements.
Add the code below to the chapter10.html file. The <br>
tag between the <a>
element and the <span>
element is used for checking how margins for inline elements are applied vertically.
<h2>Margin</h2>
<div class="size-object" style="margin:initial">div element: Original</div>
<div class="size-object" style="margin:30px">div element: margin 30px</div>
<p class="size-object" style="margin:initial">p element: Original</p>
<p class="size-object" style="margin:30px">p element: margin 30px</p>
<a class="size-object" style="margin:initial">a element: Original</a>
<a class="size-object" style="margin:30px">a element: margin 30px</a>
<br>
<span class="size-object" style="margin:initial">span element: Original</span>
<span class="size-object" style="margin:30px">span element: margin 30px</span>
<hr>
margin
is zero.margin
for the block elements horizontally and vertically; however, vertical margins for the inline elements are not applied.You can also check the sample result here (Demo Site ).
When two elements with margins are placed side by side, their margins overlap. In this situation, a margin with a larger value will be applied to the margin between the two elements.
In the illustration below, element A has a right margin of 10px while element B has a left margin of 20px. In this case, the margin between the two becomes 20px.
One potential question about margins and paddings is if margins overlap paddings.
The answer is no. Even though a block element with a margin is nested under a parent block element with paddings, both settings are applied.
In the example below, the parent element has 10px of padding while the child element has 10px of margin. In this case, the distance between the parent element's borderline and the child element's borderline becomes 20px.
Add the code below to the chapter10.html file.
<h2>Margin: Overlap</h2>
<div class="size-object" style="height:30px; margin:30px">height:10px; margin:10px</div>
<div class="size-object" style="height:30px; margin:30px">height:10px; margin:10px</div>
<div class="size-object" style="height:30px; margin:30px">height:10px; margin:10px</div>
<hr>
<h2>Margin: Child Margin and Parent Padding</h2>
<div class="size-object" style="width:300px; padding:30px">
<div class="size-child-object" style="height:30px; margin:30px; border: 1px dotted">hight:30px; margin: 30px</div>
</div>
<hr>
You can also check the sample result here (Demo Site ).
You can set different margin sizes for different sides of an element. The approach is the same as the one for padding.
When you specify only one margin value, the value is applied to all sides of the element.
When you specify two margin values, the first value is applied to the top and bottom margins, while the second value is applied to the right and left margins.
When you specify three margin values, the first value is applied to the top margin, the second value is applied to the right and left margin, and the last value is applied to the bottom margin.
When you specify four margin values, the value is applied from the top in the clockwise order (the order of top, right, bottom, and left).
Add the code below to the chapter10.html file. We are using display: inline-block
to show the results clearly. We'll explain about the display
property later in this course. For now, just copy and paste the code.
<h2>Margin: multiple values</h2>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;">Before padding</div>
</div>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;margin: 10px;">Margin 10px</div>
</div>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;margin: 5px 10px;">Margin 5px 10px</div>
</div>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;margin: 5px 10px 1px;">Margin 5px 10px 1px</div>
</div>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;margin: 5px 10px 1px 2px;">Margin 5px 10px 1px 2px</div>
</div>
<hr>
You can also check the sample result here (Demo Site ).
Although the default margin value is usually zero, the body element has a pre-set margin depending on the browser. You can see that there is a space like in the image below.
Depending on your website design, you may not want to have a gap at the edge of the browser. In that case, you need to set margin:0
in the body element. You can stretch out elements from the left edge of the browser to the right edge as shown below.
Margin sets space between objects. Similarly to padding, margin is very important for design layout.
You can specify a margin size using one of the length units or keywords. The default margin value is 0
. You can use the auto
keyword, which is useful for block element layout. We'll explain how to use auto
later.
px
em
rem
%
vw
vh
auto
initial
inherit
Like padding
, behaviors of margin
can differ by type of element. You can set margins for block elements horizontally and vertically; however, you cannot set vertical margins for inline elements.
Add the code below to the chapter10.html file. The <br>
tag between the <a>
element and the <span>
element is used for checking how margins for inline elements are applied vertically.
<h2>Margin</h2>
<div class="size-object" style="margin:initial">div element: Original</div>
<div class="size-object" style="margin:30px">div element: margin 30px</div>
<p class="size-object" style="margin:initial">p element: Original</p>
<p class="size-object" style="margin:30px">p element: margin 30px</p>
<a class="size-object" style="margin:initial">a element: Original</a>
<a class="size-object" style="margin:30px">a element: margin 30px</a>
<br>
<span class="size-object" style="margin:initial">span element: Original</span>
<span class="size-object" style="margin:30px">span element: margin 30px</span>
<hr>
margin
is zero.margin
for the block elements horizontally and vertically; however, vertical margins for the inline elements are not applied.You can also check the sample result here (Demo Site ).
When two elements with margins are placed side by side, their margins overlap. In this situation, a margin with a larger value will be applied to the margin between the two elements.
In the illustration below, element A has a right margin of 10px while element B has a left margin of 20px. In this case, the margin between the two becomes 20px.
One potential question about margins and paddings is if margins overlap paddings.
The answer is no. Even though a block element with a margin is nested under a parent block element with paddings, both settings are applied.
In the example below, the parent element has 10px of padding while the child element has 10px of margin. In this case, the distance between the parent element's borderline and the child element's borderline becomes 20px.
Add the code below to the chapter10.html file.
<h2>Margin: Overlap</h2>
<div class="size-object" style="height:30px; margin:30px">height:10px; margin:10px</div>
<div class="size-object" style="height:30px; margin:30px">height:10px; margin:10px</div>
<div class="size-object" style="height:30px; margin:30px">height:10px; margin:10px</div>
<hr>
<h2>Margin: Child Margin and Parent Padding</h2>
<div class="size-object" style="width:300px; padding:30px">
<div class="size-child-object" style="height:30px; margin:30px; border: 1px dotted">hight:30px; margin: 30px</div>
</div>
<hr>
You can also check the sample result here (Demo Site ).
You can set different margin sizes for different sides of an element. The approach is the same as the one for padding.
When you specify only one margin value, the value is applied to all sides of the element.
When you specify two margin values, the first value is applied to the top and bottom margins, while the second value is applied to the right and left margins.
When you specify three margin values, the first value is applied to the top margin, the second value is applied to the right and left margin, and the last value is applied to the bottom margin.
When you specify four margin values, the value is applied from the top in the clockwise order (the order of top, right, bottom, and left).
Add the code below to the chapter10.html file. We are using display: inline-block
to show the results clearly. We'll explain about the display
property later in this course. For now, just copy and paste the code.
<h2>Margin: multiple values</h2>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;">Before padding</div>
</div>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;margin: 10px;">Margin 10px</div>
</div>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;margin: 5px 10px;">Margin 5px 10px</div>
</div>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;margin: 5px 10px 1px;">Margin 5px 10px 1px</div>
</div>
<div class="size-object" style="display:inline-block">
<div class="size-child-object" style="width:200px;margin: 5px 10px 1px 2px;">Margin 5px 10px 1px 2px</div>
</div>
<hr>
You can also check the sample result here (Demo Site ).
Although the default margin value is usually zero, the body element has a pre-set margin depending on the browser. You can see that there is a space like in the image below.
Depending on your website design, you may not want to have a gap at the edge of the browser. In that case, you need to set margin:0
in the body element. You can stretch out elements from the left edge of the browser to the right edge as shown below.