Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

HTML & CSS IntroductionChapter 10. CSS: Sizing and Spacing

margin

margin

Designing with Margins in CSS

Margin sets space between objects. Similarly to padding, margin is very important for design layout.

Length and keyword

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.

Length unit

  • px
  • em
  • rem
  • %
  • vw
  • vh

Keyword

  • auto
  • initial
  • inherit

Margins for block elements and inline elements

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.

Practice 1

Objective:
Test margins for block elements and inline elements

1. Update the body section of the HTML file

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.

chapter10.html
<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>

2. Check the result with a browser

  • Open chapter10.html with a browser.
  • You can see how each element is displayed. From this, you can understand:
    • The default margin is zero.
    • You can set a 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).

link

Margin overlap

margin vs. margin

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.

Margin overlap: margin vs. margin

Margins don't overlap paddings

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.

Margins don't overlap paddings

Practice 2

Objective:
Check margin overlaps

1. Update the body section of the HTML file

Add the code below to the chapter10.html file.

chapter10.html
<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>

2. Check the result with a browser

  • Open chapter10.html with a browser.
  • You can confirm two things:
    • In the first example, the gaps between all the elements stay the same as the elements' height (20px) although each element has 20px of margins. This means margins overlap.
    • In the second example, the child element's margin and the parent element's padding are both 20px, resulting in a 40px gap between the child's border and the parent's border. This means there is no overlap between margin and padding.
How margins are displayed in a browser

You can also check the sample result here (Demo Site).

Margins with multiple values

You can set different margin sizes for different sides of an element. The approach is the same as the one for padding.

1 value (all sides)

When you specify only one margin value, the value is applied to all sides of the element.

2 values (top and bottom vs. right and left)

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.

3 values (top vs. right and left vs. bottom)

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.

4 values (clockwise order from the top)

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).

Practice 3

Objective:
Test margins with multiple property values

1. Update the body section of the HTML file

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.

chapter10.html
<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>

2. Check the result with a browser

  • Open chapter10.html with a browser.
  • You can see that values are applied in different ways depending on how many values you set for margins.

You can also check the sample result here (Demo Site).

Note: Margin of <body>

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.

A default margin for the body element

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.

Eliminating left and right margin for the body element


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Completing Website Development from Start to Finish

Chapter 18. Completing Website Development

Enhancing Text with CSS text-decoration

text-decoration

What is HTML Document Structure and Why is it Important

HTML Document Structure

Choosing Font Colors in CSS

color

Completing Website Development from Start to Finish

Chapter 18. Completing Website Development

Enhancing Text with CSS text-decoration

text-decoration

What is HTML Document Structure and Why is it Important

HTML Document Structure

Choosing Font Colors in CSS

color

Tags:

Alignment

Margin

HTML & CSS Introduction
Course Content

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;