CSS Flexbox Cheat Sheet for Building Layouts
Tags: css, ui
Published: 2023-01-12 19:15:14

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/cssflexbox_10951fac3b18c60652e8f7621132ff86.png" alt="CSS flexbox cheat sheet for building layouts."/></figure>

Due to the technicality of web designs, it can be challenging to use other CSS properties for creating layouts. To overcome this, the CSS Flexbox came into the picture. CSS Flexbox makes it possible for us to build flexible layouts without the use of CSS `float` and positioning. In this article, we’ll provide a CSS Flexbox cheat sheet and explain hos it is useful. In addition, we’ll learn the Flexbox architecture as well as the different properties of its components.

## What is Flexbox?

A [Flexible Box Module](https://www.developphp.com/lib/CSS/Flexible-Box-Layout-Module), also known as a Flexbox, is a mechanism for building layouts in one dimension. Unlike the [CSS Grid Layout](https://code.pieces.app/blog/building-a-layout-with-css-grid-using-grid-areas-and-grid-templates-properties), which creates a two-dimensional layout by creating rows and columns simultaneously, the Flexbox creates a layout as either a flex row or flex column. Flexbox enables the arrangement of spaces and alignment between elements in a webpage.

Four layout models (block, inline, table, positioned) existed before the Flexbox. However, they lack the flexibility that Flexbox offers, such as resizing, changing orientation, and shrinking and stretching elements of web pages.

## Why Use Flexbox?

The Flexbox offers many conveniences for building web pages, and can be used for the following:

- To arrange elements in web pages neatly by aligning them
- To create responsive layouts
- To space the items on web pages
- To wrap many lines (even though it is a single line by default)
- To get maximum flexibility in building layouts
- To set order and sequence while arranging elements of web pages
- To reorder items
- Mobile-friendly development


## Flexbox Architecture

The Flexbox architecture comprises two axes:

### Main Axis

The flex items (content) move as a group on this axis. The `flex-direction` [property determines the layout position](https://code.pieces.app/blog/working-with-css-position-properties). Therefore, if you have a row, the main axis will be along the row, and if you have a column, it will be along the column. The main axis moves from left to right by default. The starting point is called the `main start` and the endpoint is called the `main end`. The distance between the main start and the main end is called `main size`.

### Cross Axis

The cross axis moves perpendicular to the main axis. Thus, if the `flex-direction` is a row, it moves along the column, and vice-versa. On the cross axis, you can align items against each other and the flex container by moving them individually or as a group. You can also control the flex spacing in wrapped flex lines by treating them as a group. The cross axis moves from top to bottom by default. The starting point is called the `cross start` and the endpoint is called the `cross end`. `Cross size` is the distance between the `cross start` and the `cross end`.

Let’s look at a flexbox example regarding the architecture:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image27_7fa234967e7c96d880a14fe734c6dc45.png" alt="Diagram of flexbox architecture."/></figure>

## Components of Flexbox

Flexbox is comprised of [two components](https://www.javatpoint.com/css-flexbox):

### CSS Flex Container

The CSS flex container is also known as the `parent element`. Therefore, for us to create a Flexbox, we need the parent element with `display: flex` set on it. The flex layout allows the container to change the width or height of the item to establish order and fill the best available space, primarily to accommodate all display devices and screen sizes. Simply put, the flex container expands items to fill any open space, or shrinks the space to prevent overflow.

### CSS Flex Items

The direct children of the parent element (flex container) are `flex items`, also called `child elements`. The flex items appear in a row and start from the main axis' starting edge.

Let’s look at an example of [how the CSS flex container and items work](https://code.pieces.app/blog/learn-to-implement-css-container-queries):

```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  background-color: Purple;
  width: 800px;
}

.flex-container > div {
  background-color: Orange;
  margin: 30px;
  padding: 20px;
  font-size: 20px;
}
</style>
</head>
<body>

<h1>Flex Container</h1>

<p>Four Flex Items contained in a Flex Container</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=b4dd4a8177)

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image13_e538a0f49aa9c95d1460ad1717933c42.png" alt="Four boxes in a flex container."/></figure>

The flex items are the flexible boxes (in brown, blue, yellow, and red) inside the purple flex container.

## Flex Container Properties

The [CSS flex container](https://www.simplilearn.com/tutorials/css-tutorial/css-flexbox) has several properties that help explain its role in building layouts on our web pages. We’ll discuss these flexbox properties below.

### Flex Direction

This CSS flex property establishes how we place flex items in flex containers by determining the direction of the main axis. The flex direction is set to a row by default. However, if we change it to a flex column, the main axis changes and displays the item in a column. The following values are possible with the `flex-direction` property:

- `row value`: This value arranges the flex items horizontally, from left to right. Here is an example of how row value works:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-direction: row;
  background-color: Purple;
  width: 800px;
}

.flex-container > div {
  background-color: Orange;
  width: 50px;
  margin: 20px;
  padding: 20px;
  font-size: 20px;
}
</style>
</head>
<body>

<h1>Flex Direction</h1>

<p>Flex-direction row value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=430d4ebd48)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image18_f6112bdf8cb6e593aff53f85dea82323.png" alt="Four boxes inside a larger box."/></figure>

- `row-reverse value`**:** This value arranges the flex items horizontally, but from right to left. Let’s see how this works with this example:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-direction: row-reverse;
  background-color: Purple;
  width: 800px;
}

.flex-container > div {
  background-color: Orange;
  width: 50px;
  margin: 20px;
  padding: 20px;
  font-size: 20px;
}
</style>
</head>
<body>

<h1>Flex Direction</h1>

<p>Flex-direction row-reverse value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=1f7c4f8fcd)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image28_95b716bfb584f136ca91865e4f8514a3.png" alt="Four boxes aligned to the right in a larger box."/></figure>

- `column value`**:** This value arranges the flex items vertically, from top to bottom. Here is an example of column value:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-direction: column;
  background-color: Purple;
  width: 800px;
}

.flex-container > div {
  background-color: Orange;
  width: 50px;
  margin: 20px;
  padding: 20px;
  font-size: 20px;
}
</style>
</head>
<body>

<h1>Flex Direction</h1>

<p>Flex-direction column value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=2f234cb6de)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image11_183f1366e9e6404b269a071a42b9ed88.png" alt="Four boxes aligned vertically in a larger box."/></figure>

- `column-reverse value`**:** This value arranges the flex items vertically, but from bottom to top. Here is an example, below:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-direction: column-reverse;
  background-color: Purple;
  width: 800px;
}

.flex-container > div {
  background-color: Orange;
  width: 50px;
  margin: 20px;
  padding: 20px;
  font-size: 20px;
}
</style>
</head>
<body>

<h1>Flex Direction</h1>

<p>Flex-direction column-reverse value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=d4f04bbdd8)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image10_7e321112d37d1864efc79b5db9376751.png" alt="Four boxes aligned vertically in a larger box."/></figure>

### Flex-wrap

This Flex property defines whether or not a flex item should wrap in the case of insufficient space on one flex line.

To understand `flex-wrap`, let’s look at the different values for this flex property:

- `nowrap value`**:** This is the default setting of flex-wrap. It defines that the flex items will not wrap, as shown below:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: nowrap;
  background-color: Purple;
  width: 800px;
}

.flex-container > div {
  background-color: Orange;
  width: 50px;
  margin: 20px;
  padding: 20px;
  font-size: 20px;
}
</style>
</head>
<body>

<h1>Flex Wrap</h1>

<p>Flex-wrap nowrap value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=809c45a131)The outcome: 

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image29_6c3067e25636d2e23e84a24e38e5daca.png" alt="Four boxes inside a larger box."/></figure>

- `wrap value`**:** This defines that the flex items will wrap if needed:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: wrap;
  background-color: Purple;
  width: 200px;
}

.flex-container > div {
  background-color: Orange;
  width: 50px;
  font-size: 20px;
height: 50px;
}
</style>
</head>
<body>

<h1>Flex Wrap</h1>

<p>Flex-wrap wrap value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=599741a475)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image14_3ead0e1c61307838e8c62242153b411a.png" alt="Four boxes with no space between them."/></figure>

- `wrap-reverse value`**:** This defines that the flex items will wrap in the reverse order if necessary, as shown below:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: wrap-reverse;
  background-color: Purple;
  width: 200px;
  height: 200px
}

.flex-container > div {
  background-color: Orange;
  width: 50px;
  font-size: 20px;
height: 50px;
}
</style>
</head>
<body>

<h1>Flex Wrap</h1>

<p>Flex-wrap wrap-reverse value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=6cb04491b4)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image19_06d4d1acb31dab2758a0f8ba43018c61.png" alt="Four boxes with no space between them at the bottom of a box."/></figure>

### Justify Content

This CSS flex property helps to align the flex items based on the main axis within a Flex container. This occurs when the flex items do not use all open space on the main axis, thereby aligning horizontally. In addition, the property allows you to adjust to the left, center, right or add space between items.

Several values can explain the CSS justify-content property, including the following:

- `flex-start value`**:** This is the default value of the CSS justify-content property, and it shows the alignment of the flex items at the beginning of the container, as shown below:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  justify-content: flex-start;
  background-color: Purple;
  width: 800px;

}

.flex-container > div {
  background-color: Orange;
  width: 60px;
  font-size: 20px;
  height: 50px;
  padding: 20px;
  margin: 20px;
}
</style>
</head>
<body>

<h1>Justify Content</h1>

<p>Justify-content flex-start value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=99f8479d6e)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image15_92557969e15a71235ef7cf0abdb2d040.png" alt="Four boxes in a larger box."/></figure>

- `flex-end value`**:** This value shows the alignment of the flex items at the end of the container, like this:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  justify-content: flex-end;
  background-color: Purple;
  width: 800px;

}

.flex-container > div {
  background-color: Orange;
  width: 60px;
  font-size: 20px;
  height: 50px;
  padding: 20px;
  margin: 20px
}
</style>
</head>
<body>

<h1>Justify Content</h1>

<p>Justify-content flex-end value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=08f04a8abd)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image7_74f40571c34c969ddcd77374ff4d28aa.png" alt="Four boxes aligned to the right in a larger box."/></figure>

- `center value`**:** This value aligns the flex items at the center of the container:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  justify-content: center;
  background-color: Purple;
  width: 800px;

}

.flex-container > div {
  background-color: Orange;
  width: 60px;
  font-size: 20px;
  height: 50px;
  padding: 20px;
  margin: 20px;
}
</style>
</head>
<body>

<h1>Justify Content</h1>

<p>Justify-content center value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=e40f43b62d)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image12_acaeb215d866e0644b620b020f253977.png" alt="Four boxes aligned in the center of a larger box."/></figure>

- `space-around value`**:** This value shows the flex items with space before, between, and after the lines, as seen in this example:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  justify-content: space-around;
  background-color: Purple;
  width: 800px;
}

.flex-container > div {
`  background-color: Orange;
  width: 60px;
  font-size: 20px;
  height: 50px;
  padding: 20px;
  margin: 20px
}
</style>
</head>
<body>

<h1>Justify Content</h1>

<p>Justify-content space-around value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=de7a40ad6c)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image20_8a710c5df1ec2088d15d9eeecd3df2b2.png" alt="Four boxes aligned evenly throughout a larger box."/></figure>

- `space-between value`**:** This value shows the flex items with space between the lines:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  justify-content: space-between;
  background-color: Purple;
  width: 800px;

}

.flex-container > div {
  background-color: Orange;
  width: 60px;
  font-size: 20px;
  height: 50px;
  padding: 20px;
  margin: 20px
}
</style>
</head>
<body>

<h1>Justify Content</h1>

<p>Justify-content space-between value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=02214f9f78)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image8_2c37a829b1531c4553806bb5e8108875.png" alt="Four boxes spaced evenly throughout a larger box."/></figure>

