Flexbox Alignment

flex-wrap

The flex-wrap property sets whether flex items are forced onto one line or can wrap onto multiple lines. If wrapping is allowed, flex-wrap sets the direction in which lines are stacked. It's also affected by the choices made in flex-direction and justify-content. flex-wrap can also be used to change the direction of the cross-axis.

Pros and cons of utilizing flex-wrap:

Key values associated with the flex-wrap property: a demo with flex-direction:column

There are three commonly accepted values used with flex-wrap, which are the default value nowrap, wrap, and wrap-reverse. Obviously, our 5 200px high divs DO NOT FIT inside a 500px container. Let's see what flex-wrap and its values do with this:

  1. nowrap is the default. With nowrap, the flex items will be laid out in a single line. In undersized containers, these elements will shrink down to fit. Everything is aligned to the top, because our main axis goes from top to bottom in this case.
                        
                            #container {
                                display: flex;
                                flex-direction: column;  /*main axis is vertical*/
                                justify-content: flex-start;  /*main axis is top to bottom*/
                                flex-wrap:nowrap;  /*DEFAULT!*/
                                }
                        
                        
    flex-wrap:wrap;
    flex-wrap:nowrap;
  2. Changing this to flex-wrap:wrap gives a dramatic difference! With wrap, the Flexbox properties break into multiple lines.
                        
                            #container {
                                display: flex;
                                flex-direction: column;   
                                justify-content: flex-start;
                                flex-wrap:wrap;  /*Causes STACKING*/
                            }
                        
                        
    flex-wrap:wrap;
    flex-wrap:wrap;
  3. Finally, you should see how wrap-reverse works. It's the same as wrap, but with cross-start and cross-end beng permuted.
                        
                            #container {
                                display: flex;
                                flex-direction: column;   
                                justify-content: flex-start;
                                flex-wrap:wrap-reverse;  /**/
                            }
                        
                        
    flex-wrap:wrap-reverse;
    flex-wrap:wrap;
  4. As a final example for flex-wrap, have a look at what was achieved within this container:
                        
                            #container {
                     display: flex;
                                flex-direction: row;   
                                justify-content: space-evenly;
                                flex-wrap:wrap-reverse;  
                            }
                        
                        
    Example
    Notice how flex-wrap is set, and that justify-content is space-evenly etc...

align-items

In Flexbox, the align-items property is used to align the items on the cross-axis of a flex container. Note how it constrasts with justify-content! justify-content aligns items along the main axis, whereas align-items aligns items along the cross axis! The cross-axis is the axis perpendicular to the main axis, which is determined by the flex-direction property.

Values of align-items

The align-items property takes several possible values, including:

  1. stretch (default): Stretches the items to fill the container along the cross axis. In non-flex items, the default is "normal".
                        
                            #container {
                                display: flex;
                                flex-direction: row;
                                justify-content: center;
                                align-items:stretch;
                                }
                        
                        
    align-items:stretch;
    align-items:stretch;
  2. align-items:flex-start; aligns the items to the start of the container along the cross-axis. If flex-direction is row, and justify-content is center, we'll have an identical image to the one above!
  3. flex-end aligns the items to the end of the container along the cross axis.
                        
                            #container {
                                display: flex;
                                flex-direction: row;
                                justify-content: center;
                                align-items:flex-end;
                                }
                        
                        
    align-items:flex-end;
    align-items:flex-end;
  4. center aligns the items to the center of the container along the cross axis. One really key point to note is that if you want to center your items horizontally and vertically, you can set both justify-content and align-items to center. To really show the point, I did some (temporary) inline editing of the heights of two of the divs! The HELLO is included to help reinforce the next point too!
                        
                            #container {
                                /*Some heights were changed to help show the effect*/
                                display: flex;
                                flex-direction: row;
                                justify-content: center;
                                align-items:center;
                                }
                        
                        
    align-items:center;
    align-items:center;
  5. baseline aligns the items to the baseline of the contents of container along the cross-axis. So, the baseline of the text HELLO will be aligned! In the former example, HELLO is aligned to the top of the container. However, in the 2nd image, not all of the contents are aligned to the top of the container: rather, the contents are aligned around the word HELLO: i.e. the contents are aligned to the baseline!
                        
                            #container {
                                display: flex;
                                flex-direction: row;
                                justify-content: center;
                                align-items:baseline;
                                }
                        
                        
    align-items:baseline;
    align-items:baseline;

