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-shrink

flex-shrink

Ensuring Layout Consistency with flex-shrink

The flex-shrink property is used for shrinking the flex items to align with the flex container size when the combined size of flex items exceeds the flex container size. The shrinking 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 in each flex item becomes the ratio of each flex item's contribution to shrink. For example, if there is an overflow part of 60px, this 60px is distributed to each item based on the ratio set for each flex item.

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

Case 1: Shrink evenly

If you set flex-shrink: 1 for all flex items, each item shrinks evenly to align with the flex container size.

flex-shrink (case 1: shrink evenly)

Case 2: Shrink red and blue evenly

If you set flex-shrink: 1 for the red and blue items while flex-shrink: 0 for the green item, the red and blue items shrink evenly.

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

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

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

flex-shrink (case 3: shrink 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 shrinks to align with the flex container size.

flex-shrink (case 4: shrink blue only)

IdeaDefault value and exception for flex-shrink

As the default value of the flex-shrink is 1, the total size of flex items is already aligned with the flex container size by default. Also, if the flexbox has content that cannot shrink — such as long text, this property won't work properly.

Practice

Objective:
Check how the flex-shrink 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-shrink-1, flex-item-to-shrink-2, and flex-item-to-shrink-3.

As the flex-shrink property default value is 1, set flex-shrink: 0 for the first case to show clear contrasts with other cases.

chapter15.html
<h2>flex-shrink</h2>
<h3>Flex Item Size with No Shrink</h3>
<div class="flex-container-no-padding">
  <div class="flex-item-to-shrink-1" style="flex-shrink: 0;"></div>
  <div class="flex-item-to-shrink-2" style="flex-shrink: 0;"></div>
  <div class="flex-item-to-shrink-3" style="flex-shrink: 0;"></div>
</div>

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

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

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

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

2. Update the CSS file

Open the practice.css file and add new code for adding styles to the flex items. As we set a 300px width for the container, we also need to set a 120px width for the flex items to create the situation where the total width of flex items exceeds the flex container size (additional 60px).

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

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

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

3. Check the result with a browser

  • Open chapter15.html with a browser.
  • You can see how the flex-shrink 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

CSS Calc() Function for Responsive Design

CSS Calc() Function for Responsive Design

Controlling Element Size with width and height in CSS

width and height

Create Gradient Graphic: AI as CSS Gradient Generator

Create Gradient Graphic: AI as CSS Gradient Generator

Building Forms in HTML

Chapter 6. HTML: Create Forms

CSS Calc() Function for Responsive Design

CSS Calc() Function for Responsive Design

Controlling Element Size with width and height in CSS

width and height

Create Gradient Graphic: AI as CSS Gradient Generator

Create Gradient Graphic: AI as CSS Gradient Generator

Building Forms in HTML

Chapter 6. HTML: Create Forms

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;