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 14. CSS: Layout – Display Property

inline, block and inline-block

inline, block and inline-block

Exploring inline, block, and inline-block in CSS

We have already explained inline elements and block elements in the HTML section earlier. In this lesson, we'll further elaborate on the topic including inline-block elements.

The table below summarizes the characteristics of each element type. We'll explain each aspect one by one.

Comparison of inline, block and inline-block elements

1. Default Width

The width of inline elements and inline-block elements is set based on the content length of each element while block elements stretch out from the left edge to the right edge of the parent element (or browser).

inline, block and inline-block elements - default width

2. Line Break

A block element always starts on a new line even though the width of the block element is less than the parent element's width. To put multiple block elements on the same line, you need to change the display property of the parent element (e.g., flex or grid).

On the other hand, there is no line break between inline and inline-block elements. When the total element width becomes larger than the width of the parent element, the elements go to the next line.

inline, block and inline-block elements - line break

3. width and height

You can set width and height for block elements and inline-block elements; however, you cannot set width and height for inline elements.

inline, block and inline-block elements - width and height

4. padding and margin

You can set padding and margin for block elements and inline-block elements; however, you can set only horizontal paddings and margins for inline elements. Technically, you can set vertical paddings for inline elements; however, the padding area will overlap with other elements.

inline, block and inline-block elements - padding and margin
inline element vertical paddings

5. text-align

You can use the text-align property for the parent element of inline elements and inline-block elements, but it won't work for block elements. If you want to make a horizontal alignment for block elements, use the margin: auto, which will be explained next.

inline, block and inline-block elements - text-align

6. margin: auto

As opposed to the text-align property, margin: auto works only for block elements.

inline, block and inline-block elements - margin: auto

7. Nesting Block Elements

Block elements and inline-block elements can nest other block or inline-block elements as child elements; however, if you try to nest block or inline-block elements under inline elements, the layout will be broken.

Nesting block elements

Practice 1

Objective:
Check the default width and line break behavior of inline, block, and inline-block elements

 

1. Create a new HTML file for this chapter

  • Create a copy of the chapter13.html file and change the name to chapter14.html.
  • Change the <title> section to 14. CSS: Layout – Key Concept.
  • Also, delete the existing content of the <body> element that was created in the previous chapter.
  • Add the <h1> tag to show the chapter title of this page at the top of the page.
  • The code should look like the one below.
chapter14.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!--Google Font-->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">
  <!--Custom CSS-->
  <link rel="stylesheet" href="css/practice.css">
  <title>14. CSS: Layout – Key Concept</title>
</head>
<body>
  <h1>Chapter 14. CSS: Layout – Key Concept</h1>
</body>
</html>

2. Update the body section

Add the code below in the <body> section of the HTML file. We are adding the three classes (inline-element, block-element, and inline-block-element) for each element type to see the results clearly.

chapter14.html
<h2>inline, block and inline-block</h2>
<h3>1. Default Width</h3>
<div class="inline-element">Inline </div>
<div class="block-element">Block</div>
<div class="inline-block-element">Inline-Block</div>

<h3>2. Line Break</h3>
<div class="inline-element">Inline 1</div>
<div class="inline-element">Inline 2</div>
<div class="inline-block-element">Inline-Block 1</div>
<div class="inline-block-element">Inline-Block 2</div>
<div class="block-element">Block 1</div>
<div class="block-element">Block 2</div>

3. Update the CSS file

Open the practice.css file and add new code for adding styles to the inline-element, block-element, and inline-block-element classes. As this is the start of the chapter, add a comment upfront for code readability.

practice.css
/* Chapter 14. CSS: Layout – Key Concept */
.inline-element{
  display: inline;
  border: 1px dotted #26BCCE;
  background-color: #23ACBB
}

.block-element{
  display: block;
  border:1px dotted #26BCCE;
  background-color: #B3EBEF;
}

.inline-block-element{
  display: inline-block;
  border: 1px dotted #26BCCE;
  background-color: #55D0DC
}

4. Check the result with a browser

  • Open chapter14.html with a browser.
  • You can see how different element types behave differently.

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

link

 

Practice 2

Objective:
Check the behaviors of width, height, padding, and margin for different element types

 

1. Update the body section of the HTML file

Add the code below in the <body> section of the chapter14.html file. We use the same classes as the ones in the previous practice. There is no need to set new classes.

chapter14.html
<h3>3. width and height</h3>
<div class="inline-element" style="width:150px; height:50px;">Inline 1</div>
<div class="inline-element" style="width:150px; height:50px;">Inline 2</div>
<div class="block-element" style="width:150px; height:50px;">Block 1</div>
<div class="block-element" style="width:150px; height:50px;">Block 2</div>
<div class="inline-block-element" style="width:150px; height:50px;">Inline-Block 1</div>
<div class="inline-block-element" style="width:150px; height:50px;">Inline-Block 2</div>

<h3>4. padding and margin</h3>
<h4>padding</h4>
<div class="inline-element" style="width:150px; height:50px;padding:10px;">Inline 1</div>
<div class="inline-element" style="width:150px; height:50px;padding:10px;">Inline 2</div>
<div class="block-element" style="width:150px; height:50px;padding:10px;">Block 1</div>
<div class="block-element" style="width:150px; height:50px;padding:10px;">Block 2</div>
<div class="inline-block-element" style="width:150px; height:50px;padding:10px;">Inline-Block 1</div>
<div class="inline-block-element" style="width:150px; height:50px;padding:10px;">Inline-Block 2</div>

