Unlocking the Power of Flexbox: Understanding flex-grow, flex-shrink, and flex-basis

Savan Chhayani
TechVerito
Published in
3 min readDec 2, 2024

--

Photo by Mikael Kristenson on Unsplash

Flexbox is a layout model in CSS that allows elements within a container to be distributed and aligned efficiently. The flex property is shorthand for setting three properties: flex-grow, flex-shrink, and flex-basis. Here's a brief explanation and an example of how they work together.

Flex Properties

  1. flex-grow: This defines how much a flex item will grow relative to the other flex items inside the same container when extra space is available. The default value is 0 (no growth).
  2. flex-shrink: This defines how much a flex item will shrink relative to the rest of the flex items inside the same container when there is not enough space available. The default value is 1 (it will shrink if necessary).
  3. flex-basis: This sets the initial size of a flex item before any space distribution happens according to flex-grow and flex-shrink. The default value is auto, which means the item will be sized according to its content.

Example

Let’s create a flex container with three items and see how flex-grow, flex-shrink, and flex-basis affect their sizes.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox Example</title>
<style>
.container {
display: flex;
width: 600px;
height: 200px;
border: 1px solid black;
}
.item {
border: 1px solid blue;
margin: 5px;
text-align: center;
}
.item1 {
flex: 1 1 100px; /* flex-grow: 1; flex-shrink: 1; flex-basis: 100px; */
}
.item2 {
flex: 2 2 150px; /* flex-grow: 2; flex-shrink: 2; flex-basis: 150px; */
}
.item3 {
flex: 1 0 200px; /* flex-grow: 1; flex-shrink: 0; flex-basis: 200px; */
}
</style>
</head>
<body>
<div class="container">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
</body>
</html>

Output:

Explanation

Item 1: flex: 1 1 100px;

  • flex-grow: 1: It can grow, and will take up space proportional to its flex-grow value relative to other items.
  • flex-shrink: 1: It can shrink if necessary.
  • flex-basis: 100px: Its initial size is 100px.

Item 2: flex: 2 2 150px;

  • flex-grow: 2: It can grow twice as much as Item 1.
  • flex-shrink: 2: It will shrink twice as much as Item 1 if necessary.
  • flex-basis: 150px: Its initial size is 150px.

Item 3: flex: 1 0 200px;

  • flex-grow: 1: It can grow, taking up space proportional to its flex-grow value.
  • flex-shrink: 0: It will not shrink even if there is insufficient space.
  • flex-basis: 200px: Its initial size is 200px.

Behavior Analysis

In a container of 600px width:

  1. The initial total width taken by all items is 100px (Item 1) + 150px (Item 2) + 200px (Item 3) = 450px.
  2. Extra space available in the container is 600px —450px = 150px.

Distribution of Extra Space (150px):

  • flex-grow values:
    -
    Total flex-grow = 1(Item 1) + 2 (Item 2) + 1 (Item 3) = 4 parts.
  • Space distribution based on flex-grow:
    - Item 1: 1/4 * 150px = 37.5px
    - Item 2: 2/4 * 150px = 75px
    - Item 3: 1/4 * 150px = 37.5px

Total width after growth:

- Item 1: 100px (initial) + 37.5px = 137.5px
- Item 2: 150px (initial) + 75px = 225px
- Item 3: 200px (initial) + 37.5px = 237.5px

The items distribute the available space according to their flex-grow values, and they don't shrink beyond their flex-basis If there needs to be more space.

This example demonstrates how flex-grow, flex-shrink, and flex-basis work together to control the sizes of flex items within a container.

Photo by Jackson Sophat on Unsplash

If you enjoyed this post, please hit the follow button and give it a clap. Stay tuned for more exciting topics!

Happy Coding ⌨️

--

--

TechVerito
TechVerito

Published in TechVerito

All about Technology, Software Craftsmanship, Agile

Savan Chhayani
Savan Chhayani

Written by Savan Chhayani

Senior Consultant At Techverito React | JavaScript | TypeScript | Kotlin | Spring Boot | Docker

No responses yet