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 15. CSS: Layout – Flex Box

Nested Flex Box

Nested Flex Box

How to Use Nested Flex Box for Multi-layer Layouts

A Flex Box can be nested by another Flex Box, and you can create a multi-layer nesting structure using Flex Box. This is a very helpful feature in website layout design.

Case 1: 3-layer nesting structure

You can create a nested Flex Box by simply nesting a Flex Box under another Flex Box. If you don't specify flex-direction, the flex containers are horizontally separated like in the illustration below. To set the widths of nested containers, it is better to use flex-basis as it gives you the flexibility to change flex-direction later on. In the illustration, flex-basis for the nested containers is all 50%.

Nested Flex Box (Case 1: 3 Layer Nesting Structure)

IdeaContent position alignment

One of the major benefits of using Flex Box is to make content positioning easier. For example, if you want to put content (e.g., text) in the center of the 3rd layer container, you can set justify-content: center and align-items: center for the 3rd layer container element.

Nested Flex Box (Content Position)

Case 2: 3-layer nesting structure with multiple flex directions

By adding a flex-direction: column in the first case, you can change the layout of the 3rd layer containers like in the illustration below. In this case, you don't need to change the flex-basis value. In the first case, it worked as the width property. In this case, it works as the height property by changing the flex-direction property setting. This is the key benefit of using flex-basis instead of using width or height.

Nested Flex Box (Case 2: 3 Layer Nesting Structure with Multiple Flex Directions)

Case 3: 4-layer nesting structure

You can further add another layer to create a 4-layer nesting structure.

Nested Flex Box (Case 3: 4 Layer Nesting Structure)

Practice

Objective:
Practice creating various nested Flex Boxes

1. Update the body section of the HTML file

Add the code below in the <body> section of the chapter15.html file. We are adding new classes for the main container and nested containers. We are using the same basic properties for nested containers while customizing backgrounds, borders, content positions, and flex-direction directly in the HTML code using the style attribute.

chapter15.html
<h2>Nested Flex Box</h2>
<h3>Layout 1</h3>
<div class="flex-container-main">
  <div class="flex-container-nested" style="border: solid 2px green; background-color: lightcyan;">
    <div class="flex-container-nested" style="border: solid 2px orange; background-color: lightyellow;"></div>
    <div class="flex-container-nested" style="border: solid 2px orange; background-color: lightyellow;"></div>
  </div>
  <div class="flex-container-nested" style="border: solid 2px green; background-color: lightcyan"></div>
</div>

<h3>Layout 1 with Content in the Center</h3>
<div class="flex-container-main">
  <div class="flex-container-nested" style="border: solid 2px green; background-color: lightcyan;">
    <div class="flex-container-nested" style="border: solid 2px orange; background-color: lightyellow;align-items: center;justify-content: center;">Content</div>
    <div class="flex-container-nested" style="border: solid 2px orange; background-color: lightyellow;align-items: center;justify-content: center;">Content</div>
  </div>
  <div class="flex-container-nested" style="border: solid 2px green; background-color: lightcyan"></div>
</div>

<h3>Layout 2</h3>
<div class="flex-container-main">
  <div class="flex-container-nested" style="flex-direction: column; border: solid 2px green; background-color: lightcyan;">
    <div class="flex-container-nested" style="border: solid 2px orange; background-color: lightyellow;"></div>
    <div class="flex-container-nested" style="border: solid 2px orange; background-color: lightyellow;"></div>
  </div>
  <div class="flex-container-nested" style="border: solid 2px green; background-color: lightcyan"></div>
</div>

<h3>Layout 3</h3>
<div class="flex-container-main">
  <div class="flex-container-nested" style="flex-direction: column; border: solid 2px green; background-color: lightcyan;">
    <div class="flex-container-nested" style="border: solid 2px orange; background-color: lightyellow;">
      <div class="flex-container-nested" style="border: solid 2px lightcoral; background-color: pink;"></div>
      <div class="flex-container-nested" style="border: solid 2px lightcoral; background-color: pink;"></div>
    </div>
    <div class="flex-container-nested" style="border: solid 2px orange; background-color: lightyellow;"></div>
  </div>
  <div class="flex-container-nested" style="border: solid 2px green; background-color: lightcyan"></div>
</div>

2. Update the CSS file

Open the practice.css file and add new code for adding styles to the flex containers.

practice.css
.flex-container-main{
  display: flex;
  border: solid blue 2px;
  background-color: lightblue;
  padding: 3px;
  margin: 10px;
  width: 300px;
  height: 150px;
}

.flex-container-nested{
  display: flex;
  margin: 5px;
  flex-basis: 50%;
}

3. Check the result with a browser

  • Open chapter15.html with a browser.
  • You can see different types of nested Flex Boxes are created.

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

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

How CSS Inheritance Works

Inheritance

Practice Mockup Design in Figma

Practice Mockup Design in Figma

Designing User Flows and Wireframe Layouts

Designing User Flows and Wireframe Layouts

Understanding Absolute vs. Relative Paths

Absolute Path vs. Relative Path

How CSS Inheritance Works

Inheritance

Practice Mockup Design in Figma

Practice Mockup Design in Figma

Designing User Flows and Wireframe Layouts

Designing User Flows and Wireframe Layouts

Understanding Absolute vs. Relative Paths

Absolute Path vs. Relative Path

Tags:

Nesting

Layout

Alignment

Display Property

Flex Box

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;