### Align Items

The `align-items` property aligns the flex items vertically due to the item's inability to use all available space on the cross-axis. Let’s check out examples of different values of the CSS align-item property in order to understand it better:

- `stretch value`**:** This shows the default value of the CSS align-items property, and it fills the flex container by stretching the flex items, as we can see below:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  align-items: stretch;
  background-color: Purple;
  height: 300px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
}
</style>
</head>
<body>

<h1>Align items</h1>

<p>Align-items stretch value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=bd764998ec)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image16_ab3418b8c95c7fd41703fb73422f775d.png" alt="Four boxes without space between them that stretch the entire vertical space of a larger box."/></figure>

- `center value`**:** This value helps to align the flex items in the middle of the container, like this:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  align-items: center;
  background-color: Purple;
  height: 300px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  height: 50px;
  padding: 20px;
  margin: 10px
}
</style>
</head>
<body>

<h1>Align items</h1>

<p>Align-items center value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=07ef448ba9)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image21_d43382c6615bf1013ff56b02b7b4ca49.png" alt="Four boxes aligned in the middle of a larger box."/></figure>

- `flex-start value`**:** This value helps to align the flex items at the top of the container:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  align-items: flex-start;
  background-color: Purple;
  width: 800px;
  height: 300px;
}

.flex-container > div {
  background-color: Orange;
  width: 60px;
  font-size: 20px;
  height: 50px;
  padding: 20px;
  margin: 20px
}
</style>
</head>
<body>

