Tutorial: CSS Flexbox
Published: 2021-07-31 14:28:43

---
title: Tutorial: CSS Flexbox
published: true
date: 2021-04-20 01:16:04 UTC
tags: 
canonical_url: https://www.devwares.com/blog//css-flexbox
series: Devwares Web development Free Course
---

## CSS Flexbox
![cover image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utzr7xss77jfqr14f1t2.png)

CSS Flexbox, or the Flexible Box Layout, is a layout model that allows responsive elements within a container to be automatically arranged depending upon screen size or view. This model provides an efficient way to layout, [align](https://www.devwares.com/tailwindcss/classes/tailwind-align-self/), and distribute space among items in a container, even when their sizes are unknown or dynamic.

## Basic Concepts

There are two fundamental concepts for Flexbox:

**1. The Container**: The container is the parent element in which flex items are enclosed. To use Flexbox, you need to set the [display](https://www.devwares.com/tailwindcss/classes/tailwind-display/) property of the container to flex or inline-flex.

```css
.container {  display: flex;}
```

**2. The Items**: The items are the children elements contained in the flex container. Flex properties are applied to these items.

## Main Properties

Some of the main properties of the Flexbox layout include:

**1.[flex-direction](https://www.devwares.com/tailwindcss/classes/tailwind-flex-direction/)**: This property determines in which direction the container’s items will be arranged. The possible values are: row, row-reverse, column, column-reverse. For instance, flex-direction: row; will arrange items from left to right.

```css
.container {  display: flex;  flex-direction: row;}
```

**2.[justify-content](https://www.devwares.com/tailwindcss/classes/tailwind-justify-content/)**: This property defines how the browser distributes space between and around content [items](https://www.devwares.com/tailwindcss/classes/tailwind-justify-items/) along the main axis of a flex container. Possible values are: flex-start, flex-end, center, space-between, space-around, space-evenly. For instance, justify-content: center; will center the items.

```css
.container {  display: flex;  justify-content: center;}
```

**3.align-items**: This property sets the default alignment for [items](https://www.devwares.com/tailwindcss/classes/tailwind-align-items/) inside the flex container. It works similar to justify-content but in the perpendicular direction. Possible values are: flex-start, flex-end, center, baseline, stretch. For instance, align-items: center; will center the items.

```css
.container {  display: flex;  align-items: center;}
```


**4.[flex-wrap](https://www.devwares.com/tailwindcss/classes/tailwind-flex-wrap/)**: By default, flex items will all try to fit onto one line. This property allows the items to wrap as needed with this property. For instance, flex-wrap: wrap; allows the items to wrap onto multiple lines.


```css
.container {  display: flex;  flex-wrap: wrap;}
```

**5. flex-flow**: This is a shorthand property for setting both the flex-direction and flex-wrap properties. For instance, flex-flow: row wrap; will arrange items from left to right and allow them to wrap onto multiple lines.

```css
.container {  display: flex;  flex-flow: row wrap;}
```


**6.align-content**: This property aligns a flex container’s lines within when there is extra space in the cross-axis, similar to how [justify-content](https://www.devwares.com/tailwindcss/classes/tailwind-justify-self/) aligns individual items within the main-axis. For instance, align-content: space-between; will distribute the space evenly between the lines.

```css
.container {  display: flex;  align-content: space-between;}
```

## Flex Item Properties

Just as there are properties for the flex container, there are several properties that apply to the flex items. These include:

**1.[flex-grow](https://www.devwares.com/tailwindcss/classes/tailwind-flex-grow/)**: This property defines the ability for a flex item to grow if necessary. It accepts a unitless value that serves as a proportion. It dictates what amount of the available space inside the flex container the item should [fill](https://www.devwares.com/tailwindcss/classes/tailwind-fill/) up.

```css
.item {  flex-grow: 1;}
```


**2.[flex-shrink](https://www.devwares.com/tailwindcss/classes/tailwind-flex-shrink/)**: This property defines the ability for a flex item to shrink if necessary.

```css
.item {  flex-shrink: 2;}
```


**3. flex-basis**: This property sets the initial main size of the flex item. It sets the size of the content box unless otherwise set with box-sizing.

```css
.item {  flex-basis: 20%;}
```
**4. [flex](https://www.devwares.com/tailwindcss/classes/tailwind-flex/)**: This is a shorthand property for setting flex-grow, flex-shrink, and flex-basis at the same time.

```css
.item {  flex: 0 1 auto;}
```

**5.[align-self](https://www.devwares.com/tailwindcss/classes/tailwind-align-self/)**: This property allows an item to override the align-items value from its parent. For instance, align-self: flex-end; will align the item at the end of the container.


```css
.item {  align-self: flex-end;}
```

## Using Flexbox

Here's an example of how to use these properties in a real-world scenario:

```css
.container {  display: flex;  justify-content: space-between;}
.item {  flex-grow: 1;  flex-basis: calc(100% / 3);  margin: 0 1em;}
```

In this example, we've created a flex container with three items. The items will evenly distribute themselves with equal space around them. Each item will take up a third of the container's width, minus a little bit for margin.

Flexbox is a versatile tool that can significantly simplify your CSS. It's worth learning and using in your projects to make your layouts more flexible and intuitive

## Order Property

The [order](https://www.devwares.com/tailwindcss/classes/tailwind-order/) property can be applied to individual flex items. This property takes an integer value and determines the order in which the flex items appear within the flex container. The default value is 0.

```css
.item {  order: 2;}
```

## Flexibility

The flex property is a shorthand for setting the flex-grow, flex-shrink, and flex-basis properties at the same time. The flex property may have one, two, or three values. A value of auto for flex is equivalent to 1 1 auto. A value of none for flex is equivalent to 0 0 auto.

```css
.item {  flex: 1 0 20%;}
```

## Responsive Design with Flexbox

One of the major advantages of Flexbox is its ability to create responsive designs without relying on media queries.

For example, by setting the flex-wrap property to wrap, the flex container becomes a multi-line flex container. This means that if there isn't enough space for all items to fit on one line, the items will automatically move to a new line.

```css
.container {  display: flex;  flex-wrap: wrap;}
```

In combination with the flex-grow, flex-shrink, and flex-basis properties, Flexbox provides a powerful toolset to create responsive designs that adapt to different screen sizes and orientations.


![Contrast](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yuz057uo4vkmvma7bdom.png)

Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc. project with a clean, prebuilt and well documented template and UI components. Learn more about [contrast](https://www.devwares.com/product/bootstrap-contrast-pro).

## Resources 
- [Contrast Design Bootstrap](https://www.devwares.com/docs/contrast/javascript/index/)
- [Accordion](https://www.devwares.com/docs/contrast/javascript/components/accordion/)
- [Alert](https://www.devwares.com/docs/contrast/javascript/components/alert/)
- [Animation](https://www.devwares.com/docs/contrast/javascript/components/animations/)
- [Autocomplete](https://www.devwares.com/docs/contrast/javascript/components/autocomplete/)
- [Badges](https://www.devwares.com/docs/contrast/javascript/components/badge/)
- [Box](https://www.devwares.com/docs/contrast/javascript/components/box/)
- [Breadcrumb](https://www.devwares.com/docs/contrast/javascript/components/breadcrumb/)
- [ButtonGroup](https://www.devwares.com/docs/contrast/javascript/components/buttonGroup/)
- [Button](https://www.devwares.com/docs/contrast/javascript/components/buttons/)
- [ButtonToolbar](https://www.devwares.com/docs/contrast/javascript/components/buttonToolbar/)
- [Card](https://www.devwares.com/docs/contrast/javascript/components/card/)
- [Carousel](https://www.devwares.com/docs/contrast/javascript/components/carousel/)
- [Checkbox](https://www.devwares.com/docs/contrast/javascript/components/checkbox/)
- [Collapse](https://www.devwares.com/docs/contrast/javascript/components/collapse/)
- [DataTable](https://www.devwares.com/docs/contrast/javascript/components/datatables/)
- [DatePicker](https://www.devwares.com/docs/contrast/javascript/components/datepicker/)
- [DropDown](https://www.devwares.com/docs/contrast/javascript/components/dropdown/)
- [Footer](https://www.devwares.com/docs/contrast/javascript/components/footer/)
- [Forms](https://www.devwares.com/docs/contrast/javascript/components/forms/)
- [Icon](https://www.devwares.com/docs/contrast/javascript/components/icon/)
- [Iframe](https://www.devwares.com/docs/contrast/javascript/components/iframe/)
- [Input](https://www.devwares.com/docs/contrast/javascript/components/input/)
- [InputGroup](https://www.devwares.com/docs/contrast/javascript/components/inputgroup/)
- [ListGroup](https://www.devwares.com/docs/contrast/javascript/components/listgroup/)
- [Mask](https://www.devwares.com/docs/contrast/javascript/components/mask/)
- [Modal](https://www.devwares.com/docs/contrast/javascript/components/modal/)
- [Multiselect](https://www.devwares.com/docs/contrast/javascript/components/multiselect/)
- [Notification](https://www.devwares.com/docs/contrast/javascript/components/notification/)
- [Pane](https://www.devwares.com/docs/contrast/javascript/components/pane/)
- [Panel](https://www.devwares.com/docs/contrast/javascript/components/panels/)
- [Pop Over](https://www.devwares.com/docs/contrast/javascript/components/popover/)
- [Progress](https://www.devwares.com/docs/contrast/javascript/components/progress/)
- [Radio](https://www.devwares.com/docs/contrast/javascript/components/radio/)
- [Rating](https://www.devwares.com/docs/contrast/javascript/components/rating/)
- [Select](https://www.devwares.com/docs/contrast/javascript/components/select/)
- [Select 2](https://www.devwares.com/docs/contrast/javascript/components/select2/)
- [Slider](https://www.devwares.com/docs/contrast/javascript/components/slider/)
- [Spinner](https://www.devwares.com/docs/contrast/javascript/components/spinner/)
- [Stepper](https://www.devwares.com/docs/contrast/javascript/components/stepper/)
- [Switch](https://www.devwares.com/docs/contrast/javascript/components/switch/)
- [Table](https://www.devwares.com/docs/contrast/javascript/components/table/)
- [Forms](https://www.devwares.com/docs/contrast/javascript/forms/)
- [Navbar](https://www.devwares.com/docs/contrast/javascript/navigation/navbar/)
- [Pagination](https://www.devwares.com/docs/contrast/javascript/navigation/pagination/)
- [Tab](https://www.devwares.com/docs/contrast/javascript/navigation/tabs/)
- [Sidebar](https://www.devwares.com/docs/contrast/javascript/navigation/sidebar/)
- [Time Picker](https://www.devwares.com/docs/contrast/javascript/sections/timepicker/)
- [Data Table](https://www.devwares.com/docs/contrast/javascript/table/datatables/)
- [Fixed Data Table](https://www.devwares.com/docs/contrast/javascript/table/fixed_datatables/)
- [Table](https://www.devwares.com/docs/contrast/javascript/table/table/)
- [Widgets](https://www.devwares.com/docs/contrast/javascript/widgets/)