1. Web Design
  2. HTML/CSS
  3. HTML Templates

A Comprehensive Guide to Flexbox Sizing

Scroll to top
25 min read
29

Flexbox sizing makes it possible to create flexible layouts that fully adapt to the screen. If you set up everything correctly you won’t have to rely on media queries to support different viewports, layouts, and orientations.

In this guide I’ll show you how to use the following flexbox sizing properties:

  • flex-grow
  • flex-shrink
  • flex-basis
  • flex 

Explainer: Flexbox Sizing

Filling up Free Space

One of the most challenging aspects of writing CSS is figuring out how to allocate the free space that remains on the screen after the page has been populated with content. At some viewport sizes, you’ll often find there’s too much remaining space and you want to fill it with something. At other viewport sizes, you might find there’s not enough space, and the layout breaks in one way or another.

Flexbox’s sizing properties allow you to make decisions about three kinds of scenarios:

  1. flex-grow: how flex items should behave when there’s a surplus of free space (how they should grow).
  2. flex-shrink: how flex items should behave when there’s a shortage of free space (how they should shrink).
  3. flex-basis: how flex items should behave when there’s exactly as much space as needed.

As flexbox is a one-dimensional layout, as opposed to CSS Grid which is two-dimensional, you can allocate free space along the main axis (whether that be top to bottom, bottom to top, left to right, or right to left). You can set the direction of the main axis using the flex-direction property. If you need a refresher on how this works take a look at my tutorial about flexbox alignment.

The cross axis is always perpendicular to the main axis The cross axis is always perpendicular to the main axis The cross axis is always perpendicular to the main axis
A reminder of how the main axis and the cross axis interact.

The most common flex-direction used on left-to-right websites is row, which means you can allocate free space on the left-to-right axis. This also happens to be the default value of flex-direction, so I’ll use it in the following examples.

1. Positive Free Space: flex-grow

The flex-grow property defines how any extra space in-between flex items should be allocated on the screen. The most important thing to remember about flexbox sizing is that flex-grow doesn’t divide up the entire flex container, only the space that remains after the browser renders all flex items. If there’s no surplus of space, flex-grow has no effect.

 Let’s start with the following HTML:

1
<div class="container">
2
    <div class="item item-1">1</div>
3
    <div class="item item-2">2</div>
4
    <div class="item item-3">3</div>
5
</div>

The  .container class will be the flex container (defined by display: flex;) and our .item elements will be the flex items:

1
.container {
2
  background-color: darkslategrey;
3
  padding: 1rem;
4
  display: flex;
5
  flex-direction: row;
6
}
7
.item {
8
  font-size: 2rem;
9
  line-height: 1;
10
  text-align: center;
11
  width: 3rem;
12
  padding: 2rem 1rem;
13
  background: #e2f0ef;
14
  border: 3px solid #51aaa3;
15
  color: #51aaa3;
16
  margin: 0.375rem; 
17
}

Without telling the browser what to do with the remaining space, this is how flex items are allocated on the screen:

The browser has used the default value of flex-grow, which is 0, and gives us total inflexibility. This arrangement might be a good solution for some layouts, however, you can also make the items cover the whole space by setting flex-grow to 1:

1
.item {
2
  flex-grow: 1;
3
}

As you can see below, the flex items have stretched out and filled the whole available space:

In the above example, all flex items have the same flex-grow value, so they grow at the same rate. However, you can also make them grow according to different ratios. For instance, .item-1 can take up twice as much of the available space as the other items. We would write that as follows:

1
.item {
2
    flex-grow: 1;   
3
}
4
.item-1 {
5
    flex-grow: 2;
6
}

Similarly, you can set a different flex-grow value for each flex item to make them grow relative to each other. Play around with the values in this example and see how they affect the layout:

2. Negative Free Space: flex-shrink

The flex-shrink property is the opposite of flex-grow. It defines how flex items should behave when there’s not enough space on the screen. This happens when flex items are larger than the flex container.

Without flex-shrink, the following CSS would result in a layout where the items overflow the container, as the total width of the items (3*10rem) is bigger than the container’s width (20rem).

1
.container {
2
    width: 20rem;
3
    padding: 1rem;
4
    background-color: darkslategrey;
5
    display: flex;
6
    flex-direction: row;
7
}
8
.item {
9
    width: 10rem;
10
    padding: 2rem 1rem;
11
    font-size: 2rem;
12
    line-height: 1;
13
    text-align: center;  
14
    background: #e2f0ef;
15
    border: 3px solid #51aaa3;
16
    color: #51aaa3;
17
    margin: 0.375rem; 
18
}
no flex-shrinkno flex-shrinkno flex-shrink
Hypothetical world without flex-shrink..