<h1>Align items</h1>

<p>Align-items Flex-start value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=4552489294)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image3_33263857ed9e5aea6dba5378ee2e7582.png" alt="Four boxes aligned at the top of a larger box."/></figure>

- `flex-end value`**:** This value helps to align the flex items at the bottom of the container, as shown here:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  align-items: flex-end;
  background-color: Purple;
  width: 800px;
  height: 300px;
}

.flex-container > div {
  background-color: Orange;
  width: 60px;
  font-size: 20px;
  height: 50px;
  padding: 20px;
  margin: 20px
}
</style>
</head>
<body>

<h1>Align items</h1>

<p>Align-items Flex-end value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code ](https://queendarlinnna.pieces.cloud/?p=0c6e44a0d7) The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image1_fc7506a030854c91c78b6bd3b2c4a648.png" alt="Four boxes aligned at the bottom of a larger box."/></figure>

### Align Content

The CSS align-content property helps to align the flex lines by applying flex-wrap; it wraps if there are many flex lines. This is similar to align-items, but it aligns lines instead of aligning flex items. The CSS align-content property can have the following values:

- `stretch value`**:** This value shows the CSS align-content property's default value and involves stretching the flex lines to take up the available space, as in the example below:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: wrap;
  align-content: stretch;
  background-color: Purple;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  height: 200px;
  margin: 20px;

}
</style>
</head>
<body>