An example of combinations and their effect

Example of combinations
Flexing Example 1: changing flex-wrap (default/wrap/wrap-reverse)

align-content

The align-content property in Flexbox is used to align the items on a flex container's cross-axis. It can be used to distribute space between rows or columns of flex items. This can be useful for creating evenly spaced grids of items, or for aligning items to the start or end of a container. The TLDR: it aligns a flex container's lines within the flex container when there is extra space in the cross-axis.

align-content is similar to justify-content, but works on the cross-axis(rows) rather than the main-axis(columns)

Finally, if neither wrap nor wrap-reverse are being used, then this property is meaningless!

The benefits of align-content

  • Allows for precise alignment of lines within a flex container.
  • Can be used to distribute space between lines evenly.
    1. stretch (default value): with this, lines stretch to take up the remaining space.

      align-content:;

      align-content:stretch;
      align-content:stretch;
                          
                              #container {
                                  display: flex;
                                  flex-direction: column;
                                  justify-content: center;
                                  align-items:flex-end;
                                  flex-wrap:wrap;
                                  align-content:stretch; /*DEFAULT*/
                                  }
                          
                          
    2. center: lines are centered within the flex container. Look at the effect on the image. Below is after

      align-content:center;

      align-content:center;
      align-content:center;
                              
                                  #container {
                                      display: flex;
                                      flex-direction: column;
                                      justify-content: center;
                                      align-items:flex-end;
                                      flex-wrap:wrap;
                                      align-content:center; 
                                      }
                              
                              
    3. space-between: lines are evenly distributed with equal space between each line.

      align-content:space-between;

      align-content:space-between;
      align-content:space-between;
                          
                              #container {
                                  display: flex;
                                  flex-direction: column;
                                  justify-content: center;
                                  align-items:flex-end;
                                  flex-wrap:wrap;
                                  align-content:space-between;
                                  }
                          
                          
    4. space-around: lines are evenly distributed with equal space around each line.

      align-content:space-around;

      align-content:space-around;
      align-content:space-around;
                          
                              #container {
                                  display: flex;
                                      flex-direction: column;
                                      justify-content: center;
                                      align-items:flex-end;
                                      flex-wrap:wrap;
                                      align-content:space-around;
                                  }
                          
                          
    5. flex-start: lines are positioned at the start of the flex container.

      align-content:flex-start;

      stretch align-content:flex-start;
      align-content:flex-start;
                          
                              #container {
                                  display: flex;
                                      flex-direction: column;
                                      justify-content: center;
                                      align-items:flex-end;
                                      flex-wrap:wrap;
                                      align-content:flex-start;
                                  }
                          
                          
    6. flex-end: lines are positioned at the end of the flex container.

      align-content:flex-end;

      align-content:flex-end;
      align-content:flex-start vs center vs flex-end;
                          
                              #container {
                                  display: flex;
                                      flex-direction: column;
                                      justify-content: center;
                                      align-items:flex-end;
                                      flex-wrap:wrap;
                                      align-content:flex-end;
                                  }
                          
                          

    align-self

    Whereas align-content is used to align the lines within a flex container along the cross-axis, align-self is used to align individual items along the cross-axis within a flex container.

  • flex-end: lines are positioned at the end of the flex container. It allows you to align a single item, and can have an effect on each element individually. I'll briefly demonstrate this on the 2nd child div in the container

    align-self:flex-end;

    align-content:flex-end;
    align-self:flex-end;
                        
                            #container {
                                background-color: #003049;
                                width: 90%;
                                height: 500px;
                                margin: 0 auto;
                                border: 5px solid #003049;
                                display: flex;
                                flex-direction: row;
                                justify-content: space-evenly;
                            }
                            
                            #container div {
                                width: 200px;
                                height: 200px;
                            }
                            
                            #container div:nth-child(2) {
                                align-self: flex-end;
                            }
                        
                        
  • If you're ready, try part 3 of this section