How to use Tailwind CSS Grid.
By Emmanuel Chinonso
Technical Writer/Web Developer
Tailwind Grid is a powerful and flexible system for building responsive and customizable grid layouts. With Tailwind Grid, you can easily create complex grid structures and layouts without writing custom CSS. In this article, we will explore how to use Tailwind Grid, including the basics, advanced techniques, and best practices.
Table of content
- What is Tailwind Grid System
- Getting Started with Tailwind CSS Grid
- Basic Grid Structure in Tailwind CSS
- Grid Template Rows and Columns
- Tailwind CSS Grid Template Rows
- Tailwind CSS Grid Template Rows Example
- Tailwind CSS Grid Template columns
- Adding the Tailwind CSS grid column
- Adding space and gap in the Tailwind grid
- How do you make a Tailwind grid Responsive
- Creating Complex Layouts
- Using Tailwind CSS Grid with Other Frameworks
- Conclusion
- Resources
What is Tailwind Grid System?
Tailwind grid System is a two-dimensional layout that is made up of rows and columns of horizontal and vertical lines. It is a collection of classes and utilities that can create complex layouts quickly and easily.
Getting Started with Tailwind CSS Grid
Before you can start using the Tailwind grid, you need to install Tailwind to your project. This can be done with a package manager such as NPM
or Yarn
.
You can check out how to install Tailwind on your project using the Tailwind docs.
Once you have installed Tailwind, you need to include it in your project. You can do this by adding the following line of code to your HTML file:
<link rel=" stylesheet" href="tailwind.css" />
Basic Grid Structure in Tailwind CSS
The default Tailwind grid is based on the 12-column grid system. This means that you can divide your page into 12 columns, each with a specific width. You can also divide your page into rows, with each row having its own set of columns.
The basic grid structure in Tailwind consists of a container element with the grid
class, and child elements with the grid-cols-*
class. For example:
<div class="grid grid-cols-3 gap-4"> <div class="bg-blue-300 p-4 rounded-md text-white font-extrabold text-center">Item 1</div> <div class="bg-green-300 p-4 rounded-md text-white font-extrabold text-center">Item 2</div> <div class="bg-red-300 p-4 rounded-md text-white font-extrabold text-center">Item 3</div></div>
The code above shows the Tailwind grid class
Grid Template Rows and Columns
Tailwind Grid also provides classes for defining grid template rows and columns. Use the grid-template-rows
and grid-template-columns
classes to define the number and size of grid rows and columns.
For example:
<div class="grid grid-template-rows-2 grid-template-columns-3 gap-4"> <div>Grid item 1</div> <div>Grid item 2</div> <div>Grid item 3</div></div>
This will create a grid with two rows and three columns.
Tailwind CSS Grid Template Rows
Tailwind grid Template rows are a class used to accept multiple values in tailwind CSS. This property is the same as the CSS grid template rows. Below are the different classes and properties of the Tailwind grid Template rows.
Class | Properties |
---|---|
grid-rows-1 | grid-template-rows: repeat(1, minmax(0, 1fr)); |
grid-rows-2 | grid-template-rows: repeat(2, minmax(0, 1fr)); |
grid-rows-3 | grid-template-rows: repeat(3, minmax(0, 1fr)); |
grid-rows-4 | grid-template-rows: repeat(4, minmax(0, 1fr)); |
grid-rows-5 | grid-template-rows: repeat(5, minmax(0, 1fr)); |
grid-rows-6 | grid-template-rows: repeat(6, minmax(0, 1fr)); |
grid-rows-none | grid-template-rows: none; |
Tailwind CSS Grid Template Rows Example
we are going to look at how to use the Tailwind grid template rows here by building template rows using Tailwind CSS. You can check out the syntax below.
Syntax
<element class="grid grid-rows-number"> Contents... </element>
<div class="bg-yellow-500 p-8"> <div class="grid grid-row-3 gap-4 md:grid-row-2 lg:grid-row-4"> <div class="bg-blue-700 w-26 h-12 flex items-center justify-center rounded-md">1</div> <div class="bg-blue-500 w-26 flex h-12 items-center justify-center rounded-md">2</div> <div class="bg-blue-300 w-26 flex h-12 items-center justify-center rounded-md">3</div> <div class="bg-blue-500 w-26 flex h-12 items-center justify-center rounded-md">4</div> <div class="bg-blue-400 w-26 flex h-12 items-center justify-center rounded-md">5</div> <div class="bg-blue-300 w-26 flex h-12 items-center justify-center rounded-md">6</div> </div></div>
Some of the Tailwind classes used include the following
flex
: sets our element in a flexJustify-center
: this centers our elementsitems-center
: This centers all the elements in the files boxGrid
: this creates the grid rowGrid-row-3
: this creates a 3-grid rowrounded-md
: used to create a rounded corner for an element or body. which is the same as border-radius.md:grid-row-2
: this sets up the responsive screen for a medium size device.lg:grid-row-4
: this sets up the responsive screen for a large-size device
Tailwind CSS Grid Template Columns
The Tailwind CSS grid template columns are the alternative to CSS grid template columns. This property is used to set the number of columns and the size of the grid layout. Below are the different classes and properties of the Tailwind grid Template columns.
Class | Property |
---|---|
Grid-cols-1: | the row concedes one column |
Grid-cols-2: | the row concedes two columns |
Grid-cols-3: | the row concedes three columns |
Grid-cols-4: | the row concedes four columns |
Grid-cols-5: | the row concedes five columns |
Grid-cols-6: | the row concedes six columns |
Grid-cols-7: | the row concedes seven columns |
Grid-cols-8: | the row concedes eight columns |
Grid-cols-9: | the row concedes nine columns |
Grid-cols-10: | the row concedes ten columns |
Grid-cols-11: | the row concedes eleven columns |
Grid-cols-12: | the row concedes twelve columns |
Grid-cols-none: | this does not follow the grid-column property |
How do I use the Tailwind Grid Column?
Using the Tailwind Grid Column is as simple as specifying the number of columns in the grid layout. An example is a syntax below.
Syntax
<element class="grid grid-cols-{number}"> Content of the element </element>
Adding the Tailwind CSS grid column
We are going to build a grid layout using the Tailwind column. Adding the Tailwind grid column is as easy as defining the grid system and adding the number of columns you want to add. For example, in our case, we used the grid-cols-4
utility to add the number of columns we want on the grid layout.
<div class="bg-yellow-500 p-8"> <div class="grid grid-cols-4"> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-700">1</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-500">2</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-300">3</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-500">4</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-400">5</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-200">6</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-600">7</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-100">8</div> </div></div>
Adding space and gap in the Tailwind grid column
we can see that our Tailwind Grid Column isn't looking nice. We are going to add some spacing and gaps to the Tailwind grid column by using the classes flex
, justify-content
, and items-center
.
<div class="bg-yellow-500 p-8"> <div class="grid grid-cols-4 gap-3"> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-700">1</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-500">2</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-300">3</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-500">4</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-400">5</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-200">6</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-600">7</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-100">8</div> </div></div>
How do you make a Tailwind grid Responsive
Tailwind CSS Grid also allows you to add responsive breakpoints to your grid layout. This is useful when you want your grid to change its layout based on the screen size. Here is an example:
<div class="bg-yellow-500 p-8"> <div class="grid grid-cols-4 gap-3 md:grid-col-4 lg:grid-col-4"> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-700">1</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-500">2</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-300">3</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-500">4</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-400">5</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-200">6</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-600">7</div> <div class="w-26 flex h-12 items-center justify-center rounded-md bg-blue-100">8</div> </div></div>
Creating Complex Layouts
Once you have mastered the basics of the Tailwind CSS grid, you can start creating more complex layouts.
Tailwind CSS grid has a variety of utility classes that can be used to create complex layouts, just like we saw in our examples. You can use classes such as float
, flex-wrap
, and order
to control the alignment and positioning of your elements.
Using Tailwind CSS Grid with Other Frameworks
Tailwind grid can also be used in conjunction with other frameworks such as Bootstrap or Foundation. This makes it easy to create complex layouts quickly and easily. When using Tailwind CSS grid with other frameworks, you will need to make sure that you are using the correct classes to ensure compatibility.
Conclusion
Tailwind Grid is a powerful tool for building responsive and customizable grid layouts. By following the basics, advanced techniques, and best practices outlined in this article, you can create high-quality grid layouts that enhance your user experience.
Resources
Build modern projects using Bootstrap 5 and Contrast
Trying to create components and pages for a web app or website from
scratch while maintaining a modern User interface can be very tedious.
This is why we created Contrast, to help drastically reduce the amount of time we spend doing that.
so we can focus on building some other aspects of the project.
Contrast Bootstrap PRO consists of a Premium UI Kit Library featuring over 10000+ component variants.
Which even comes bundled together with its own admin template comprising of 5 admin dashboards and 23+ additional admin and multipurpose pages for
building almost any type of website or web app.
See a demo and learn more about Contrast Bootstrap Pro by clicking here.
Related Posts
Comments
...