Mastering Flexbox: The Modern way to layout web pages

1: Introduction

When it comes to designing modern, responsive web layouts, Flexbox (Flexible Box Layout) is an essential tool in every web developer's toolkit. Since its introduction, Flexbox has revolutionized how we structure and align content, making previously complex tasks significantly more straightforward. In this blog, we'll dive deep into what Flexbox is, its features, and how you can leverage it to create flexible, efficient, and responsive web designs.

2: What is Flexbox?

Flexbox, or Flexible Box Layout, is a CSS3 layout module designed to provide a more efficient way to lay out, align, and distribute space among items within a container, even when their sizes are unknown or dynamic. It is particularly useful for creating complex layouts that need to be responsive to different screen sizes.

3: Here are the key features of Flexbox

3.1) Flex Container and Flex Items

  • Flex Container: This is the box (or parent element) where you turn on Flexbox with display: flex.
  • Flex Items: These are the boxes (or child elements) inside the flex container.

3.2) Main Axis and Cross Axis

  • Main Axis: This is the direction you want your items to line up in. It can be horizontal (like a row) or vertical (like a column).
  • Cross Axis: This is the direction that is perpendicular to the main axis.

3.3) Flex Properties

  • flex-direction: Tells the container to arrange items in a row (left to right) or column (top to bottom), and their reverse orders.
  • justify-content: Controls how items are spaced out along the main axis (e.g., evenly spaced, all pushed to one side, centered).
  • align-items: Controls how items are aligned along the cross-axis (e.g., stretched to fill, centered, starting from the top).
  • align-self: Let a single item override the align-items rule, so it can be aligned differently from the others.
  • flex-wrap: Decides if items should stay on one line or wrap onto new lines if they don’t fit.
  • flex-grow: Let items grow bigger to fill extra space in the container.
  • flex-shrink: Let items shrink if the container is too small.
  • flex-basis: Sets a default size for the items before they grow or shrink.

display

By default, when we have a container with 4 child items, they are displayed one below the other because a `div` is a block-level element. By using `display: flex;`, we enable the flex context for all its direct children. Note that CSS columns do not have any effect on a flex container. Below, I have added examples for Flexbox. You can check these examples for a complete understanding.

Code Example

                                        
HTML
< 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 class = " item item4 " > Item 4 < / div >
< / div >
                                        
CSS
.container { display : flex ; / or inline-flex / }

Image Example

vistotheme, flexbox, website design
vistotheme, flexbox, website design

flex-direction

The function of flex-direction in CSS is to tell child elements in which direction they will be positioned or oriented. Direction means either they will be in a row or they will be in a column format and they can also be column reverse and row reverse. We have four settings with flex direction. The flex-direction property in CSS has a default value is row. This means that, by default, items in a flex container are arranged in a horizontal line from left to right. Below, I have added examples for flex-direction. You can check these examples for a complete understanding.

Code Example

                                        
CSS
.container { flex-direction : row | row-reverse | column | column-reverse ; }

Image Example

vistotheme, flexbox, flex-direction, website design
  • row (default): Items are arranged horizontally from left to right
  • row-reverse: Items are arranged horizontally from right to left.
  • column: Items are stacked vertically from top to bottom.
  • column-reverse: Items are stacked vertically from bottom to top.

flex-wrap

The flex-wrap property in CSS decides if child items should fit in a single line or wrap to the next line within the main container.

For example, if we have four items with widths of 20%, 50%, 30%, and 30%, they total 130%. Since this exceeds 100%, some items will wrap to the next line. This wrapping behavior is controlled by the flex-wrap property. Below, I have added examples for flex-wrap. You can check these examples for a complete understanding.

Code Example

                                        
CSS
.container { flex-wrap : nowrap | wrap | wrap-reverse ; }

Image Example

vistotheme, flexbox, flex-wrap, website design
  • nowrap (default): All items stay on a single line, regardless of how much space they need.
  • wrap: Items will wrap onto multiple lines if they exceed the container's width or height.
  • wrap-reverse: Similar to wrap, but the lines are stacked in reverse order, with new lines appearing above the previous ones.

