Learn CSS Flexbox - Programming Made Easy

Division Elements Being Made To Flex

When I started to learn flexbox, I experimented with division elements. Division elements are block elements by default, which means they display one beneath the other as shown in the screenshot below (fig 1).

CSS Container items not flexing because display property is unchanged.

How To Make the Division Elements Flex Onto 1 Row

To make division elements flex side by side, we just need to set the container division's "display" property to "flex" like so: "display: flex" as shown in the HTML code below (fig 2). The rest of the CSS properties are optional, which I've added just to make everything look better spaced out.

HTML & CSS (fig 2)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <style>
  5.     body {
  6.       width: 95%;
  7.       margin: auto;
  8.     }
  9.     .flex-container {
  10.       display: flex;    
  11.       padding: 5px;
  12.       border: 2px solid blue;
  13.       background-color: rgb(10, 184, 45);
  14.     }
  15.     .flex-container > div {
  16.       padding: 10px;
  17.       margin: 5px;
  18.       font-size: 40px;    
  19.       border: 2px solid rgb(198, 14, 42);
  20.       background-color: #f1f1f1;
  21.     }
  22.   </style>
  23. </head>
  24.  
  25. <body style="background-color: rgb(243, 232, 220);">
  26.   <h1>Flex Container: Getting Started</h1>
  27.   <div class="flex-container">
  28.     <div>Division 1</div>
  29.     <div>Division 2</div>
  30.     <div>Division 3</div>
  31.     <div>Division 4</div>
  32.   </div>
  33. </body>
  34. </html>

Within the above HTML code, you'll see that we have a parent division which has been assigned the class name "flex-container", and four inner divisions. Notice that the display property value for the four inner divisions is unchanged, and so it's the container itself that kind of owns the flexbox functionality...

The screenshot below (fig 3) shows the result of the above code.

CSS Container items flexing after setting the display property to flex.

To elaborate on what I mean by "owns" – CSS flexbox has what we call: "Flex Container Properties" and "Flex Item Properties", but if the container's display property isn't set to flex as discussed above, then those container and item properties won't do anything.

Before we continue to discuss and understand more about CSS flexbox, you might want to watch the following video tutorial. I demonstrate all of the most important flexbox features, and analyse what is happening in considerable detail.

CSS Flexbox Video Tutorial - Learn the Most Important Features

The most important flexbox container properties are:

The most important flexbox item properties are:

CSS Flexbox "flex-direction" Property

CSS Flexbox refers to the horizontal and vertical axes as the "Main Axis" and the "Cross Axis". By default, the flex-direction property is set to "row", which I initially thought meant horizontal, until I read about the CSS "writing-mode" property – MDN Web Docs "The writing-mode CSS property sets whether lines of text are laid out horizontally or vertically, as well as the direction in which blocks progress."

So the default writing mode value "horizontal-tb" does indeed result in the flex direction being horizontal when "flex-direction" is set to its default value row, and vertical when flex-direction is set to "column". Flexing occurs along the main axis, and so when the flex direction is set to column, the main axis runs vertical as shown below in screenshot (fig 4).

Code Snippet
  1. <style>
  2.   .flex-container {
  3.     display: flex;
  4.     flex-direction: column;
  5.     /*...*/
  6.   }
The CSS flex-direction property has been set to column, as represented by the main axis and cross axis.

CSS Flexbox "flex-wrap" Property

When you set the "flex-wrap" property to "wrap", it causes the items that cannot fit on the current row, to wrap onto the next row.

Flex wrapping occurs either horizontally or vertically depending on the flex direction as discussed above. For example when "flex-direction" is set to "column" as shown above in (fig 4), wrapping occurs horizontally, i.e. division 1 has wrapped onto a new column. When flex-direction is set to "row" as shown in screenshot (fig 5), wrapping occurs vertically onto a new row.

Code Snippet
  1. <style>
  2.   .flex-container {
  3.     display: flex;
  4.     flex-wrap: wrap;
  5.     /*...*/
  6.   }
Container items wrapping onto a new row after setting flex-wrap to wrap.

CSS Flexbox "justify-content" Property

The "justify-content" property is used to control how items are spaced out in the given row. There are quite a few values to choose from, and so I've only shown five, commonly used values as shown below in screenshot (fig 6).

Code Snippet
  1. <style>
  2.   .flex-container {
  3.     display: flex;
  4.     justify-content: flex-start;
  5.     /*...*/
  6.   }
Five container rows, each which has a different value set for the justify-content property.

CSS Flexbox "align-items" vs "align-content"

The difference between these two properties can be confusing, so it's worth watching how I demonstrate the difference in the video... further up the page.

These two properties are both vertical alignment properties as show below in (fig 7 and fig 8), in contrast to "justify-content" controlling horizontal alignment as shown above in (fig 6), but there is a fundamental difference in how both vertical alignment properties work, and so they both play an important roll in CSS flexbox.

CSS Flexbox "align-items" property

The "align-items" property aligns all the items in a given row, relative to the row's available height. For example if you look at (fig 7), you can see that align-items is set to "center", which results in the items being vertically central (concentric) to one another along any given row.

