Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Tailwind CSS Flexbox and Grid Layout

Tailwind CSS with Flexbox and Grid Layout

Tailwind CSS provides powerful utility classes to manage layouts, especially Flexbox and Grid.

Flexbox and Grid utility classes let you create responsive, flexible layouts very efficiently without writing custom CSS.

Tailwind CSS's philosophy is to control layout through low-level utility classes (utility-first), flexibly combining classes to meet different design needs.


1. Flexbox Layout

Flexbox is a layout mode that helps developers create one-dimensional (horizontal or vertical) layouts.

Tailwind CSS provides several concise utility classes to control all aspects of Flexbox layout, including direction, alignment, flexing, and more.

Set Up a Flex Container

To create a Flexbox layout, first set the element as a flex container.

Example

 

Item 1

 

Item 2

 

Item 3

Try it »

flex sets an element's display to flex, enabling Flexbox layout.

Utility classes for flex item initial sizes and their corresponding CSS properties:

Tailwind Class Name CSS Property and Value Description
basis-0 flex-basis:0px; Set the flex item's base size to 0px.
basis-1 flex-basis:0.25rem; Set the flex item's base size to 4px.
basis-2 flex-basis:0.5rem; Set the flex item's base size to 8px.
basis-3 flex-basis:0.75rem; Set the flex item's base size to 12px.
basis-4 flex-basis:1rem; Set the flex item's base size to 16px.
basis-5 flex-basis:1.25rem; Set the flex item's base size to 20px.
basis-6 flex-basis:1.5rem; Set the flex item's base size to 24px.
basis-7 flex-basis:1.75rem; Set the flex item's base size to 28px.
basis-8 flex-basis:2rem; Set the flex item's base size to 32px.
basis-9 flex-basis:2.25rem; Set the flex item's base size to 36px.
basis-10 flex-basis:2.5rem; Set the flex item's base size to 40px.
basis-11 flex-basis:2.75rem; Set the flex item's base size to 44px.
basis-12 flex-basis:3rem; Set the flex item's base size to 48px.
basis-14 flex-basis:3.5rem; Set the flex item's base size to 56px.
basis-16 flex-basis:4rem; Set the flex item's base size to 64px.
basis-20 flex-basis:5rem; Set the flex item's base size to 80px.
basis-24 flex-basis:6rem; Set the flex item's base size to 96px.
basis-28 flex-basis:7rem; Set the flex item's base size to 112px.
basis-32 flex-basis:8rem; Set the flex item's base size to 128px.
basis-36 flex-basis:9rem; Set the flex item's base size to 144px.
basis-40 flex-basis:10rem; Set the flex item's base size to 160px.
basis-44 flex-basis:11rem; Set the flex item's base size to 176px.
basis-48 flex-basis:12rem; Set the flex item's base size to 192px.
basis-52 flex-basis:13rem; Set the flex item's base size to 208px.
basis-56 flex-basis:14rem; Set the flex item's base size to 224px.
basis-60 flex-basis:15rem; Set the flex item's base size to 240px.
basis-64 flex-basis:16rem; Set the flex item's base size to 256px.
basis-72 flex-basis:18rem; Set the flex item's base size to 288px.
basis-80 flex-basis:20rem; Set the flex item's base size to 320px.
basis-96 flex-basis:24rem; Set the flex item's base size to 384px.
basis-auto flex-basis:auto; Set the flex item's base size to auto.
basis-px flex-basis:1px; Set the flex item's base size to 1px.
basis-0.5 flex-basis:0.125rem; Set the flex item's base size to 2px.
basis-1.5 flex-basis:0.375rem; Set the flex item's base size to 6px.
basis-2.5 flex-basis:0.625rem; Set the flex item's base size to 10px.
basis-3.5 flex-basis:0.875rem; Set the base size of the flex item to 14px.
basis-1/2 flex-basis:50%; Set the base size of the flex item to half the width of the container.
basis-1/3 flex-basis:33.333333%; Set the base size of the flex item to one third of the container width.
basis-2/3 flex-basis:66.666667%; Set the base size of the flex item to two thirds of the container width.
basis-1/4 flex-basis:25%; Set the base size of the flex item to one quarter of the container width.
basis-2/4 flex-basis:50%; Set the base size of the flex item to one half of the container width.
basis-3/4 flex-basis:75%; Set the base size of the flex item to three quarters of the container width.
basis-1/5 flex-basis:20%; Set the base size of the flex item to one fifth of the container width.
basis-2/5 flex-basis:40%; Set the base size of the flex item to two fifths of the container width.
basis-3/5 flex-basis:60%; Set the base size of the flex item to three fifths of the container width.
basis-4/5 flex-basis:80%; Set the base size of the flex item to four fifths of the container width.
basis-1/6 flex-basis:16.666667%; Set the base size of the flex item to one sixth of the container width.
basis-2/6 flex-basis:33.333333%; Set the base size of the flex item to two sixths of the container width.
basis-3/6 flex-basis:50%; Set the base size of the flex item to two thirds of the container width.
basis-4/6 flex-basis:66.666667%; Set the base size of the flex item to four sixths of the container width.
basis-5/6 flex-basis:83.333333%; Set the base size of the flex item to five sixths of the container width.
basis-1/12 flex-basis:8.333333%; Set the base size of the flex item to 8.33%.
basis-2/12 flex-basis:16.666667%; Set the base size of the flex item to 16.67%.
basis-3/12 flex-basis:25%; Set the base size of the flex item to 25%.
basis-4/12 flex-basis:33.333333%; Set the base size of the flex item to 33.33%.
basis-5/12 flex-basis:41.666667%; Set the base size of the flex item to 41.67%.
basis-6/12 flex-basis:50%; Set the base size of the flex item to 50%.
basis-7/12 flex-basis:58.333333%; Set the base size of the flex item to 58.33%.
basis-8/12 flex-basis:66.666667%; Set the base size of the flex item to 66.67%.
basis-9/12 flex-basis:75%; Set the base size of the flex item to 75%.
basis-10/12 flex-basis:83.333333%; Set the base size of the flex item to 83.33%.
basis-11/12 flex-basis:91.666667%; Set the base size of the flex item to 91.67%.
basis-full flex-basis:100%; Set the base size of the flex item to 100%.