<h4>margin</h4>
<div class="inline-element" style="width:150px; height:50px; margin:30px;">Inline 1</div>
<div class="inline-element" style="width:150px; height:50px; margin:30px;">Inline 2</div>
<div class="block-element" style="width:150px; height:50px; margin:10px;">Block 1</div>
<div class="block-element" style="width:150px; height:50px; margin:10px;">Block 2</div>
<div class="inline-block-element" style="width:150px; height:50px; margin:10px;">Inline-Block 1</div>
<div class="inline-block-element" style="width:150px; height:50px; margin:10px;">Inline-Block 2</div>

2. Check the result with a browser

  • Open chapter14.html with a browser.
  • You can see how different element types behave differently.

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

link

 

Practice 3

Objective:
Check the behaviors of text-align and margin: auto for different element types

 

1. Update the body section in the HTML file

Add the code below in the <body> section of the chapter14.html file. We are adding the container class to create a nesting structure.

chapter14.html
<h3>5. text-align</h3>
<h4>text-align: center</h4>
<div class="container" style="text-align: center;">
  <div class="inline-element">Inline</div>
</div>
<div class="container" style="text-align: center;">
  <div class="block-element" style="width:150px;">Block</div>
</div>
<div class="container" style="text-align: center;">
  <div class="inline-block-element" style="width:150px;">Inline-Block</div>
</div>

<h4>text-align: right</h4>
<div class="container" style="text-align: right;">
  <div class="inline-element">Inline</div>
</div>
<div class="container" style="text-align: right;">
  <div class="block-element" style="width:150px;">Block</div>
</div>
<div class="container" style="text-align: right;">
  <div class="inline-block-element" style="width:150px;">Inline-Block</div>
</div>

<h3>margin: auto</h3>
<h4>margin: auto</h4>
<div class="container">
  <div class="inline-element" style="margin: auto;">Inline</div>
</div>
<div class="container">
  <div class="block-element" style="width:150px; margin: auto;">Block</div>
</div>
<div class="container">
  <div class="inline-block-element" style="width:150px; margin: auto">Inline-Block</div>
</div>

<h4>margin-left: auto</h4>
<div class="container">
  <div class="inline-element" style="margin-left: auto;">Inline</div>
</div>
<div class="container">
  <div class="block-element" style="width:150px; margin-left: auto;">Block</div>
</div>
<div class="container">
  <div class="inline-block-element" style="width:150px; margin-left: auto">Inline-Block</div>
</div>

3. Update the CSS file

Open the practice.css file and add new code for adding styles to the container class.

practice.css
.container{
  border: solid #23ACBB;
  width: 300px;
  padding: 15px;
  margin: 10px;
}

4. Check the result with a browser

  • Open chapter14.html with a browser.
  • You can see how different element types behave differently.

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

link

 

Practice 4

Objective:
Check how you can nest child elements in different element types

 

1. Update the body section of the HTML file

Add the code below in the body section of the chapter14.html file. We use the same classes as the ones set before. There is no need to set new classes.

chapter14.html
<h3>Nesting</h3>
<h4>Inline Element</h4>
<div class="inline-element" style="width:300px; height:200px;">
  Inline Parent
  <div class="inline-element" style="width:150px; height:50px; margin:10px;">Inline Child</div>
  <div class="block-element" style="width:150px; height:50px; margin:10px;">Block Child</div>
  <div class="inline-block-element" style="width:150px; height:50px; margin:10px;">Inline-Block Child</div>
</div>
<br>
<h4>Block Element</h4>
<div class="block-element" style="width:300px; height:200px; padding:10px;">
  Block Parent<br>
  <div class="inline-element" style="width:150px; height:50px; margin:10px;">Inline Child</div>
  <div class="block-element" style="width:150px; height:50px; margin:10px;">Block Child</div>
  <div class="inline-block-element" style="width:150px; height:50px; margin:10px;">Inline-Block Child</div>
</div>
<br>
<h4>Inline-Block Element</h4>
<div class="inline-block-element" style="width:300px; height:200px; padding:10px">
  Inline-Block Parent<br>
  <div class="inline-element" style="width:150px; height:50px; margin:10px;">Inline Child</div>
  <div class="block-element" style="width:150px; height:50px; margin:10px;">Block Child</div>
  <div class="inline-block-element" style="width:150px; height:50px; margin:10px;">Inline-Block Child</div>
</div>
<hr>

2. Check the result with a browser

  • Open chapter14.html with a browser.
  • You can see that you cannot nest block and inline-block elements under the inline element.

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

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Simplifying Border Styling in CSS

Border (Multiple Properties)

Utilizing Select Boxes in HTML Forms

Select Box

Learn How to Create Forms in HTML

Create Forms

Embed Video in HTML Code with AI

Embed Video in HTML Code with AI

Simplifying Border Styling in CSS

Border (Multiple Properties)

Utilizing Select Boxes in HTML Forms

Select Box

Learn How to Create Forms in HTML

Create Forms

Embed Video in HTML Code with AI

Embed Video in HTML Code with AI

Tags:

Inline Element

Block Element

Alignment

Inline-block Element

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;