Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

Sizing in Flexbox

6m:28s

The term flexbox is short for flexible box, which means that the size of the flexbox can grow and shrink to adapt to the amount of free space available in its parent container. This concept was completely different from what most web developers were used to when it was first introduced, and many folks found it hard to understand.

This was because the size of a box was usually a direct reflection of the width or height values we set on it, but in a flex formatting context, the final size of a flexbox may not necessarily be that exact value you declared. Today, we have the benefit of Devtools that can break down and show you what the browser is doing when calculating the sizes of your flexboxes.

The flex property only applies to flex items and specifies the components of a flexible sizing. flex is actually the shorthand property for flex-grow, flex-shrink and flex-basis. The specification encourages you to use the shorthand instead of the individual longhand properties because the shorthand resets unspecified component values to accommodate the most common use-cases.

Annotated diagram of the flex property shorthand

The initial value of flex is 0 1 auto, which resolves to a flex-grow of 0, a flex-shrink of 1 and a flex-basis of auto. As mentioned in CSS syntax and terminology, the use of shorthands may come with some unintended consequences if you are not aware of what values get reset, so we will have dedicated section on the flex shorthand once you get familiar with the individual longhand components.

flex-grow

This property determines how much of the available free space ought to be distributed to a particular flex item. It is only apparent when the flex container is larger than the total widths of the flex items within it, hence the available free space for distribution.

Let’s say we have 3 boxes, A, B and C, and their widths are 170px, 140px and 160px respectively. These 3 boxes live within a parent container of 800px. Which means there is 330px of free space within that container.

Diagram of initial sizes of flex items set to grow

In a flex formatting context, how this extra 330px is distributed between the 3 boxes is determined by the flex-grow value. It is a number value that determines the flex grow factor of a flex item. If all 3 boxes have a flex-grow of 1, then they each an equal amount of free space and end up with an extra 110px each.

Diagram of final sizes of flex items set to grow

Or if you only want space evenly distributed to the first 2 boxes but none to the last, you would set the flex-grow for A and B to be 1 and leave C alone, which would give A and B an extra 165px each and C would not grow at all.

Diagram of final sizes where 1 flex item does not grow

It is important to understand that the flex grow factor impacts the amount of extra free space distributed and not the size of the flex item directly. You can see how the browser does these calculations via Firefox Devtools.

Firefox devtools showing the flex grow calculations

flex-shrink

This property is very similar to flex-grow but in the opposite direction. In other words, this value determines how much a flex item should shrink so it can still fit within the bounds of its parent container. It only becomes apparent when the width of the parent container is less than the sum of all the widths of the flex items.

The calculations for how much a flex item should shrink is not as straightforward as for flex grow. For each of the flex items that needs to be shrunk:

  1. Multiply its flex-shrink value by its flex-basis
  2. Divide the resultant value by the sum of flex-basis values of all the flex items
  3. Take this value and multiply by the total amount of space that needs to be shrunk
  4. That final value is the amount your flex item needs to shrink by
Firefox devtools showing the flex shrink calculations

Let’s say we have a container that is 600px and our 3 flex items are 150px, 450px and 300px respectively with the default flex value of 0 1 auto. In this case, our 3 items’ calculations for the amount to shrink would look like this:

A: (1 * 150) / (150 + 450 + 300) * 300 = 50px

B: (1 * 450) / (150 + 450 + 300) * 300 = 150px

C: (1 * 300) / (150 + 450 + 300) * 300 = 100px

Again, if you have Firefox Devtools, you will be able to see the calculations up to 2 decimal points but that’s how the browser got to those values.

If you do not want any shrinking to occur, you set the flex-shrink value on your flex item to 0. This will prevent the flex item from shrinking below its original width but may result in your flex items overflowing their parent container.

flex-basis

This property is key for understanding how sizing works with flexbox. The flex-basis value determines the starting value of the flex item, which, depending on the situation, may not be the final value of the flex item.

The initial value of this property is auto, which is the width value of the flex item if one was explicitly set. If no width value was set, then this value will be whatever the size of the content is in the flex item.

The width of the content is 433.15px, which is how much space is needed for the entire sentence to be laid out in a single line.

If a width value is present while the flex-basis value is set to auto, then width will be used as the starting value from which the browser calculates the final size.

The width of the element had been set to 300px and is reflected in the Base Size display on Devtools

But if an explicit flex-basis value is set, then that value is used as the starting value from which the browser calculates the final size.

The element now has a flex-basis value of 250px set, which is reflected in the Base Size display on Devtools

Earlier, in the flex-grow section, I mentioned that free space is whatever space is leftover in the parent container after accounting for the space taken up by the flex items. This space can either be the width of the content in the flex item, an explicit width set on the flex item or the flex-basis value.

However, there is a way to let the browser take the entire width of the parent container as “free space”. You can do this by setting flex-basis to 0. Just for comparison’s sake, this is how the browser handles flex items with their flex value set to flex: 1 1 auto:

Firefox devtools showing calculations when flex basis is auto

You can see that the starting size is the content size for this example. However, if you set the flex basis value to 0, the browser will use the entire width of the flex container as free space for distribution, like so:

Firefox devtools showing calculations when flex basis is zero

You can see that the 3 flex items end up being exactly the same width despite having different amounts of content. This is because the content width no longer factors into the final size calculation in this particular case.