Example

 

01

 

02

 

03

Try it »

Flex Direction

flex-direction controls the direction of the main axis (that is, the direction in which items are arranged inside the Flex container).

Tailwind provides the following utility classes to set the direction:

Example

 

01

 

02

 

03

 

01

 

02

 

03

 

01

 

02

 

03

Try it »

Flex Wrap

By default, items in a Flexbox container are arranged in a single line.

If you want them to wrap, you can use the flex-wrap utility classes.

Example

 

01

 

02

 

03

 

01

 

02

 

03

 

01

 

02

 

03

Try it »

Flexibility of Flex Items

Flexbox allows elements to grow or shrink according to the available space.

Tailwind CSS provides the following utility classes to control flexibility:

Tailwind Class Name CSS Property and Value Description
flex-1 flex: 1 1 0%; Give a flex item equal flex-grow and flex-shrink ability, with a base size of 0%; commonly used to create flex layouts with equal widths or heights.
flex-auto flex: 1 1 auto; Give a flex item an automatic base size and equal flex-grow and flex-shrink ability; commonly used to let flex items fill extra space.
flex-initial flex: 0 1 auto; Give a flex item an automatic base size; it does not grow but can shrink, used to keep the element's natural size unless space is insufficient.
flex-none flex: none; Prevent a flex item from growing or shrinking so that it keeps its base size; commonly used to fix an element's size.

Example

 

    01

 

 

    02

 

 

    03

 

 

    01

 

 

    02

 

 

    03

 

Try it »

flex-grow Property

The following are utility classes for controlling the flex-grow property of flex items and their corresponding CSS properties:

Tailwind Class Name CSS Property and Value Description
grow flex-grow: 1; Allow a flex item to grow to fill extra space, with a grow factor of 1.
grow-0 flex-grow: 0; Prevent a flex item from growing to fill extra space, with a grow factor of 0.

Example

 

    01

 

 

    02

 

 

    03

 

Try it »

flex-shrink Property

The following are utility classes for controlling the flex-shrink property of flex items and their corresponding CSS properties:

Tailwind Class Name CSS Property and Value Description
shrink flex-shrink: 1; Allow a flex item to shrink when necessary to fit the flex container, with a shrink factor of 1.
shrink-0 flex-shrink: 0; Prevent a flex item from shrinking when necessary to fit the flex container, with a shrink factor of 0.

Example

 

    01

 

 

    02

 

 

    03

 

Try it »


Sorting

The following are utility classes for controlling the arrangement order of flex items or grid items and their corresponding CSS properties:

Tailwind Class Name CSS Property and Value Description
order-1 order: 1; Set the arrangement order of a flex item or grid item to 1.
order-2 order: 2; Set the arrangement order of a flex item or grid item to 2.
order-3 order: 3; Set the arrangement order of a flex item or grid item to 3.
order-4 order: 4; Set the arrangement order of a flex item or grid item to 4.
order-5 order: 5; Set the arrangement order of a flex item or grid item to 5.
order-6 order: 6; Set the arrangement order of a flex item or grid item to 6.
order-7 order: 7; Set the arrangement order of a flex item or grid item to 7.
order-8 order: 8; Set the arrangement order of a flex item or grid item to 8.
order-9 order: 9; Set the arrangement order of a flex item or grid item to 9.
order-10 order: 10; Set the arrangement order of a flex item or grid item to 10.
order-11 order: 11; Set the arrangement order of a flex item or grid item to 11.
order-12 order: 12; Set the arrangement order of a flex item or grid item to 12.
order-first order: -9999; Set the arrangement order of a flex item or grid item to the front.
order-last order: 9999; Set the arrangement order of a flex item or grid item to the last.
order-none order: 0; Set the arrangement order of a flex item or grid item to the default order, that is, do not change its order.

Example

 

01

 

02

 

03

Try it »


Grid Layout

Grid is another layout model that allows you to precisely position elements in rows and columns, and makes it easy to create complex layouts.

Tailwind CSS provides powerful utility classes to work with grid layouts.

The following are utility classes in Tailwind CSS for specifying the number of columns in a grid layout and their corresponding CSS properties:

Tailwind Class Name CSS Property and Value Description
grid-cols-1 grid-template-columns: repeat(1, minmax(0, 1fr)); Create a grid layout with 1 column.
grid-cols-2 grid-template-columns: repeat(2, minmax(0, 1fr)); Create a grid layout with 2 columns.
grid-cols-3 grid-template-columns: repeat(3, minmax(0, 1fr)); Create a grid layout with 3 columns.
grid-cols-4 grid-template-columns: repeat(4, minmax(0, 1fr)); Create a grid layout with 4 columns.
grid-cols-5 grid-template-columns: repeat(5, minmax(0, 1fr)); Create a grid layout with 5 columns.
grid-cols-6 grid-template-columns: repeat(6, minmax(0, 1fr)); Create a grid layout with 6 columns.
grid-cols-7 grid-template-columns: repeat(7, minmax(0, 1fr)); Create a grid layout with 7 columns.
grid-cols-8 grid-template-columns: repeat(8, minmax(0, 1fr)); Create a grid layout with 8 columns.
grid-cols-9 grid-template-columns: repeat(9, minmax(0, 1fr)); Create a grid layout with 9 columns.
grid-cols-10 grid-template-columns: repeat(10, minmax(0, 1fr)); Create a grid layout with 10 columns.
grid-cols-11 grid-template-columns: repeat(11, minmax(0, 1fr)); Create a grid layout with 11 columns.
grid-cols-12 grid-template-columns: repeat(12, minmax(0, 1fr)); Create a grid layout with 12 columns.
grid-cols-none grid-template-columns: none; Do not create grid columns; child elements will not use grid layout.
grid-cols-subgrid grid-template-columns: subgrid; Use subgrid layout; child elements will inherit the column definitions of the parent grid.

Example

 

01

 

 

09

Try it »

Grid Rows and Columns

Pass grid-template-columns and grid-template-rows Control the width and height of columns and rows. Tailwind provides the following utility classes to control them:

Example

 

01

 

 

05

 

   

06

 

Try it »

Example

 

01

 

02

 

03

 

04

 

05

 

06

 

07

Try it »

Example

 

01

 

02

 

03

Try it »

Positioning of Grid Items

grid-column and grid-row control the position of each item in the grid.

You can specify via the following utility classes:

Example

 

01

 

02

 

03

 

04

Try it »

Example

 

01

 

02

 

03

Try it »


Alignment and Distribution

Similar to Flexbox, Grid also provides options for alignment and distribution.

The justify-content property is used to align child elements inside the container along the main axis (horizontal direction).

Tailwind Class Name CSS Property and Value Description
justify-normal justify-content: normal; Using the default alignment, usually equivalent to start。
justify-start justify-content: flex-start; moving all child elements to the start of the container. In a horizontal layout, child elements align to the left.
justify-end justify-content: flex-end; moving all child elements to the end of the container. In a horizontal layout, child elements align to the right.
justify-center justify-content: center; Center-align all child elements.
justify-between justify-content: space-between; Distribute the space between child elements equally, with no gaps at either end.
justify-around justify-content: space-around; Distribute the space between child elements equally, with half-sized gaps at either end.
justify-evenly justify-content: space-evenly; Distribute the space between child elements completely equally, including the gaps at both ends.
justify-stretch justify-content: stretch; Child elements are stretched to fill the container, with each child occupying the same amount of space.

Example

 

01

 

02

 

03

Try it »

Other alignment settings:

Example

 

01

 

02

 

03

 

04

 

05

 

06

Try it »