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

flex-grow

flex-grow

Maximizing Layout Flexibility with flex-grow

The flex-grow property is used for filling the flex container's remaining space with the flex items. The growing direction defined by this property is the main axis direction. We'll provide our explanation based on the default flex-direction setting.

The value set for each flex item becomes the ratio of each flex item's contribution to fill the remaining space. For example, if there is a remaining space of 150px, a portion of this 150px is distributed to each item based on the ratio set for each flex item. The default value 0 is applied to the flex item if you don't specify flex-grow for the flex item.

Original Status Example
flex-grow (base case: before the property is applied)

Case 1: Grow evenly

If you set flex-grow: 1 for all flex items, each item grows evenly to fill the remaining space.

flex-grow (case 1: grow evenly)

Case 2: Grow red and blue evenly

If you set flex-grow: 1 for the red and blue items while flex-grow: 0 for the green item, the red and blue items grow evenly to fill the remaining space.

flex-grow (case 2: grow red and blue evenly)

Case 3: Grow red and blue with a 1:2 ratio

If you set flex-grow: 0 for green, 1 for red, and 2 for blue, the red and blue items grow at a 1:2 ratio respectively.

flex-grow (case 3: grow red and blue with a 1:2 ratio)

Case 4: Grow blue only

If you set flex-grow: 1 for the blue item while flex-grow: 0 for the other two, only the blue item grows to fill the remaining space.

flex-grow (case 4: grow blue only)

Practice

Objective:
Check how the flex-grow property works

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 flex container and flex items. For the flex items, we are using different classes for each color: flex-item-to-grow-1, flex-item-to-grow-2, and flex-item-to-grow-3.

chapter15.html
<h2>flex-grow</h2>
<h3>Original Flex Item Size</h3>
<div class="flex-container-no-padding">
  <div class="flex-item-to-grow-1"></div>
  <div class="flex-item-to-grow-2"></div>
  <div class="flex-item-to-grow-3"></div>
</div>

<h3>Grow Evenly</h3>
<div class="flex-container-no-padding">
  <div class="flex-item-to-grow-1" style="flex-grow: 1;">flex-grow: 1</div>
  <div class="flex-item-to-grow-2" style="flex-grow: 1;">flex-grow: 1</div>
  <div class="flex-item-to-grow-3" style="flex-grow: 1;">flex-grow: 1</div>
</div>

<h3>Grow Red and Blue with the Same Ratio</h3>
<div class="flex-container-no-padding">
  <div class="flex-item-to-grow-1" style="flex-grow: 0;">flex-grow: 0</div>
  <div class="flex-item-to-grow-2" style="flex-grow: 1;">flex-grow: 1</div>
  <div class="flex-item-to-grow-3" style="flex-grow: 1;">flex-grow: 1</div>
</div>

<h3>Grow Red and Blue with 1:2 Ratio</h3>
<div class="flex-container-no-padding">
  <div class="flex-item-to-grow-1" style="flex-grow: 0;">flex-grow: 0</div>
  <div class="flex-item-to-grow-2" style="flex-grow: 1;">flex-grow: 1</div>
  <div class="flex-item-to-grow-3" style="flex-grow: 2;">flex-grow: 2</div>
</div>

<h3>Grow Blue Only</h3>
<div class="flex-container-no-padding">
  <div class="flex-item-to-grow-1" style="flex-grow: 0;">flex-grow: 0</div>
  <div class="flex-item-to-grow-2" style="flex-grow: 0;">flex-grow: 0</div>
  <div class="flex-item-to-grow-3" style="flex-grow: 1;">flex-grow: 1</div>
</div>
<hr>

2. Update the CSS file

Open the practice.css file and add new code for adding styles to the flex container and flex items. Set 50px in width for the three flex items while setting 300px for the flex container to create room for growth (remaining 150px).

practice.css
.flex-container-no-padding{
  border: solid #23ACBB;
  display: flex;
  height: 100px;
  width: 300px;
  margin:10px;
}

.flex-item-to-grow-1{
  padding: 5px;
  background-color: green;
  color: white;
  text-align: center;
  box-sizing: border-box;
  height: 100px;
  width: 50px;
}

.flex-item-to-grow-2{
  padding: 5px;
  background-color: red;
  color: white;
  text-align: center;
  box-sizing: border-box;
  height: 100px;
  width: 50px;
}

.flex-item-to-grow-3{
  padding: 5px;
  background-color: blue;
  color: white;
  text-align: center;
  box-sizing: border-box;
  height: 100px;
  width: 50px;
}

3. Check the result with a browser

  • Open chapter15.html with a browser.
  • You can see how the flex-grow property adjusts the size of each flex item.

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

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Figma Component Variants and Interactive Components

Figma Component Variants and Interactive Components

Learn the Basics of HTML

Chapter 3. HTML Basics

Coloring Borders in CSS

border-color

Reset CSS: Ensuring Consistency Across Browsers

Reset CSS

Figma Component Variants and Interactive Components

Figma Component Variants and Interactive Components

Learn the Basics of HTML

Chapter 3. HTML Basics

Coloring Borders in CSS

border-color

Reset CSS: Ensuring Consistency Across Browsers

Reset CSS

Tags:

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;