<h1>Align content</h1>

<p>Align-content stretch value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=c2384ab691)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image16_afab4c6e4b156f2524ce78c602a7fae4.png" alt="Four boxes that stretch to fill all of the vertical space of a larger box."/></figure>

- `space-between value`**:** This value shows equal space between flex lines:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
  background-color: Purple;
  width: 200px;
  height: 300px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  margin: 10px;
  height: 80px;

}
</style>
</head>
<body>

<h1>Align content</h1>

<p>Align-content space-between value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>

</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=33684ea2f8)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image6_193759b437592017b867c798782638e4.png" alt="Four boxes, two at the top of a larger box and two at the bottom."/></figure>

- `space-around value`**:** This value shows space before, between, and after the flex lines. We can see this in the example below:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: wrap;
  align-content: space-around;
  background-color: Purple;
  width: 200px;
  height: 300px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  margin: 10px;
  height: 80px;

}
</style>
</head>
<body>

<h1>Align content</h1>

<p>Align-content space-around value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=c4804196b0)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image5_8d70f0a9202fa5e3a63266347467673e.png" alt="Four boxes evenly spaced throughout a larger box."/></figure>

- `center value`**:** This value shows the flex lines in the middle of the container:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  background-color: Purple;
  width: 400px;
  height: 300px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  margin: 10px;
  height: 80px;

}
</style>
</head>
<body>