VS Code screenshot of align-items vs align-content, including source code.

CSS Flexbox "align-content" property

The "align-content" property aligns all the rows relative to one another. For example if you look below at (fig 8) in contrast to (fig 7), you can see that align-content is now set to "flex-start", which pushes both rows upwards against each another.

VS Code screenshot of align-content set to flex-start, including source code.

CSS Flexbox "flex-grow" property

Screenshot (fig 10) below, demonstrates how the "flex-grow" property is being used to stretch (i.e. expand) the division elements via inline styling (fig 9). The default value is 0 which means no growing occurs. How much an individual element grows, is its grow value as a percentage (i.e. proportion) of all the grow values added together.

The width of each division element as determined by the text it contains + padding + borders + margins, when subtracted from the container's total width = available space. That available space is then divided up in accordance with the grow value proportions as mentioned above. Each grow amount is then added to the corresponding division.

If the sum of all the grow values >= 1, then all the available space will be used up to fill the container. However, it's perfectly valid to use fractional values, and so for example if flex-grow is set to 0.125 for each of the four divisions, then only 50% of the available space will be used up.

HTML & CSS (fig 9)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <style>
  5.     body {
  6.       width: 95%;
  7.       margin: auto;
  8.     }
  9.     .flex-container {
  10.       display: flex;
  11.       height: 200px;
  12.       padding: 5px;
  13.       border: 2px solid blue;
  14.       background-color: rgb(10, 184, 45);
  15.     }
  16.     .flex-container > div {
  17.       display: flex;
  18.       height: 75px;
  19.       margin: 5px;
  20.       font-size: 40px;
  21.       justify-content: center;
  22.       align-items: center;
  23.       border: 2px solid rgb(198, 14, 42);
  24.       background-color: #f1f1f1;
  25.     }
  26.   </style>
  27. </head>
  28.  
  29. <body style="background-color: rgb(243, 232, 220);">
  30.   <h1>Flex Container: Flex Grow</h1>
  31.   <div class="flex-container">
  32.     <div style="flex-grow: 1; color: rgb(9, 103, 20);">Div 1</div>
  33.     <div style="flex-grow: 3; color: rgb(53, 29, 208);">Div 2</div>
  34.     <div style="flex-grow: 7; color: rgb(117, 19, 178);">Div 3</div>
  35.     <div style="flex-grow: 13; color: rgb(181, 23, 110)">Div 4</div>
  36.   </div>
  37. </body>
  38. </html>
CSS flex-grow stretching division elements individually to each have a different width.

CSS Flexbox "flex-shrink" property

The default value for the "flex-shrink" property is 1, which causes the container items to shrink if necessary to fit inside the container. This default behaviour could easily result in the programmer being unaware that flex-shrink is shrinking the items.

So I've revealed the amount that flex-shrink would be reducing the width of the items, shown below in (fig 12), by setting its value to 0 as shown in (fig 11), thereby disabling flex-shrink. The amount that each individual item shrinks is proportional to its shrink value, exactly like it works for "flex-grow", which essentially makes flex-shrink the exact opposite of flex-grow.

HTML & CSS (fig 11)
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <style>
  5.     body {
  6.       width: 95%;
  7.       margin: auto;
  8.     }
  9.     .flex-container {
  10.       display: flex;
  11.       width: 90%;
  12.       height: 200px;
  13.       border: 2px solid blue;
  14.       background-color: rgb(10, 184, 45);
  15.     }
  16.     .flex-container > div {
  17.       flex-shrink: 0;
  18.       height: 75px;
  19.       margin: 15px;
  20.       padding: 10px;
  21.       font-size: 40px;
  22.       border: 2px solid rgb(0, 0, 0);
  23.       background-color: #ffffff;
  24.     }
  25.   </style>
  26. </head>
  27.  
  28. <body style="background-color: rgb(243, 232, 220);">
  29.   <h1>Flex Container: Flex Shrink</h1>
  30.   <div class="flex-container">
  31.     <div style="width: 200px; color: rgb(9, 103, 20);">Div 1</div>
  32.     <div style="width: 350px; color: rgb(53, 29, 208);">Div 2</div>
  33.     <div style="width: 500px; color: rgb(117, 19, 178);">Div 3</div>
  34.     <div style="width: 650px; color: rgb(181, 23, 110)">Div 4</div>
  35.   </div>
  36. </body>
  37. </html>
CSS flex-shrink has been disabled by setting its value to 0, which is causing the container to overflow.

CSS Flexbox "flex-basis" property

The "flex-basis" property tells the browser to treat items as having specific widths, which are then used to calculate the flex spacing between or around the items. Flex basis is best understood in conjunction with "flex-grow", because the final size of any item is directly dependent its own initial size and the container's available space.

For example if flex-basis is set to some value – let's say for all items it's set to 0... then the available space available becomes the full width of the container (minus borders etc), resulting in all items expanding from 0 to an equal width (based on each having the same grow value), regardless of the item's true initial width.

In the flexbox video tutorial further up the page, I've included a nice calculation, demonstrated with the assistance of VS Code's development tools.

A flex-basis calculation demonstrated inside VS Code's development tools.