flex-flow

The flex-flow property in CSS is a shorthand for the flex-direction and flex-wrap properties. By using flex-flow, you can define both properties in a single line. This property controls the direction and wrapping of items within the container. The default value of flex-flow is row nowrap.

Code Example

                                        
CSS
.container { flex-flow : column wrap ; }

justify-content

The justify-content property in CSS flexbox controls the alignment of flex items along the main axis (horizontal or vertical, depending on the flex-direction) within a flex container. It defines how the extra space is distributed among the flex items. It determines if items start from the beginning, end, or center of the container. This property helps arrange and space out items inside a flex container. Here are the possible values:

Code Example

                                        
CSS
.container { justify-content : flex-start | flex-end | center | space-between | space-around | space-evenly | start | end ; }

Image Example

flex-start (default): Items are grouped at the start of the main axis.

vistotheme, flexbox, justify-content, website design

flex-end: Items are grouped at the end of the main axis.

vistotheme, flexbox, justify-content, website design

center: Items are centered along the main axis.

vistotheme, flexbox, justify-content, website design

space-between: Items are spaced evenly, with the first item at the start and the last item at the end.

vistotheme, flexbox, justify-content, website design

space-around: Items have equal space around them, with half the space at the edges.

vistotheme, flexbox, justify-content, website design

space-evenly: Items are spaced evenly, with equal space around all items, including the edges.

vistotheme, flexbox, justify-content, website design

align-items

The align-items property in CSS flexbox arranges flex items along the cross axis, which runs perpendicular to the main axis (either horizontal or vertical, based on the flex-direction). It determines how items are positioned within the flex container along this cross axis. Below, I have added examples for align-items. You can check these examples for a complete understanding.

Code Example

                                        
CSS
.container { align-items : stretch | flex-start | flex-end | center | baseline ; }

Image Example

flex-start: Items are aligned at the top (for a vertical axis) or at the start (for a horizontal axis).

vistotheme, flexbox, justify-content, website design

flex-end: Items are aligned at the bottom (for a vertical axis) or at the end (for a horizontal axis).

vistotheme, flexbox, justify-content, website design

center: Items are centered along the main axis.

vistotheme, flexbox, justify-content, website design

baseline: Items are aligned based on where their text lines up.

vistotheme, flexbox, justify-content, website design

stretch (default): Items stretch to fill the space along the cross axis.

vistotheme, flexbox, justify-content, website design

align-content

The align-content CSS property is used in a flexbox layout to control the alignment of flex lines (rows or columns) within a flex container when there is extra space along the cross axis (the axis perpendicular to the direction of the main axis). This property is useful when you have multiple lines of flex items and want to control how these lines are spaced within the flex container.

Here are the possible values for align-content:

Code Example

                                        
CSS
.container { align-content : flex-start | flex-end | center | space-between | space-around | space-evenly | stretch | start | end ; }

Image Example

flex-start: Items are placed at the top (or start) of the container.

vistotheme, flexbox, justify-content, website design

flex-end: Items are placed at the bottom (or end) of the container.

vistotheme, flexbox, justify-content, website design

center: Items are placed in the middle of the container.

vistotheme, flexbox, justify-content, website design

stretch (default): Items stretch to fill the available space.

vistotheme, flexbox, justify-content, website design

space-between: Items are spread out evenly, with the first line at the top and the last line at the bottom.

vistotheme, flexbox, justify-content, website design

space-around: Items have equal space around them, with extra space at the edges.

vistotheme, flexbox, justify-content, website design

space-evenly: Items are spaced out equally, including the space at the edges.

vistotheme, flexbox, justify-content, website design

gap

The gap property in CSS flexbox (also known as row-gap and column-gap when used individually) is used to set the space between flex items. This property makes it easier to create consistent spacing without needing to use margins on individual items.

Here’s how it works:

Code Example

                                        
CSS
.container { display : flex ; flex-wrap : wrap ; gap : 10px ; /* sets a 10px gap between both rows and columns. */ gap : 10px 24px ; /* sets a 10px gap between rows and 24px gap between columns */ row-gap : 10px ; /* sets a 10px gap between rows. */ column-gap : 10px ; /* sets a 20px gap between columns. */ }

Image Example

gap: 30px 12px; 30px vertical gap, 12px horizontal gap.

vistotheme, flexbox, justify-content, website design

gap: 16px; 16px vertical gap, 16px horizontal gap.

vistotheme, flexbox, justify-content, website design

Order

The order CSS property is used in a flexbox layout to control the order in which individual flex items appear within a flex container. By default, flex items are laid out in the order they appear in the source code, but the order property allows you to override this default behavior.

Here’s a simple example:

Code Example

                                        
HTML
< div class = " container " >
< div class = " item item1 " > 1 < / div >
< div class = " item item2 " > 1 < / div >
< div class = " item item3 " > 5 < / div >
< div class = " item item4 " > 1 < / div >
< div class = " item item5 " > 1 < / div >
< / div >
                                        
CSS
.container { display : flex ; align-items : center ; justify-content : center ; } .item3 { order : 5 ; /* default is 0 */ }

Image Example

(integer) : The integer value determines the order of the flex item relative to the other flex items within the same container. The default value is 0. Negative, positive, and zero values are all allowed.

vistotheme, flexbox, justify-content, website design

flex-grow

The flex-grow property in CSS flexbox is used to specify how much a flex item should grow relative to the rest of the flex items inside a flex container. It determines how extra space in the container should be distributed among its children.

Here’s a simple example:

Code Example

                                        
CSS
.item2 { flex-grow : 4 ; /* default 0 */ }

Image Example

If all flex items have flex-grow set to 0 (the default), they will not grow beyond their initial size. If a flex item has a flex-grow value greater than 0, it will grow to fill the available space in the container according to the proportion defined by the flex-grow value relative to other flex items.

vistotheme, flexbox, justify-content, website design

flex-shrink

The flex-shrink property in CSS flexbox is used to specify how much a flex item should shrink relative to the rest of the flex items inside a flex container when there isn't enough space. It determines how the items shrink to fit the container when the container is too small.

Code Example

                                        
CSS
.item2 { flex-shrink : 3 ; /* default 1 */ }

flex-basis

The flex-basis property in CSS flexbox is used to specify the initial main size (width or height depending on the main axis direction) of a flex item before the remaining space is distributed. It defines the initial size of a flex item along the main axis.

Code Example

                                        
CSS
.item2 { flex-basis : auto ; /* default auto */ }

flex

The flex property in CSS shorthand notation is used to set three separate properties for flexible layout items in a flex container: flex-grow, flex-shrink, and flex-basis. It's a good idea to use the shorthand `flex` property instead of setting the individual `flex-grow`, `flex-shrink`, and `flex-basis` properties separately. The shorthand automatically handles these values in a more efficient and concise manner, making your CSS code cleaner and easier to maintain.

Code Example

                                        
CSS
.item2 { flex : flex-grow, flex-shrink, flex-basis ; }

align-self

The align-self CSS property is used within a flex container to control the alignment of individual flex items along the cross axis. It overrides the default alignment set by the container's align-items property for the specific item.

Code Example

                                        
CSS
.item2 { align-self : auto | flex-start | flex-end | center | baseline | stretch ; }

Image Example

vistotheme, flexbox, justify-content, website design, vistothemes.com
vistotheme, flexbox, justify-content, website design, vistothemes.com
  • auto: (default): The item aligns like its container's other items.
  • flex-start: The item aligns to the beginning means top.
  • flex-end: The item aligns to the end means bottom.
  • center: The item aligns at the middle or center.
  • baseline: The item aligns with the text baseline.
  • stretch: The item stretches to fill the space.

Let's Talk with Me

I love to hear from you! Whether you have a question, need support, or want to discuss a new project, feel free to get in touch. You can reach me via email, phone, or through our social media channels. We look forward to connecting with you!