<h1>Align content</h1>

<p>Align-content center value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=2da3458e1f)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image24_8c05bc2f5c75ef7121dd0b8e6ec9ff7d.png" alt="Four boxes centered in the vertical space of a larger box."/></figure>

- `flex-start value`**:** This value shows the flex lines at the beginning of the container, as shown below:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  background-color: Purple;
  width: 400px;
  height: 300px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  margin: 10px;
  height: 80px;

}
</style>
</head>
<body>

<h1>Align content</h1>

<p>Align-content flex-start value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=0d5442980e)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image9_a65b8bce6086ecb4810363af1add8722.png" alt="Four boxes aligned at the top of a larger box."/></figure>

- `flex-end value`**:** This value shows the flex lines at the end of the container. Here is an example:


```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container	 {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-end;
  background-color: Purple;
  width: 400px;
  height: 300px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  margin: 10px;
  height: 80px;

}
</style>
</head>
<body>

<h1>Align content</h1>

<p>Align-content flex-end value</p>

<div class="flex-container">
  <div style="background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style="background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code
](https://queendarlinnna.pieces.cloud/?p=333a46a717)The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image17_27c998ac17560753480fcee4633f3917.png" alt="Four boxes aligned at the bottom of a larger box."/></figure>

## Flex Item Properties

The CSS flex item has many properties that help explain how flex items are arranged to build layouts. We’ll break down these properties below.

### Order Property

The `order-flex` property defines the order of the flex items in the same container. The order value must be a number, and there is a preset value of 0. By default, all flex items are arranged in the order of document flow, and when we apply the order property, we do not need to change the HTML structure.

For example, the order property allows us to change the order of the flex items:

```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  align-items: stretch;
  background-color: Purple;
  height: 150px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  margin: 20px;
  height: 60px;
}
</style>
</head>
<body>

<h1>Flex Item Property</h1>

<h3>Order property</h3>

<div class="flex-container">
  <div style="background-color: Orange; order: 3">Box 1</div>
  <div style="background-color: blue; order: 4">Box 2</div>
  <div style="background-color: yellow; order 2">Box 3</div>
  <div style="background-color: red; order: 1">Box 4</div>
</div>


</body>
</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=cdbd4c9c59)

The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image26_d47b1ff99a77db5cc2a6be77a152dca1.png" alt="Four boxes; the second box is much longer than the others."/></figure>

### Align-self Property

The align-self property defines the alignment for the selected item inside the flexible container. This property overrides the default alignment set by the container's align-items property, as we can see in this example:

```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  align-items: stretch;
  background-color: Purple;
  height: 200px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  margin: 20px;
  height: 60px;
}
</style>
</head>
<body>

<h1>Flex Item Property</h1>

<h3>Align-self property</h3>

<div class="flex-container">
  <div style=" align-self: center; background-color: Orange">Box 1</div>
  <div style="background-color: blue">Box 2</div>
  <div style="background-color: yellow">Box 3</div>
  <div style=" align-self: center; background-color: red">Box 4</div>
</div>


</body>
</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=e93242869f)

The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image2_531536cb5734bf483c3f9e71d7dbe944.png" alt="Four boxes inside a larger box, each aligned differently in vertical space."/></figure>

### Flex-grow Property

The flex-grow property defines the rate a flex item will grow compared to the rest of the flex items in the same container. The value must also be a number with a default value of 0. Flex-grow value of 0 means the flex item's width as per content only. A value of 1 means the flex item's width distribution will be equal:

```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  align-items: stretch;
  background-color: Purple;
  height: 150px;
}

