{"id":28950,"date":"2023-01-13T12:42:52","date_gmt":"2023-01-13T20:42:52","guid":{"rendered":"https:\/\/coderpad.io\/?p=28950"},"modified":"2023-06-07T13:39:00","modified_gmt":"2023-06-07T20:39:00","slug":"tailwind-grid-css-classes-a-how-to-guide","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/tailwind-grid-css-classes-a-how-to-guide\/","title":{"rendered":"Tailwind Grid CSS Classes: A How-to Guide"},"content":{"rendered":"\n<p><a href=\"https:\/\/coderpad.io\/blog\/development\/a-very-very-in-depth-guide-on-css-grid\/\">CSS grid<\/a> layout is a great way to keep your web content organized using a system of rows and columns.&nbsp;<\/p>\n\n\n\n<p>In this post, you&#8217;ll learn how to use Tailwind Grid CSS classes to build web components. We&#8217;ll also cover how you can easily resize the rows and columns, and how to make the grid responsive.&nbsp;<\/p>\n\n\n\n<p><a href=\"https:\/\/tailwindcss.com\/\" target=\"_blank\" rel=\"noopener\">Tailwind CSS<\/a> is a utility-first, low-level CSS framework that allows you to build web components directly into your HTML. It&#8217;s different from other CSS frameworks, like Bootstrap, that have predefined web components.&nbsp;<\/p>\n\n\n\n<p>What Tailwind provides is utility classes that will let you build more custom components.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Tailwind grid CSS classes<\/strong><\/h2>\n\n\n\n<p>There are several grid utility classes available in Tailwind, and you&#8217;ll find them all in the documentation.&nbsp;<\/p>\n\n\n\n<p>In plain CSS, if you want an element to be in a grid, you&#8217;ll use the <code>display<\/code> property and set it to <code>grid<\/code>. The equivalent to this in Tailwind is simply using the <code>grid<\/code> utility class. You&#8217;ll find this in the layout section of the documentation.&nbsp;<\/p>\n\n\n\n<p>Give this class to the components you want in a grid, and it adds the CSS <code>display: grid;<\/code>&nbsp;property to the components.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to create rows and columns<\/strong><\/h2>\n\n\n\n<p>In plain CSS, the <code>grid-template-columns<\/code>&nbsp;property lets you specify the number of columns you want in the grid. With Tailwind, you&#8217;ll use the <code>grid-cols-{n}<\/code> class, with &#8220;n&#8221; being the number of equal-sized columns.&nbsp;<\/p>\n\n\n\n<p>In the same way, you can use the <code>grid-rows-{n}<\/code> utility class when you want to add rows to the grid. This is in place of the <code>grid-template-rows<\/code>&nbsp;CSS property.&nbsp;<\/p>\n\n\n\n<p>To illustrate, we&#8217;re going to build a simple container with a few elements inside it and display it in a grid layout.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s get started!&nbsp;<\/p>\n\n\n\n<p>We&#8217;re going for a 3-by-3 grid with nine grid items and also adding a gap between each cell with the <code>gap-{size}<\/code>&nbsp;class.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"min-h-screen flex items-center justify-center\"<\/span>&gt;<\/span>\n\u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"grid grid-cols-3 grid-rows-3 gap-4\"<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>Cell 1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>Cell 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>Cell 3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>Cell 4<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>Cell 5<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>Cell 6<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>Cell 7<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>Cell 8<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>Cell 9<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since this particular example is a 3-by-3 grid, you can actually leave out the <code>grid-rows-3<\/code>&nbsp;class and the result will be the same.&nbsp;<\/p>\n\n\n\n<p>In fact, with any number of grid items, you can specify a fixed number of rows or columns and the cells will automatically flow into the next grid line.&nbsp;<\/p>\n\n\n\n<p>At this point, you can style the items as you wish by adding some utility classes. For this example, we&#8217;ve added a background color and some padding.&nbsp;<\/p>\n\n\n\n<p>Here&#8217;s a screenshot of the grid:&nbsp;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63c1c24c3b2d3.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A web page with a red-400 tailwind background color. The web page has a grid with children labelled Cell 1 to Cell 9.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>You\u2019ll notice the <code>gap<\/code> class adds equal spacing between every cell in the grid. You can change this and have a different gap size between the rows and columns.&nbsp;<\/p>\n\n\n\n<p>The gap utility class for the columns is <code>gap-x-{size}<\/code>, and for the rows it&#8217;s <code>gap-y-{size}<\/code>.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"grid grid-cols-3 gap-x-3 gap-y-1\"<\/span>&gt;<\/span>\n\n\u00a0\u00a0<span class=\"hljs-comment\">&lt;!-- cells --&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63c1c24e0bde1.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A web page grid with 9 children and a small gap size between the columns and rows.<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Grid column and row start\/end<\/strong><\/h2>\n\n\n\n<p>The size of the grid items can be increased so that they occupy multiple rows or columns.&nbsp;<\/p>\n\n\n\n<p>Tailwind has the grid column start\/end and grid row start\/end utilities to make this possible. These utility classes will go directly into the individual grid items, and they control the <code>grid-column<\/code> and <code>grid-row<\/code>&nbsp;CSS properties.&nbsp;<\/p>\n\n\n\n<p>Apart from resizing the cells, you can also choose where the rows and columns start.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Spanning rows and columns<\/strong><\/h3>\n\n\n\n<p>To increase the size of a grid item so that it expands across multiple columns, you&#8217;ll use the <code>col-span-{n}<\/code>&nbsp;class, with &#8220;n&#8221; being the number of columns you want that item to occupy. For the rows, the grid utility class is <code>row-span-{n}.<\/code>&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s see it in action. To illustrate, we&#8217;re going to create a masonry grid. A masonry grid is a grid where the cells don&#8217;t have a fixed height and length.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"grid grid-cols-3 gap-3\"<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"row-span-3 bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"row-span-2 bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 4<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"col-span-2 bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 5<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 6<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 7<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n \u00a0 \u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 8<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And here&#8217;s a screenshot of the grid layout:&nbsp;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63c1c24f2fb71.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A masonry grid comprising eight cells.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>Starting and ending lines<\/strong><\/h3>\n\n\n\n<p>For the columns, the grid utility classes <code>col-start-{n}<\/code> and <code>col-end-{n}<\/code> are used to change the position of the columns. To change the start and end positions for the rows, you&#8217;ll use <code>row-start-{n}<\/code>&nbsp;and <code>row-end-{n}<\/code>.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"grid grid-cols-3 grid-rows-3 gap-3\"<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"col-start-2 bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 4<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 5<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>\n\u00a0 \u00a0 &lt;<span class=\"hljs-attr\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 6<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 7<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0 \u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 8<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For this example, we want the first cell to start in the second column.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63c1c24fc62bf.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A grid with a defined start position.<\/figcaption><\/figure>\n\n\n\n<p>You can mix and match grid classes and get more interesting grid patterns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Auto grid placement<\/strong><\/h2>\n\n\n\n<p>Earlier, we mentioned how the grid items will auto-flow into the next line when you specify the number of columns or rows only. Defining both the number of columns and rows creates an explicit grid, and any overflow creates an implicit column or row. In Tailwind, there are <a href=\"https:\/\/tailwindcss.com\/docs\/grid-auto-flow\" target=\"_blank\" rel=\"noopener\">grid auto-flow<\/a> utilities, <code>grid-auto-{keyword}<\/code>, that control this placement of the grid items.&nbsp;<\/p>\n\n\n\n<p>Using the <code>grid-auto-row<\/code> function makes any extra cell overflow into a new row outside the explicit grid lines. This is the default if no auto-flow class is added.&nbsp;<\/p>\n\n\n\n<p>In the same way, using the <code>grid-auto-col<\/code> class adds the extra cells in a new column.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63c1c2506725c.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A grid child overflowing into a new row outside the explicit grid lines.<\/figcaption><\/figure>\n\n\n\n<p>The <code>grid-auto-dense<\/code> class makes the grid items attempt to occupy every available space in the grid. Adding the &#8220;dense&#8221; keyword to the <code>grid-auto-row<\/code> and <code>grid-auto-col<\/code> classes performs the same function but in the implicit rows or columns. The default, if you don&#8217;t add an auto-flow utility, is <code>grid-auto-row-dense<\/code>.&nbsp;<\/p>\n\n\n\n<p>There are also utilities for controlling the size of the implicit rows and columns. You&#8217;ll use the <a href=\"https:\/\/tailwindcss.com\/docs\/grid-auto-columns\" target=\"_blank\" rel=\"noopener\"><code>auto-cols-{size}<\/code><\/a> class for the columns, and <a href=\"https:\/\/tailwindcss.com\/docs\/grid-auto-rows\" target=\"_blank\" rel=\"noopener\"><code>auto-rows-{size}<\/code><\/a>&nbsp;for the rows.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Nesting grids<\/strong><\/h2>\n\n\n\n<p>A nested grid is a grid within a grid. That is, one of the grid items will be a smaller grid.&nbsp;<\/p>\n\n\n\n<p>It&#8217;s a pretty straightforward process to create a nested grid. First, you&#8217;ll build the bigger grid. Then, within one of the cells, build another grid.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"grid grid-cols-3 grid-rows-3 gap-3\"<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 4<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 5<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>\n\u00a0\u00a0    \u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"flex items-center justify-center\"<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"grid grid-cols-2 gap-2\"<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-red-400 p-2 rounded-md\"<\/span>&gt;<\/span>1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0   \u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-red-400 p-2 rounded-md\"<\/span>&gt;<\/span>2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0   \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-red-400 p-2 rounded-md\"<\/span>&gt;<\/span>3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0   \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-red-400 p-2 rounded-md\"<\/span>&gt;<\/span>4<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n   \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n \u00a0\u00a0\u00a0    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0 <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 7<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 8<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-3 rounded-md\"<\/span>&gt;<\/span>Cell 9<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/img_63c1c2510928e.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A nested grid.<\/figcaption><\/figure>\n\n\n\n<p>Since there&#8217;s no predefined dimensions for the grid items in this example, every cell expands to match the width and height of the biggest content, which in this case is the smaller grid.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Adding breakpoints<\/strong><\/h2>\n\n\n\n<p>Sometimes a large grid will not look good on smaller screens, so there&#8217;s a need to collapse it so it fits. This is possible by adding breakpoints for small, medium, and large screens.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-3\"<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0 <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-5 rounded-md\"<\/span>&gt;<\/span>Cell 1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-5 rounded-md\"<\/span>&gt;<\/span>Cell 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-5 rounded-md\"<\/span>&gt;<\/span>Cell 3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-5 rounded-md\"<\/span>&gt;<\/span>Cell 4<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n \u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-5 rounded-md\"<\/span>&gt;<\/span>Cell 5<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n \u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-5 rounded-md\"<\/span>&gt;<\/span>Cell 6<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-5 rounded-md\"<\/span>&gt;<\/span>Cell 7<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"bg-white p-5 rounded-md\"<\/span>&gt;<\/span>Cell 8<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For large screens, you&#8217;ll add the <code>lg<\/code> modifier, and for medium screens, it&#8217;s <code>md<\/code>.&nbsp;<\/p>\n\n\n\n<p>Now, you can choose to add an <code>sm<\/code> modifier for small screens, but it&#8217;s not necessary, as the <code>grid-cols-1<\/code> class will automatically be applied.&nbsp;<\/p>\n\n\n\n<p>Check out the sandbox below to see how it works:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 85%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=239170&#038;use_question_button\" width=\"640\" height=\"544\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>That&#8217;s a wrap<\/strong><\/h2>\n\n\n\n<p>You can place different kinds of content into a grid\u2014from text to videos to images, grids keep everything nice and tidy.&nbsp;<\/p>\n\n\n\n<p>Tailwind makes it very easy to create grid layouts through the use of its utility classes. You can specify the number of rows and columns, the size of the grid items, how they behave, and much more.&nbsp;<\/p>\n\n\n\n<p>Grids can also be made responsive by adding in breakpoints, which do the work of CSS media queries.&nbsp;<\/p>\n\n\n\n<p>Now that you know how to use Tailwind CSS grid classes, let&#8217;s see what you can create.&nbsp;<\/p>\n\n\n\n<p><em>This post was written by Oscar Jite-Orimiono. <\/em><a href=\"https:\/\/omj.netlify.app\/\" target=\"_blank\" rel=\"noopener\"><em>Oscar<\/em><\/a><em> has a B.Eng in mechanical engineering, but now he\u2019s a self-taught frontend web developer and technical writer. His skills include HTML, CSS, and JavaScript(Vanilla and jQuery). He builds websites with a focus on them being user-friendly, responsive, and having pleasing aesthetics. He\u2019s also interested in data science, Python, and SQL.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grids are a great way to keep your web content organized. You&#8217;ll learn how to use Tailwind Grid CSS classes in this article.<\/p>\n","protected":false},"author":1,"featured_media":28958,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[62],"keyword-cluster":[],"class_list":["post-28950","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/28950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=28950"}],"version-history":[{"count":15,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/28950\/revisions"}],"predecessor-version":[{"id":34555,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/28950\/revisions\/34555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/28958"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=28950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=28950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=28950"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=28950"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=28950"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=28950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}