iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😢

Using Grid Layout in Tailwind CSS

に公開
2020/09/26

Cheat Sheet

Official Docs - tailwindcss.com - grid

className css
.grid display: grid;
.grid-cols-{n} grid-template-columns: repeat({n}, minmax(0, 1fr));
.col-span-{n} grid-column: span {n} / span {n};
.col-start-{n} grid-column-start: {n};
.col-end-{n} grid-column-end: {n};
.gap-{n} gap: {0.25 * n}rem;
.col-gap-{n} column-gap: {0.25 * n}rem;
.grid-flow-row grid-auto-flow: row;
.grid-flow-col grid-auto-flow: column;
.grid-flow-row-dense grid-auto-flow: row dense;
.grid-flow-col-dense grid-auto-flow: column dense;

1. Replacing flex

Use .flex-row to arrange items horizontally.
When using flex, you need to specify the width, so here I used .w-1/5 to divide it into five equal parts.

const Flex = () => (
  <div className="h-screen flex flex-wrap flex-row">
    <div className="bg-red-100 w-1/5"></div>
    <div className="bg-red-200 w-1/5"></div>
    <div className="bg-red-300 w-1/5"></div>
    <div className="bg-red-400 w-1/5"></div>
    <div className="bg-red-500 w-1/5"></div>
  </div>
);

This is what it looks like when implementing the same layout with Grid.

.grid-cols-{n} generates a grid with n columns.
The items are divided into five equal parts cleanly even without specifying the size of the child elements.

const Grid = () => (
  <div className="h-screen grid grid-cols-5">
    <div className="bg-red-100"></div>
    <div className="bg-red-200"></div>
    <div className="bg-red-300"></div>
    <div className="bg-red-400"></div>
    <div className="bg-red-500"></div>
  </div>
);

I am using the Firefox Developer Edition browser to make the Grid layout easier to visualize.

2. Changing the width

You can declare {col | row}-span-{n} to make an element span n grid tracks.
Note that the n in grid-cols-{n} represents the number of grid divisions, not the number of horizontal elements.

const Grid = () => (
  <div className="h-screen grid grid-cols-5">
    <div className="bg-red-100"></div>
    <div className="bg-blue-200 col-span-2"></div> {/* Spans two columns */}
    <div className="bg-red-300"></div>
    <div className="bg-red-400"></div>
  </div>
);

3. Combining with rows

By default, as more elements are added, rows are automatically divided.

const Grid = () => (
  <div className="h-screen grid grid-cols-5">
    <div className="bg-red-100"></div>
    <div className="bg-blue-200 col-span-2"></div> {/* Spans two columns */}
    <div className="bg-red-300"></div>
    <div className="bg-red-400"></div>
    <div className="bg-red-500"></div>
  </div>
);

You can adjust the size of the rows by adding .grid-rows-{n} to the parent element.

const Grid = () => (
  <div className="h-screen grid grid-cols-5 grid-rows-3">
    {/* First Row */}
    <div className="bg-red-100"></div>
    <div className="bg-blue-200 col-span-2"></div>
    <div className="bg-red-300"></div>
    <div className="bg-red-400"></div>
    {/* Second Row */}
    <div className="bg-orange-100"></div>
    <div className="bg-orange-200 row-span-2"></div> {/* Let's try making one vertically long */}
    <div className="bg-orange-300"></div>
    <div className="bg-orange-400"></div>
    <div className="bg-orange-500"></div>
  </div>
);

4. Removing unused div elements

If you want to implement the layout so that the <div> in the upper-left and upper-right (as shown in the image above) are not rendered, you might do it like this:

const Grid = () => (
  <div className="h-screen grid grid-cols-5 grid-rows-3">
    {/* First Row */}
    <div className=""></div> {/* Unnecessary */}
    <div className="bg-blue-200 col-span-2"></div>
    <div className="bg-red-300"></div>
    <div className=""></div> {/* Unnecessary */}
    {/* Second Row */}
    <div className="bg-orange-100"></div>
    <div className="bg-orange-200 row-span-2"></div>
    <div className="bg-orange-300"></div>
    <div className="bg-orange-400"></div>
    <div className="bg-orange-500"></div>
  </div>
);

The <div> tags labeled as "unnecessary" can be removed by using .col-start-{n} and .col-end-{n}.

const Grid = () => (
  <div className="h-screen grid grid-cols-5 grid-rows-3">
    {/* First Row */}
    <div className="bg-blue-200 col-start-2 col-span-2"></div> {/* Spans two columns */}
    <div className="bg-red-300 col-end-5"></div>
    {/* Second Row */}
    <div className="bg-orange-100"></div>
    <div className="bg-orange-200 row-span-2"></div>
    <div className="bg-orange-300"></div>
    <div className="bg-orange-400"></div>
    <div className="bg-orange-500"></div>
  </div>
);

The values 2 and 5 used here mean that the element starts at the 2nd grid line and ends at the 5th grid line.
You can clearly see this by looking at the grid line numbers displayed at the top of the image below.

5. gap

Finally, let's add some spacing using gap, which is part of the Grid specification.

const Grid = () => (
  <div className="h-screen grid grid-cols-5 grid-rows-3 gap-4">
    {/* First Row */}
    <div className="bg-blue-200 col-start-2 col-span-2"></div> {/* Spans two columns */}
    <div className="bg-red-300 col-end-5"></div>
    {/* Second Row */}
    <div className="bg-orange-100"></div>
    <div className="bg-orange-200 row-span-2"></div>
    <div className="bg-orange-300"></div>
    <div className="bg-orange-400"></div>
    <div className="bg-orange-500"></div>
  </div>
);

Discussion