.flex-container > div {
  background-color: Orange;
  width: 80px;
  font-size: 20px;
  margin: 20px;
  height: 60px;
}
</style>
</head>
<body>

<h1>Flex Item Property</h1>

<h3>Flex grow property</h3>

<div class="flex-container">
  <div style="background-color: Orange; flex-grow: 1">Box 1</div>
  <div style="background-color: blue; flex-grow: 4">Box 2</div>
  <div style="background-color: yellow; flex-grow: 1">Box 3</div>
  <div style="background-color: red; flex-grow: 1">Box 4</div>
</div>


</body>
</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=11f14d9932)

The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image23_9d71b70adc5be01298ef5c8f2b23e598.png" alt="Four boxes in a larger box; the second is much longer than the other three."/></figure>

### Flex-shrink

The flex-shrink property defines the rate a flex item will shrink compared to the other flex items in the same flex container. The value must be a number with a default value of 1. We can see how this looks below:

```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
        width: 350px;
        height: 150px;
        border: 1px solid black;
        background-color: Purple;
        display: flex;
      }

.flex-container>div {
  background-color: brown;
  width: 80px;
  font-size: 20px;
  margin: 20px;
  height: 60px;
  }

  .flex-container div:nth-of-type(2) {
          flex-shrink: 4;
        }
        .flex-container div:nth-of-type(3) {
               flex-shrink: 3;
             }
</style>
</head>
<body>

<h1>Flex Item Property</h1>

<h3>Flex shrink property</h3>

<div class="flex-container">
  <div class="box 1" style="background-color: Orange;">Box 1</div>
  <div class="box 2" style="background-color: blue;">Box 2</div>
  <div class="box 3" style="background-color: yellow;">Box 3</div>
  <div class="box 4" style="background-color: red;">Box 4</div>
</div>


</body>
</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=56f54ea6bc)

The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image22_4183223714665d4f1f66e770db47086f.png" alt="Four boxes within a larger box; the second and third are much smaller than the first and fourth."/></figure>

### Flex-basis

The flex-basis property defines the initial length or width of a flex item. The default value of the flex-basis property is auto. In addition, for the percentage we need, we’ll assign flex-shrink. For example, 30% means the width of the item will be 30%, and the sum of the widths of other items will be 70%, as shown below:

```
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
        display: flex;
        align-items: stretch;
        background-color: Purple;
      }

.flex-container>div {
  background-color: brown;
  width: 80px;
  font-size: 20px;
  margin: 20px;
  height: 60px;
  }

</style>
</head>
<body>

<h1>Flex Item Property</h1>

<h3>Flex basis property</h3>

<div class="flex-container">
  <div class="box 1" style="background-color: Orange;">Box 1</div>
  <div class="box 2" style="flex-basis: 300px; background-color: blue;">Box 2</div>
  <div class="box 3" style="background-color: yellow;">Box 3</div>
  <div class="box 4" style="background-color: red;">Box 4</div>
</div>


</body>
</html>
```

[Save this code](https://queendarlinnna.pieces.cloud/?p=ead74593ee)

The outcome:

<figure><img src="https://d37oebn0w9ir6a.cloudfront.net/account_32099/image26_3de1a4e3a9b2c00a55c9573b4d74d17d.png" alt="Four boxes inside a larger box; the second is much longer than the others."/></figure>

## Conclusion

CSS Flexbox allows us to build layouts on our web pages in many spectacular and flexible ways. In this article, you’ve learned a variety of ways that you can create layouts using this CSS flexbox cheat sheet. So, flex your development muscles with all these flexible ways of [building responsive layouts on your websites](https://code.pieces.app/blog/foundation-the-best-framework-for-building-responsive-sites)!