Happily for us, flex-shrink is implied, taking on the default value of 1, giving us a layout where the items fit into the container even though there’s not enough space:

When flex-shrink is 1, flex items are fully flexible and when there's not enough space, they shrink together with the flex container. 

Following the same logic, you can make flex items fully inflexible when there’s negative space on the screen. You only have to set flex-shrink to 0 and the items will overflow the flex container:

1
.item {
2
    flex-shrink: 0;
3
}

Similarly to flex-grow, you can also set a different flex-shrink value for each flex item so that they can shrink relatively to each other. Play around with the values in this example and see what impact they have:

Testing the above demo you might have noticed that larger flex-shrink values lead to narrower flex items. For instance, the following CSS results in a layout where .item-3 is the narrowest item:

1
.item-1 {
2
   flex-shrink: 1;
3
}
4
.item-2 {
5
   flex-shrink: 1;
6
}
7
.item-3 {
8
   flex-shrink: 2;
9
}
greater flex-shrink value on the third itemgreater flex-shrink value on the third itemgreater flex-shrink value on the third item

This is because flex-shrink defines how much a flex item should shrink compared to other items. Thus, larger flex-shrink values lead to smaller elements, which can make things pretty confusing!

3. No Remaining Space: flex-basis

The last scenario of free space allocation is when there’s exactly as much space on the screen as you need. This is when flex items will take the value of flex-basis

The flex-basis property defines the initial size of flex items. The default value of flex-basis is auto, which means that the size of the flex items is calculated using either the width or height of the element (depending on if it’s a row-based or column-based layout). 

However, when the value of flex-basis is something other than auto, it overrides the value of width (or height in case of vertical layouts). For example, the following CSS overrides the default width: 20rem; rule with a respective value for each flex item:

1
.item {
2
    width: 20rem;
3
    padding: 2rem 1rem;
4
    font-size: 2rem;
5
    line-height: 1;
6
    text-align: center;  
7
    background: #e2f0ef;
8
    border: 3px solid #51aaa3;
9
    color: #51aaa3;
10
    margin: 0.375rem; 
11
}
12
.item-1 {
13
   flex-basis: 5rem;
14
}
15
.item-2 {
16
   flex-basis: 10rem;
17
}
18
.item-3 {
19
   flex-basis: 30rem;
20
}

Besides length units, percentages, and auto, you can also use the content keyword as a value for flex-basis. It will make a flex item as wide as the content it holds.

As flex-basis defines the initial value of flex items, it's the basis the browser uses to calculate flex-grow and flex-shrink. Note that while flex-grow and flex-shrink have relative values (0, 1, 2, etc.), flex-basis always takes an absolute value (px, rem, content, etc.).

The flex Shorthand

Flexbox’s sizing properties also have a shorthand called flex. The flex property abbreviates flex-grow, flex-shrink, and flex-basis in the following way:

1
/* Longhand form of default values */
2
.item {
3
  flex-grow: 0;
4
  flex-shrink: 1;
5
  flex-basis: auto;
6
}
7
8
/* Shorthand form of default values */
9
.item {
10
    flex: 0 1 auto;
11
}

You don’t necessarily have to list all the three values if you don’t want. You can use flex with one or two values, according to the following rules and assumptions:

1
/* One relative value: flex-grow */
2
flex: 1;
3
4
/* One absolute value: flex-basis */
5
flex: 20rem;
6
flex: 50px;
7
8
/* One relative and one absolute value: flex-grow | flex-basis */
9
flex: 1 20rem;
10
11
/* Two relative values: flex-grow | flex-shrink */
12
flex: 1 2;
13
14
/* Three values: flex-grow | flex-shrink | flex-basis */
15
flex: 1 2 20rem;
16
17
/* Fully inflexible layout: equivalent of flex: 0 0 auto */
18
flex: none;

It might take a while to get used to the flex shorthand, but the W3C docs actually recommend using it, instead of the longhand properties:

“Authors are encouraged to control flexibility using the flex shorthand rather than with its longhand properties directly, as the shorthand correctly resets any unspecified components to accommodate common uses.”

Conclusion

There we have it: you can completely control flexibility with flexbox’s sizing properties! In this tutorial, I used a horizontal layout set by flex-direction: row, so space allocation happened along the horizontal (left to right) axis and flex-growflex-shrink, and flex-basis modified the width of the flex items. 

If you take what we covered and apply it to a vertical layout set by flex-direction: column, allocation will happen along the vertical (top-to-bottom) axis and the sizing properties will modify the height of the flex items.

This tutorial is part of my Comprehensive Guide to Flexbox series. To fully understand how flexbox works, be sure to check out the other parts:

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.