Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Building Layouts with Grid Utilities | Layout Basics
Tailwind CSS for Web Development

book
Building Layouts with Grid Utilities

Another way of organizing website content is through grids. Tailwind CSS offers powerful utilities for creating grid layouts. This chapter will explain how to use Tailwind CSS grid utilities to establish grid containers and items.

Grid Container

To define a grid container, use the grid class. You can control the number of columns, the gaps between rows and columns, and more.

html

index.html

copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Grid Utilities - Example 1</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="grid grid-cols-3 gap-4">
<div class="bg-blue-500 p-4">Item 1</div>
<div class="bg-green-500 p-4">Item 2</div>
<div class="bg-red-500 p-4">Item 3</div>
</div>
</body>
</html>

Explanation

  1. grid : Sets the display of the element to grid;

  2. grid-cols-3 : Defines a grid with three columns;

  3. gap-4 : Adds a gap of 1rem (16px) between grid items.

Grid with Rows

html

index.html

copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Grid Utilities - Example 2</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="grid grid-cols-2 grid-rows-2 gap-4">
<div class="bg-blue-500 p-4">Item 1</div>
<div class="bg-green-500 p-4">Item 2</div>
<div class="bg-red-500 p-4">Item 3</div>
<div class="bg-yellow-500 p-4">Item 4</div>
</div>
</body>
</html>

grid-rows-2: Defines a grid with two rows.

Controlling Column and Row Sizes

You can control the sizes of columns and rows using grid-cols-{n} and grid-rows-{n} classes, where {n} represents the number of columns or rows. You can also use fractional units (fr), percentages, and other sizing units.

html

index.html

copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Grid Utilities - Example 3</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="grid grid-cols-3 gap-4">
<div class="col-span-2 bg-blue-500 p-4">Item 1</div>
<div class="bg-green-500 p-4">Item 2</div>
<div class="col-span-3 bg-red-500 p-4">Item 3</div>
</div>
</body>
</html>

Explanation

  1. col-span-2 : Spans the element across two columns;

  2. col-span-3 : Spans the element across all three columns.

Fixed and Flexible Sizes

html

index.html

copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Grid Utilities - Example 4</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="grid grid-cols-3 gap-4">
<div class="w-1/2 bg-blue-500 p-4">Item 1</div>
<div class="w-full bg-green-500 p-4">Item 2</div>
<div class="w-1/3 bg-red-500 p-4">Item 3</div>
</div>
</body>
</html>

Explanation

  1. w-1/2 : Sets the width of the element to 50% of its container;

  2. w-full : Sets the width of the element to 100% of its container;

  3. w-1/3 : Sets the width of the element to 33.33% of its container.

Grid Template Columns and Rows

You can use specific utility classes to define the number and width of columns and rows in your grid layout.

Custom Grid Columns

html

index.html

copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Grid Utilities - Example 5</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="grid grid-cols-4 gap-4">
<div class="col-span-1 bg-blue-500 p-4">1 / 4</div>
<div class="col-span-2 bg-green-500 p-4">2 / 4</div>
<div class="col-span-1 bg-red-500 p-4">1 / 4</div>
</div>
</body>
</html>

Explanation

  1. grid-cols-4 : Defines a grid with four columns;

  2. col-span-1 : Spans the element across one column;

  3. col-span-2 : Spans the element across two columns.

Custom Grid Rows

html

index.html

copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Grid Utilities - Example 6</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="grid grid-rows-3 gap-4">
<div class="row-span-2 bg-blue-500 p-4">2 / 3</div>
<div class="row-span-1 bg-green-500 p-4">1 / 3</div>
<div class="row-span-1 bg-red-500 p-4">1 / 3</div>
</div>
</body>
</html>

Explanation

  1. grid-rows-3 : Defines a grid with three rows;

  2. row-span-2 : Spans the element across two rows;

  3. row-span-1 : Spans the element across one row.

Note

Please refer to the official documentation for more information about the Tailwind CSS Grid.

1. Which class sets the display property to grid?

2. How do you define a grid with three columns?

question mark

Which class sets the display property to grid?

Select the correct answer

question mark

How do you define a grid with three columns?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 5

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt