Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Grid

Now the cards. In the CSS course, you arranged cards in a grid with display: grid and grid-template-columns. In Tailwind, the grid is switched on by the class grid, and the columns are set by the classes grid-cols-*:

.grid {
	display: grid;
}

.grid-cols-2 {
	grid-template-columns: repeat(2, minmax(0, 1fr));
}

The number in the class is the number of columns: grid-cols-3 makes three, grid-cols-4 four, and so on; any number works. The columns are always equally wide. Each one gets one share of the space (1fr, as you know), and minmax(0, 1fr) means that a column may get narrower than its content: a long word in one card can't make its column wider than the others.

The gaps between the rows and the columns are set by the same classes as in flexbox: gap-*, or separately gap-x-* and gap-y-*.

Do you need columns of different widths? Write them in square brackets, like the arbitrary values you know (w-[350px]), with an underscore instead of each space: grid-cols-[12rem_1fr] makes a column 12rem wide and a second one that takes the rest.

Instructions

  1. 1

    Add the class grid to the div that holds the cards (it's right below the labels). Nothing changes yet: a grid without columns has just one column.

  2. 2

    Add the class grid-cols-2. The cards are now in two columns, and the news box got a cell of its own in the third row. In a narrow preview, two columns are tight: on a phone, one column would be better, and on a big screen four. You'll learn to change the number of columns by the width of the screen in the lesson on responsive design.

  3. 3

    The cards touch each other. Add the class gap-6: 24 px between the rows and the columns.

Back

6/11

  1. 1. Display
  2. 2. Flexbox
  3. 3. Justify and Align
  4. 4. Columns
  5. 5. Wrapping and Growing
  6. 6. Grid
  7. 7. Spanning Columns
  8. 8. Centering
  9. 9. Badges on Cards
  10. 10. A Sticky Header
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.