{"id":17330,"date":"2022-09-16T04:39:36","date_gmt":"2022-09-16T11:39:36","guid":{"rendered":"https:\/\/coderpad.io\/?p=17330"},"modified":"2023-06-05T14:09:12","modified_gmt":"2023-06-05T21:09:12","slug":"a-very-very-in-depth-guide-on-css-grid","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/a-very-very-in-depth-guide-on-css-grid\/","title":{"rendered":"A Very, Very In-Depth Guide on CSS Grid"},"content":{"rendered":"\n<p>As websites progressed from simple documents into complex, interactive applications, web developers have utilized multiple approaches to organize and control the layout of UI elements on a webpage:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Floating elements around the webpage with the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/float\" target=\"_blank\" rel=\"noreferrer noopener\">CSS float property<\/a> to&nbsp;<\/li><li>Inserting elements into <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/table\" target=\"_blank\" rel=\"noreferrer noopener\">an HTML table elemen<\/a>t to create a table-based layout<\/li><li>Positioning elements <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/position\" target=\"_blank\" rel=\"noreferrer noopener\">using the position property<\/a><\/li><li>Using the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Learn\/CSS\/CSS_layout\/Flexbox\" target=\"_blank\" rel=\"noreferrer noopener\">Flexible box layout (Flexbox)<\/a> to align content on the page.<\/li><\/ul>\n\n\n\n<p>While these techniques enable us to organize and control the layout of elements on a webpage, they are not always sustainable, as some lead to performance bottlenecks. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Floats affect a web page&#8217;s SEO.&nbsp;<\/li><li>Table-based layouts lead to non-semantic markup on our sites, compromising <a href=\"https:\/\/coderpad.io\/blog\/development\/introduction-to-web-accessibility-a11y\/\" target=\"_blank\" rel=\"noreferrer noopener\">accessibility<\/a>.<\/li><li>Positioning elements require using CSS hacks.<\/li><\/ul>\n\n\n\n<p>Enter CSS Grid.<\/p>\n\n\n\n<p>CSS Grid is a layout system\u2014a method of organizing elements on a webpage\u2014used for building two-dimensional layouts. It is a structure made up of intersecting horizontal and vertical lines known as grid lines that divide the layout of an HTML element into rows and columns.<\/p>\n\n\n\n<p>CSS Grid is a powerful tool that enables web developers to define the position of HTML elements and control how the elements would span horizontally and vertically across the page\u2014a feat that cannot be achieved using other layout systems.<\/p>\n\n\n\n<p>This article is a comprehensive guide for everything you need to know about the CSS Grid layout system.&nbsp;<\/p>\n\n\n\n<p>In this article, we&#8217;ll go over:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"#grid-vocab\">The CSS Grid vocabulary<\/a><\/li><li><a href=\"#make-basic-grid\">How to make a basic grid<\/a><\/li><li><a href=\"#insert-gutters\">Inserting gutters between grid elements<\/a><\/li><li><a href=\"#align-horizontal\">Aligning grid items horizontally&nbsp;<\/a><\/li><li><a href=\"#align-vertical\">Aligning grid items vertically&nbsp;<\/a><\/li><li><a href=\"#grid-measurement-units\">Measurement units introduced with the CSS Grid layout<\/a><\/li><li><a href=\"#useful-css-functions\">Useful CSS functions you should utilize when working with CSS Grid<\/a><\/li><li><a href=\"#expand-grid-items\">Expanding grid items across multiple grid lines<\/a><\/li><li><a href=\"#css-shorthand\">Syntax to shorten the amount of CSS you need to type<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"grid-vocab\">The CSS Grid vocabulary<\/h2>\n\n\n\n<p>CSS Grid introduces a few new concepts you should familiarize yourself with before diving into its usage.<\/p>\n\n\n\n<p><strong>Grid container:<\/strong> The HTML element on which the grid layout is applied.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"518\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_6323267e8ec66-1024x518.png\" alt=\"\" class=\"wp-image-17334\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6323267e8ec66-1024x518.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6323267e8ec66-300x152.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6323267e8ec66-768x389.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6323267e8ec66-1536x778.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6323267e8ec66.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>A grid container is what holds the grid itself. If the grid constructs a box, then the container makes up the corners of the box.<\/figcaption><\/figure>\n<\/div>\n\n\n<p><strong>Grid item(s)<\/strong>: All direct child elements of the grid container.<\/p>\n\n\n\n<p><strong>Grid line:<\/strong> These are the horizontal and vertical dividing lines that make up the structure of the grid. These lines reside on either side of a column (column grid lines) or row (row grid lines). Hence, a grid with four columns will have five column lines, with one after the last column.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_63232721e79d2-1024x638.png\" alt=\"\" class=\"wp-image-17378\" width=\"768\" height=\"479\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63232721e79d2-1024x638.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63232721e79d2-300x187.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63232721e79d2-768x478.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63232721e79d2.png 1535w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><figcaption>Grid lines are the horizontal and vertical dividers (lines) that separate grid items into a grid<\/figcaption><\/figure>\n<\/div>\n\n\n<p><strong>Grid cell<\/strong>: The space between two column grid lines and two adjacent row grid lines, also known as grid tracks. It is a single unit of a grid and the smallest unit possible. By default, all grid items are laid out one item per grid cell.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_6323275f80322-1024x638.png\" alt=\"\" class=\"wp-image-17401\" width=\"768\" height=\"479\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6323275f80322-1024x638.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6323275f80322-300x187.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6323275f80322-768x478.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6323275f80322.png 1535w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><figcaption>A grid cell is the space between adjacent grid lines. Think of them as smaller boxes that construct the larger grid system.<\/figcaption><\/figure>\n<\/div>\n\n\n<p><strong>Grid track<\/strong>: The area between two adjacent grid lines. The gap between two grid row lines is a row track, and the gap between two grid column lines is a column track. The size of a grid track determines the width and height of our grid items.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_632335f4c5c41-1024x638.png\" alt=\"\" class=\"wp-image-17422\" width=\"768\" height=\"479\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_632335f4c5c41-1024x638.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_632335f4c5c41-300x187.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_632335f4c5c41-768x478.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_632335f4c5c41.png 1535w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><figcaption>A grid track is the area between two adjacent grid lines. Horizontal grid tracks are known as rows, and vertical grid tracks are known as columns.<\/figcaption><\/figure>\n<\/div>\n\n\n<p><strong>Grid area<\/strong>: A rectangular area on the grid made up of one or more grid cells. Grid areas are created when a grid item spans over one-to-many grid tracks. They must be rectangular; creating a T- or L-shaped grid area is impossible.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"479\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_632335f582f9f.png\" alt=\"\" class=\"wp-image-17423\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_632335f582f9f.png 729w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_632335f582f9f-300x197.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><figcaption>A grid area is a rectangular area on the grid and can consist of something as small as one cell, or as big as the entire grid.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Now, without further ado, let&#8217;s dive in!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"make-basic-grid\">How to make a basic grid<\/h2>\n\n\n\n<p>To create a CSS Grid layout, you&#8217;ll first need to define a grid container. The grid container can be any individual parent element (for example, a div).<\/p>\n\n\n\n<p>To define a grid container, pass <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/display\" target=\"_blank\" rel=\"noreferrer noopener\">the <code>display<\/code> property<\/a> with either a value of <code>grid<\/code> or <code>inline-grid<\/code> to an element&#8217;s style block. Doing this will implicitly convert all direct child elements of the grid container into grid items.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.block-level-grid-container<\/span> {\n<span class=\"hljs-attribute\">display<\/span>: grid;\n}\n\n<span class=\"hljs-selector-class\">.inline-level-grid-container<\/span> {\n<span class=\"hljs-attribute\">display<\/span>: inline-grid;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The value <code>grid<\/code> makes the parent grid element act a bit like an un-styled <code>div<\/code>, making it span the full width of a page by default due to them being block-level elements. In contrast, the value <code>inline-grid<\/code> behaves much like an un-styled <code>span<\/code>, making it act like an inline element to its siblings.<\/p>\n\n\n\n<p>Let&#8217;s look at an example.<\/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\">\"container\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"box a\"<\/span>&gt;<\/span>1<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> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"box b\"<\/span>&gt;<\/span>2<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> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"box c\"<\/span>&gt;<\/span>3<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> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"box d\"<\/span>&gt;<\/span>4<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>\n<\/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\n<p>In the code block above, we define a <code>div<\/code> with a class of <code>\"container\"<\/code>. Within the <code>div<\/code> are four child elements.<\/p>\n\n\n\n<p>We can now set the <code>div<\/code> as a grid container.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n     <span class=\"hljs-attribute\">display<\/span>: grid;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/VXoawWo3t8LEZsDACVXI_HmKLE5x1UKiQdtChppsVNQYgtJRU6hbVJdFGB9VhfSMMqAhJmBTVUunXFuAgmNBNnXIGU_mTfvfo4Xm2hSHSTQL3-bsYFriRTYOP9VqdoC_f78jaOLBSr1lnhMtb5Xc-3Zfq-PIsTtlLvTzg47qsN-c284P6NuOW44F1g\" alt=\"\"\/><figcaption>The four elements are stacked on top of one-another vertically.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Setting an element to a grid container alone will not immediately affect how the child elements are displayed, as we still haven&#8217;t specified how the layout should look. Hence, CSS lays out the grid items in a single-column grid.<\/p>\n\n\n\n<p>To change the layout of the grid items within a grid, we must explicitly define the rows and columns we want our grid to have. To do that, we&#8217;ll use the CSS properties <code>grid-template-columns<\/code> and <code>grid-template-rows<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Grid Template Columns<\/h3>\n\n\n\n<p>The <code>grid-template-columns<\/code> property lets us specify the number of columns we want in a grid container and how wide each column should be.<\/p>\n\n\n\n<p>The <code>grid-template-columns<\/code> property accepts one or more non-negative <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Learn\/CSS\/Building_blocks\/Values_and_units#lengths\" target=\"_blank\" rel=\"noreferrer noopener\">CSS length unit<\/a> values. The number of CSS-length values passed to the property specifies how many columns our grid container will have, and each value represents how wide each column (i.e., each grid track) would be.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n\u00a0\u00a0<span class=\"hljs-attribute\">display<\/span>: grid;\n\u00a0\u00a0<span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-number\">400px<\/span> <span class=\"hljs-number\">400px<\/span> <span class=\"hljs-number\">400px<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/dClQ_R4I5McKW85BH8VES6zr2tB8joHTqPaIvmToNjW555FlvCAuQ5LL21AcIkfs4uf6ZAIOlP-GEcypDETpg_j0_XYVewjqOfQK0l0KZH6llcqzhn8K5McIqYBUNZd5tRCTLTTuHRIocrmrTMCLlLvwGidF3zx541DAPWvev8VMQw7lpP_SKUZYNg\" alt=\"\"\/><figcaption>Because there are three columns, with four items, there will be two rows. The top row will have 1, 2, and 3 split into thirds, each 400px wide. The bottom row will have a 400px wide 4 element.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>In the example above, we use the grid-template-columns property to change the layout of our grid container to be divided into three columns, each <code>400px<\/code> wide.<\/p>\n\n\n\n<p>Notice that the last grid item starts on a new row below the first three columns. That is because we only specified three columns that are each <code>400px<\/code> wide. Consequently, when the number of grid items in the grid container exceeds three, CSS will wrap other grid items into new rows to maintain the container&#8217;s layout.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u26a0\ufe0f You can&#8217;t assign negative values to the CSS properties associated with the grid layout. Passing a value such as<code> -400px<\/code> to the grid-template-columns property will not affect the structure of a grid.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Grid Template Rows<\/h3>\n\n\n\n<p>The <code>grid-template-rows property<\/code> lets us specify the height of each row in a grid container.<\/p>\n\n\n\n<p>Unlike the <code>grid-template-columns<\/code> property, it doesn&#8217;t specify the number of rows our grid container should have. It only sets the height of each row. Such behavior occurs because a grid container will implicitly create a new row whenever grid items wrap into new lines. Hence, we can&#8217;t control the number of rows that should be present in a grid using the <code>grid-template-rows<\/code> property.<\/p>\n\n\n\n<p>The <code>grid-template-rows<\/code> property accepts one or more non-negative <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Learn\/CSS\/Building_blocks\/Values_and_units#lengths\" target=\"_blank\" rel=\"noreferrer noopener\">CSS length<\/a> values where each value represents the height of each row in a grid container, from the first to the last.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n\u00a0\u00a0<span class=\"hljs-attribute\">display<\/span>: grid;\n\u00a0\u00a0<span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-number\">400px<\/span> <span class=\"hljs-number\">400px<\/span> <span class=\"hljs-number\">400px<\/span>;\n\u00a0\u00a0<span class=\"hljs-attribute\">grid-template-rows<\/span>: <span class=\"hljs-number\">100px<\/span> <span class=\"hljs-number\">200px<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, values of <code>100px<\/code> and <code>200px<\/code> are passed to the grid-template-rows property. This gives the first row in the grid container a height of <code>100px<\/code> and the second row a height of <code>200px<\/code>.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/EPr3CGbwI7jLx0GTsSCcq4T0vbUw9DK79I494Cg8S3mOiCGwpv-Ntrjarj9oiLVhn5LFbd1hNDbQRj2MY3_rWrqIJ8Iy_JXhoI4w5HW-ZpeijY9lN7s3sKa4bjtmAO07kat4eeMqAG2e3m7q1MFgIEZ6DNkzmI5YoXgt-JxkPZGTuOeZ1E1EGIs-sw\" alt=\"\"\/><figcaption>Four elements with numbers inside of them are divided into a grid. There are two rows in this grid; the first is 100px tall, while the second is 200px high.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>In the code above, the <code>grid-template-rows<\/code> property sets only the heights of the first two rows in the grid container. If we are to add more grid items to the grid container and it creates new rows to wrap them onto, the heights of these implicitly created rows will not be determined by the <code>grid-template-rows<\/code> property but by the size of their content.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/LaiRX48vHFbZY2SZp-tMBHWvrH4VP7A4sEqnW6GQp0Fy6cho8HTbcRuXFGIcmxM-1D0lsGd8uWSoDfX9lPzTW2HO7_uGzm9Qd_R2jbkajzpvQBTrsapoHa9D4TV3GOCPqwLFbqRQ3uZqnXU5ByRbtbXjRjFw-BkCXHcNaq_6xSgWjiCTEmzIGe_DAg\" alt=\"\"\/><figcaption>A grid that contains twelve items and is split into three columns will create four rows. The first row is 100px high, the second is 200px, and the third and fourth are only as tall as the element&#8217;s contents.<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"insert-gutters\">Adding a gap between rows and columns<\/h2>\n\n\n\n<p>Adding more rows and columns to a grid container without properly spacing them apart would make the grid look too jam-packed and not helpful as a layout system. We can add gutters (spacing) between columns and rows to space them apart by adjusting the size of our grid lines. Let&#8217;s explore how we can do that.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">column-gap<\/h3>\n\n\n\n<p>The <code>column-gap<\/code> property adds some spacing between columns by adjusting the width of the vertical grid lines in a grid container. It accepts a non-negative CSS length value which defines how broad the grid line between each column will be.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\u00a0\n   <span class=\"hljs-attribute\">display<\/span>: grid;\u00a0\n   <span class=\"hljs-attribute\">column-gap<\/span>: <span class=\"hljs-number\">28px<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The code above sets the width of every vertical (column) grid line in the grid container to <code>28px<\/code>. This adds a space of <code>28px<\/code> between every column in the grid.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/LOfsGHOGi7eF9JLSZbIqUgGLtjIpa1OTy2MiZaJCxRHxWfi3l4wn0F7rqgi3AYyRh5uHNi5x6HMqMTaIq12yIjz5_7ZcvOZBRMvBntqOr7qvukOlBIptuSuZ9BF57eoAJRnWRFWrgu1-Pv0_NLvAU7TrWlWOz3jgWdlH4euub5Oemw5rJree_ykJXg\" alt=\"\"\/><figcaption>With three columns inside of a grid, there is now a 28 pixels gap to separate each column within the grid. The rows do not have a gap, as one has not yet been specified.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">row-gap<\/h3>\n\n\n\n<p>Just like <code>column-gap<\/code>, the <code>row-gap<\/code> property adds some spacing between rows in a grid container by adjusting the height of all horizontal grid lines in a grid container. It accepts a non-negative CSS length value which defines how large the grid line between each row will be.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\u00a0\n   <span class=\"hljs-attribute\">display<\/span>: grid;\u00a0\n   <span class=\"hljs-attribute\">column-row<\/span>: <span class=\"hljs-number\">40px<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The code above sets the height of every horizontal (row) grid line in the grid container to <code>40px<\/code>. This adds a distance of <code>40px<\/code> between every row in the grid.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/5cKP16pY1nAK3BigMnhij17RZhpAGQV2byDmOtSExqjByDggZwkN99nANJeQf4ewvBo4zAi1SYWiK-AOQafEP9tmBfZrj96BM-XaSvsJ-4ukLCZ3aggAj3AAggxE9QLGqOlGnhZoTu34pObYvqaHrSd7b9HoxBVvHUqF-q23rEiKgSRJQO08gVGIEQ\" alt=\"\"\/><figcaption>There are two rows in this column, with a 40px gap between them.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>We can combine the <code>column-gap<\/code> and <code>row-gap<\/code> properties to space our columns and rows.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\u00a0\n   <span class=\"hljs-attribute\">display<\/span>: grid;\u00a0\n   <span class=\"hljs-attribute\">column-row<\/span>: <span class=\"hljs-number\">40px<\/span>;\n\u00a0\u00a0\u00a0<span class=\"hljs-attribute\">row-gap<\/span>: <span class=\"hljs-number\">20px<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/0NHlyzdfg7OT__fWnpSh206SFErgOEFsHZhkB6mHB8re8_SWBFN0Xj5pePK70B61dc82433x71uszMfkyrTKCkeSvy6SHmU0pJTvE9BVcGIC5XTCRCHz3Rje31xP73SgvjGPSuccclZufZtLTWw3DR6HDfoBV6ggIeHWgtpUH6Lqv_3N_VCV-JU3Gg\" alt=\"\"\/><figcaption>A grid with rows and columns and a 20px of spacing between cells specified with the aforementioned CSS<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Using just a few lines of CSS, we have created a grid layout with all the basic CSS Grid properties such as columns, rows, and gutters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"align-horizontal\">How to align content horizontally<\/h2>\n\n\n\n<p>CSS Grid exposes six properties that let us control the alignment of grid items along the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Grid_Axis\" target=\"_blank\" rel=\"noreferrer noopener\">rows (inline axis) or columns (block axis)<\/a> of a grid container. In this section, we&#8217;ll go over each property that aligns grid items horizontally along the columns of the grid container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">justify-items<\/h3>\n\n\n\n<p>The <code>justify-items<\/code> property controls the alignment of all grid items along a grid container&#8217;s inline<em> <\/em>(column) axis. It is passed to the grid container, and its value applies to all grid items within the grid. It accepts four possible values.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>start<\/strong> \u2014 aligns grid items to the beginning of all columns, which is the left edge of their cell.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-items<\/span>: start;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh6.googleusercontent.com\/2IPDx_J66dWMwYDzRGZigfpGjKJgRDFEoEhO5N6reUN_fFafqYt1bNZqfeWOzDaJe9yQNLHV67mlLFfu-FeW623m88Kd4jmpGmIgpvWbuCMjpFzX9XbWaVfQF3zukP2W1_nTPT1jFkgGAAVwoiXNG1mrMT3fuq7IbmJhDdWXZqc_CiYvDU4fG66yWA\" alt=\"\"\/><figcaption>Items in the container are aligned to the left of the cell<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>end<\/strong> \u2014 aligns grid items to the end of all columns, which is the right edge of their cell.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-items<\/span>: end;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh6.googleusercontent.com\/bFVQwiKLRZ7y9JeeSLxvnwCW_qDCsdsgcD8w3jedJCYvZvOO1RBN2390fPeKLJdihEH4Bl7FPV5JR-yOUVluHpwTv-L5rnOcnM5JlZWC2E5DJd4ITWZn5GAub1FwDD5APvlAp3a6sHmxmSNG8mKTFvBiTa85s6ny7UzVWHeMXeECPe9fN-5lgPgQ-w\" alt=\"\"\/><figcaption>Items in the container are aligned to the right of the cell.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>center<\/strong> \u2014 places all grid items at the center of their cells.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-items<\/span>: center;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/pJOpNF8KRGvAAiukEFBDjaK5McxWFzOJzW7r0hASNPzJRhtu07dCC1tCcTdhYusveyno-1M7LDNv2e_BcBzkP72RVKuLfc7oFY5cncL_aWTwqKrCQJsLRXZrRfjlJ6zrwJUZt9zki8GkAZMAIOyHmuVyaKtAZEJ_GewP1Hq1lz6Te8U3mi1dryOcwQ\" alt=\"\"\/><figcaption>Items in the container are aligned to the center of the cell, with space on both sides.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>stretch<\/strong> \u2014 will stretch out the grid items to fill the whole width of their cells: this is the default value of the justify-items property.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-items<\/span>: stretch;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/yScUKOdzpulUp5ciVkIQIssysZElUdXH0k6uHFCpt4RTmIP68A6vfyeBSRtnGIVstY3Aqo_lmuyFZRCj59B7dMi3fMg5BvUJj2OFBLzeqTe2q9JNrOoRQhL_kKDCjkXnn3YrlGg8bk2gm5SkhtJZTZKSG8dYOTePcHxbmWjq_QKgvC8LaADosPWJeg\" alt=\"\"\/><figcaption>Items in the container take up the entire space of the cell.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">justify-content<\/h3>\n\n\n\n<p>The <code>justify-content<\/code> property lets you set the horizontal alignment of the entire grid within the inline (row) axis of the grid container. It accepts seven possible values.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>start<\/strong> \u2014 aligns the entire grid to the left edge of the grid container.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-content<\/span>: start;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"522\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_63233cee4798f-1024x522.png\" alt=\"\" class=\"wp-image-17589\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233cee4798f-1024x522.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233cee4798f-300x153.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233cee4798f-768x391.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233cee4798f-1536x782.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233cee4798f.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>end<\/strong> \u2014 aligns the entire grid to the right edge of the grid container.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-content<\/span>: end;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"522\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_63233c40cdb9c-1024x522.png\" alt=\"\" class=\"wp-image-17552\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233c40cdb9c-1024x522.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233c40cdb9c-300x153.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233c40cdb9c-768x391.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233c40cdb9c-1536x782.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233c40cdb9c.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>center<\/strong> \u2014 places the entire grid horizontally at the center of the grid container.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-content<\/span>: center;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"522\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_63233d345f5fe-1024x522.png\" alt=\"\" class=\"wp-image-17610\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d345f5fe-1024x522.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d345f5fe-300x153.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d345f5fe-768x391.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d345f5fe-1536x782.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d345f5fe.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>stretch<\/strong> \u2014 resizes the grid items to allow the grid to fill the entire width of the grid container: this is the default value of the justify-content property.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-content<\/span>: stretch;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"522\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_63233d38d8311-1024x522.png\" alt=\"\" class=\"wp-image-17611\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d38d8311-1024x522.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d38d8311-300x153.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d38d8311-768x391.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d38d8311-1536x782.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d38d8311.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>space-around<\/strong> \u2014 adds an even amount of space between each grid item, with half-sized spaces on both ends of the grid.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-content<\/span>: space-around;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"522\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-1024x522.png\" alt=\"\" class=\"wp-image-17614\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-1024x522.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-300x153.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-768x391.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-1536x782.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>space-between<\/strong> \u2014 creates an even amount of space between each grid item, with no space at the ends of the grid.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-content<\/span>: space-between;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"522\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_63233d422eb13-1024x522.png\" alt=\"\" class=\"wp-image-17613\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d422eb13-1024x522.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d422eb13-300x153.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d422eb13-768x391.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d422eb13-1536x782.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d422eb13.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>space-evenly<\/strong> \u2014 puts an even amount of space between each grid item and on both ends of the grid.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">justify-content<\/span>: space-evenly;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"522\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-1024x522.png\" alt=\"\" class=\"wp-image-17614\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-1024x522.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-300x153.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-768x391.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b-1536x782.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_63233d4700c2b.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">justify-self<\/h3>\n\n\n\n<p>This behavior of the <code>justify-items<\/code> property that controls the alignment of grid items along the inline (row) axis of a grid container can also be set on individual grid items via the <code>justify-self<\/code> property. This property is passed to the grid items. It accepts four possible values.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>start<\/strong> \u2014 aligns a grid item to the beginning of a column, which is the left edge of its cell.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.box-1<\/span> {\n   <span class=\"hljs-attribute\">justify-self<\/span>: start;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/mMg6DvdFb1vvqpJduTIDZglYnuLcB-MvA1XMeSFcN4vSfDovGtNdGQF98_CjB9ySEGI8xex-2SfS76pYbe05lISTmTJb6zZpFdNKFHdfLdHwf8gA8iDrsPOsnc1bEdWLlpLnB4RBbnX-NRumWW17jHZrDSTSmB2v4k7DtQxaYPQIPHrS8S_HIt8Wzg\" alt=\"\"\/><figcaption>The first item in the grid is aligned to the left of the cell space, while all of the other items stretch to fill the cell area.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>end<\/strong> \u2014 aligns the grid item to the end of a column, which is the right edge of its cell.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.box-2<\/span> {\n   <span class=\"hljs-attribute\">justify-self<\/span>: end;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/x3yDLrLa_f9UDD5O3Iu9VZDi31yceLNOSmqxDE9xhbZi9eejydms5V02_o7aYR8zPCkKgHt_S9DK_0KsucmVMCYsXLiQYjbfivEsxC7jKEL6Gyerhzf4xckpfpLZb7WgEgpSovvryyyCVEdcyGcikNUjhDTNAW8kdlgQ3wW9PaPYaIH40TuwT_lKjA\" alt=\"\"\/><figcaption>The second item in the grid is aligned to the right of the cell space, while all of the other items stretch to fill the cell area.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>center<\/strong> \u2014 places the grid item at the center of its cell.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.box-3<\/span> {\n   <span class=\"hljs-attribute\">justify-self<\/span>: center;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh6.googleusercontent.com\/NyJDcY0rSAa9x57POMqnjgySj-vZ4ShCfpxoXHpZwwHzlGCUrmsFNQ8O2_01l_aTLa--NVp8iVTDyddrSqEJlZBlER1aPZKeVf1RXspzqQE0hIzrfhvazXnlTQDsfy2J00_L_hIYXS8Tl48bv6o9HDvkdYVx5IYMRy81H8uBcAj6Iru2lQV4jRwoig\" alt=\"\"\/><figcaption>The third item in the grid is aligned to the center of the cell space, while all of the other items stretch to fill the cell area.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>stretch<\/strong> \u2014 will stretch out the grid item to fill the entire cell width; this is the default value of the <code>justify-self<\/code> property.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.box-4<\/span> {\n   <span class=\"hljs-attribute\">justify-self<\/span>: stretch;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/AkfRRzNlycOjbASCx0oqFSn_CznFPfcId0NBJQvAx1WNGkD1MzyRQFIg9O4Jzk5xVZdGlDH3W57A5h_w_pe8jQXjus-lzUv3SWqlwBnp6gWy0lj2A5bsWvziKV5ZmXsFMbCDymoOl7QrM5U8pLZbdImpVzVl2aZZamDt4thX_-Dgy4Z8Ccy1oXVWwA\" alt=\"\"\/><figcaption>The fourth grid item, like all grid items in the grid, is stretched to take up the full horizontal space of the grid cell<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"align-vertical\">How to align content vertically<\/h2>\n\n\n\n<p>Now let&#8217;s look at the properties that align grid items vertically along the rows of the grid container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">align-items<\/h3>\n\n\n\n<p>The <code>align-items<\/code> property controls the alignment of all grid items along a grid container&#8217;s block<em> <\/em>(column) axis. It is passed to the grid container, and its value applies to all grid items within the grid. It accepts four possible values.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>start<\/strong> \u2014 places all grid items at the top of all rows.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-items<\/span>: start;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh6.googleusercontent.com\/njewz8wPOW1NVSx52kZPiptKspeT1vS3W22PUTIfFagQUifabMzHeG6QBRKJ_LfmIH1DiZvrk6QrI2-SIbZQcKJUPiYMLmYIpNBTzpTFYkhk4_ZGzIA-FdNTqslUTna3Ov6ffUtMDLKE0QiKOX0teaE0uRbCOcARZOCpQTLrWgqAiYItoEL7v3W9kQ\" alt=\"\"\/><figcaption>The items in the grid are aligned to the top of the cell space.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>end<\/strong> \u2014 places all grid items at the bottom of all rows.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-items<\/span>: end;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/fzoIGqy6vW0-f5uK5UUMLVJYfskqeiTLhDPFXe-_5Q28At98UFhpU4SylHJgpTPsejJ6HFtHempFPlB1ev4eJ9uygoqB0CrQCU8hc5UEm7b8KZMpA0KrEmP10-LliUhpI5BqtSCtCK3nWRK6gICrKa3xHuSVqDirlGaC-Zimy7y84qf-j3ODj5etLg\" alt=\"\"\/><figcaption>The items in the grid are aligned to the bottom of the cell space.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>center<\/strong> \u2014 places all grid items at the center of their cells.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-items<\/span>: center;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/X4AWz1NuKt39_cTgMW1rxhPk5Vg77-er-uv41zyB-wUlbaVNH4N7HR_O-jpuDmzBiEZDaacsDBcx1OXNzqegN5wvEPdAJ-fGWuhpV8cxyNxyRvJ31wwzGJB2kfHlg0Ap9yEre_EANHyONYlbF-4pkoxrLPcVyUpef4p3BQxRNHbr-ElFs9l3HbLt1A\" alt=\"\"\/><figcaption>The items in the grid are aligned to the vertical center of the cell space.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>stretch<\/strong> \u2014 will stretch out all grid items to fill the entire height of their cells; this is the default value.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-items<\/span>: stretch;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh6.googleusercontent.com\/wBtpdmGcK4jnLzFe7eGFzJKb_16Td7CFZsmv3hQE_LZ7oYf9dNC9M0XWXTtKF1hZauTfXRfAms1pw4cLuysS5aqEW8urr2wlRWhbdQuddUovDVfSkdh5C8z7UGYJWbqX2wo--jpUIqOuLFuF2oOk0m68juRATDdint8aoB7SqOgYtH5LP3-va9KlFA\" alt=\"\"\/><figcaption>The items in the grid are stretched to take up the full vertical space of the cell space.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">align-content<\/h3>\n\n\n\n<p>The <code>align-content<\/code> property lets you set the vertical alignment of the entire grid within the block (column) axis of the grid container. It accepts seven possible values.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>start<\/strong> \u2014 aligns the entire grid to the top of the grid container.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-content<\/span>: start;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh6.googleusercontent.com\/mfLzck8xvPZJCj4p9vAKjkpN6wbtSrHScRS2CobDqLjHtXc2MACiao4OM9tnodhFLwpobICREL0TQUteoamfQFy2_tKuXl3VlikVXIvNe_-ZeF8d51nP4HDyfbQ1tEoD3_MJwJ3OE1b9mMbGnWVHF710a4BMx43MCauRfn2MykObfR3Evxc5tYkuxA\" alt=\"\"\/><figcaption>Grid items spaced near the top of the grid. Image captured with help of <a href=\"https:\/\/www.w3schools.com\/css\/tryit.asp?filename=trycss_grid_align-content_center\" target=\"_blank\" rel=\"noreferrer noopener\">W3 Schools<\/a>.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>end<\/strong> \u2014 aligns the entire grid to the bottom of the grid container.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-content<\/span>: end;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/ZdWM6ZK86Kvzj-1T5CAqCXiBc2eHWoRfYf6-swRS2Tgya5AzJlTnju2KxTxKHw2QEkgr0VrIXQJRIKMLCc6wxPYbPQc3kVxNEoKRKbwyNSlxn6z1yKgRPIFxwfKladrgyBDCN7X03KzhNcolTGmz6i2gm07EDzW7sY8h2wNMw9vnP2eN5-lCnBZtLA\" alt=\"\"\/><figcaption>Grid items spaced near the bottom of the grid. Image captured with help of <a href=\"https:\/\/www.w3schools.com\/css\/tryit.asp?filename=trycss_grid_align-content_center\" target=\"_blank\" rel=\"noreferrer noopener\">W3 Schools<\/a>.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>center<\/strong> \u2014 places the entire grid vertically at the center of the grid container.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-content<\/span>: center;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh5.googleusercontent.com\/q5xJoTSWvwa2g6Af_cj9EOJ4_7xkurZn392oIgKHLLKg5MiLbihj6rgYgHjrKhqBLmq6WNRxpcO9qQ-RdKeSY-FqTehi6dcH-9EHLr_a675M401xm2Bmp7xtHNDc6avoK8_zvQXr9LelTHsDUe05PLzhqT56omQ6XqTz0Vg-CASoxv2TKtkow0UNwA\" alt=\"\"\/><figcaption>Grid items spaced in the center of the grid. Image captured with help of <a href=\"https:\/\/www.w3schools.com\/css\/tryit.asp?filename=trycss_grid_align-content_center\" target=\"_blank\" rel=\"noreferrer noopener\">W3 Schools<\/a>.<\/figcaption><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>stretch<\/strong> \u2014 The grid items stretch to fill the full height of the container grid; this is the default value.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-content<\/span>: stretch;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/QMWt_Z9x5BsHmhEHHeWXtjTuTAyccdE36nrHkLWWELf6SK7WVW4ZYum81dkK4q8lR4JKaHBXONvYXLV7Cfb6xS16OxeD24VgeUI5RQFSbFwNmY2fR25MXWVnHDSLCW_StFsMO_gr5AdKCSq-ys1_NS_QhzsfPMHhB6RJ4LMJ1SAXWiM_SFYQ4IFPeA\" alt=\"\"\/><figcaption>Grid items stretched evenly across the grid. Image captured with help of <a href=\"https:\/\/www.w3schools.com\/css\/tryit.asp?filename=trycss_grid_align-content_center\" target=\"_blank\" rel=\"noreferrer noopener\">W3 Schools<\/a>.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>space-around<\/strong> \u2014 creates an even amount of space between each grid item, with half-sized spaces on both ends of the grid.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-content<\/span>: space-around;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh5.googleusercontent.com\/Xmx7kD6Mk9NDW9Uap7cYYLy4Ohn1Nyf3c1-KVm5bdQeYiEybXq2UeYQx8EHnGCAjlteMwSsGsc-lfg4ozJfZ0vbZFqXuDPkwTRYt3_NK7aLHXSo427E8-DwdJeS329buO8i5O1ykPk_iC3_uWN3TQqy-NC6yZwQDIeI-MZ-sAB-7Xj_cgpivKIpB6A\" alt=\"\"\/><figcaption>Grid items spaced with top and bottom spacing each half the size of the middle spacing. Image captured with help of <a href=\"https:\/\/www.w3schools.com\/css\/tryit.asp?filename=trycss_grid_align-content_center\" target=\"_blank\" rel=\"noreferrer noopener\">W3 Schools<\/a><\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>space-between<\/strong> \u2014 adds an even amount of space between each grid item, with no space at the ends of the grid.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-33\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-content<\/span>: space-between;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-33\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh3.googleusercontent.com\/YHDqpOlvwSpMkMCk7liGTtNEYXuQHwhh36cfQEXSxRYubd2pE4Jf3tysZtolnHDHgMVsN-XCDbbkFIqG27aiiEXZplLFpK2gVkbImJaZHeCZgtK9oGEn8_BUTKKKKcgJeLQPAyOrYR9wM3sjQlFtHZ7WCDXf_ltKlUC3U0kDu7NGg-ovryCWr_gbpQ\" alt=\"\"\/><figcaption>Grid items have large space in the vertical-middle. Image captured with help of <a href=\"https:\/\/www.w3schools.com\/css\/tryit.asp?filename=trycss_grid_align-content_center\" target=\"_blank\" rel=\"noreferrer noopener\">W3 Schools<\/a>.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>space-evenly<\/strong> \u2014 places an even amount of space between each grid item and on both ends of the grid.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-34\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">align-content<\/span>: space-evenly;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-34\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh6.googleusercontent.com\/rQFpvpx9PsZIBC37QaZPJZFjP2lpn-UQ2i6C2UBCsNuXAZlRsjOCgpvd1lPFH293IVbTohtraZy1ifrh5ZOrrJLiE1t1u_eEBMdJe8ormCmky3-MabQeA-uDbXqNoP_cOPtH6EFxjwTkUxL5lCiynjyrHRL6jWo-sQ1h7rKR8oPQ7qSKbjhRk-VEWw\" alt=\"\"\/><figcaption>Grid items have even spaces in the spaces between them. Image captured with help of <a href=\"https:\/\/www.w3schools.com\/css\/tryit.asp?filename=trycss_grid_align-content_center\" target=\"_blank\" rel=\"noreferrer noopener\">W3 Schools<\/a>.<\/figcaption><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">align-self<\/h3>\n\n\n\n<p>This behavior of the <code>align-items<\/code> property that controls the alignment of grid items along the block (column) axis of a grid container can also be set on individual grid items via the <code>align-self<\/code> property. This property is passed to the grid items. It accepts four possible values.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>start<\/strong> \u2014 places a grid item at the top of its cell.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-35\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.box-4<\/span> {\n   <span class=\"hljs-attribute\">align-self<\/span>: start;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-35\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh5.googleusercontent.com\/fATY8sjc-5PGkpENqd4pqHBBwrxt5nE5wlavDtZxTjFUiJtceR6bCfBOLUQ_jk1x8-W4iuMIjpD41LZ_-G0v3SdzpIgoOVxa_Gsoz-bPRLTd_qzvxX41cflooVGyHxgaL68vgt5xrXLG_qCw7uT2S1BPwYjyV_OeHxmJw01-Kbkh6l3Ih_yqkkwa6A\" alt=\"\"\/><figcaption>The fourth item in the grid is aligned to the top of the cell space, while all of the other items stretch to fill the cell area.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>end<\/strong> \u2014 places a grid item at the bottom of its cell.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-36\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.box-5<\/span> {\n   <span class=\"hljs-attribute\">align-self<\/span>: end;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-36\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh6.googleusercontent.com\/RDkLUmgFOo9zwHWliP3mtfjhNt02LuikYby45Sbs1HjceDiSa8Y3BZDQTKicv544GNPZB3MfE57J4R9I7AXPYJLxVm9fnCHOZzrGpwsh7ULeSpbtC1sYNyBtrqu3tKg2whLjdE5-6ywPOsEpMSCXoxGnM7jwH63rpJPKfnZbuSlvj87q5UTaUnkXrQ\" alt=\"\"\/><figcaption>The fifth item in the grid is aligned to the bottom of the cell space, while all of the other items stretch to fill the cell area.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>center<\/strong> \u2014 places a grid item at the center of its cells.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-37\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.box-6<\/span> {\n   <span class=\"hljs-attribute\">align-self<\/span>: center;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-37\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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:\/\/lh5.googleusercontent.com\/GIhfPU42FXym-wVrO-oM5qATUwOEsCeKF-hFrH55KU2k18hTxul69ipZEeGnfx6oywZ8VrW-zvo12nETKpGurt6l-m-qSLRI00my7v-nJesx8na6ZQb4PGV2-5KbcSzVtBkPK5ZqbuK_hLNzIw933_ZqFyiarBsqSUCrlNa2kDxq-26NHiFZk_KihA\" alt=\"\"\/><figcaption>The sixth item in the grid is aligned to the vertical center of the cell space, while all of the other items stretch to fill the cell area.<\/figcaption><\/figure>\n<\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>stretch<\/strong> \u2014 will stretch out the grid item to fill the entire height of its cell; this is the default value.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-38\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.box-1<\/span> {\n  <span class=\"hljs-attribute\">align-self<\/span>: stretch;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-38\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"192\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/image-20.png\" alt=\"\" class=\"wp-image-18161\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/image-20.png 704w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/image-20-300x82.png 300w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><figcaption>The first grid item, like all grid items in the grid, is stretched to take up the full vertical space of the grid cell.<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"grid-measurement-units\">Introducing new measurement units<\/h2>\n\n\n\n<p>Aside from non-negative <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Learn\/CSS\/Building_blocks\/Values_and_units#numbers_lengths_and_percentages\" target=\"_blank\" rel=\"noreferrer noopener\">CSS length<\/a> values such as <code>px<\/code>, <code>rem<\/code>, <code>vw<\/code>, and percentages (<code>%<\/code>), you can also use special sizing units and keywords to control the size of grid items in a grid.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The fractional (fr) unit<\/h3>\n\n\n\n<p>The <code>fr<\/code> unit, short for &#8220;fractional,&#8221; is a length unit introduced with the CSS Grid layout. It represents a fraction of the free space available in a grid container. This makes it the ideal unit for defining responsive columns and rows that will scale as the browser&#8217;s viewport shrinks and grows.<\/p>\n\n\n\n<p>Let&#8217;s look at an example to explain how helpful the <code>fr<\/code> unit is and why it&#8217;s a better alternative to other CSS length units.<\/p>\n\n\n\n<p>Say we are to create a grid layout made up of three columns where the first column takes up 1\/6 of the grid&#8217;s width, the second column takes up twice the width of the first column, and the third column takes up three times the width of the first column.<\/p>\n\n\n\n<p>If we weren&#8217;t using the <code>fr<\/code> unit in the example above, we&#8217;d have to perform some math on our end. We had to figure out the total width of the grid (100%), divide that by 6, then multiply whatever the result is by how wide each column should span, which would be:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>width of first column = 100% \/ 6 * 1 &#8212;&gt; 15%<\/li><li>width of second column = 100% \/ 6 * 2 &#8212;&gt; 30%<\/li><li>width of third column = 100% \/ 6 * 3 &#8212;&gt; 45%<\/li><\/ul>\n\n\n\n<p>As the layout becomes more complex, using percentages or any <a href=\"https:\/\/coderpad.io\/blog\/development\/an-overview-of-five-css-math-functions\/\" target=\"_blank\" rel=\"noreferrer noopener\">CSS math function<\/a> becomes unsustainable. This is when the <code>fr<\/code> unit comes in handy.<\/p>\n\n\n\n<p>The <code>fr<\/code> unit tidies all this by letting us specify how the available space in a grid container should be distributed among its rows and columns. Then it spreads the space available in that order.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-39\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">display<\/span>: grid;\n   <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-number\">1<\/span>fr <span class=\"hljs-number\">2<\/span>fr <span class=\"hljs-number\">3<\/span>fr;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-39\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, we assign a fraction of the available space in a grid container to the first column, then twice to the second column, and thrice to the third column.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/o2_OEa_aixFsTRUcbe0LFBzTgIArTBEpQwkqiZFEFcJ3Kyk8hl8Moa6HRrrGjId3PKcl8rvD9VadqauWw9MNhRxIdm_mQBz4NeHUqPrNyAX4FM-M-6QDZf4JU1tmc268_9GVzqEB1geAAbKa8-471QHWhEvR1cQjrm-BRQTwHs1AvpFglkvI87QtBg\" alt=\"\"\/><figcaption>A grid that consists of three columns and two rows. The first column is one-third the size of the last column and half the size of the second column.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">min-content<\/h3>\n\n\n\n<p>The <code>min-content<\/code> is a sizing keyword that sets the width of a grid track to its <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Intrinsic_Size\" target=\"_blank\" rel=\"noreferrer noopener\">intrinsic minimum width<\/a>, which is usually the size of the smallest content or text within its grid items. It works similarly<a href=\"https:\/\/coderpad.io\/blog\/development\/an-overview-of-five-css-math-functions\/#min\" target=\"_blank\" rel=\"noreferrer noopener\"> to the CSS function <code>min<\/code><\/a>, but is used as a spacing unit instead of a function.<\/p>\n\n\n\n<p>When applied to a column or row, they become as narrow as the longest word in their track. This lets you get the shortest measurement of the content inside the grid items.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-40\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">display<\/span>: grid;\n   <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-number\">1<\/span>fr min-content <span class=\"hljs-number\">1<\/span>fr;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-40\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, we set the first and third columns to be as wide as a fraction of the grid container while passing the <code>min-content<\/code> to the second column, making it shrink to the size of the words in its grid items.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/nYF4gWJqbZgV2eRfIb-8ZJMmX0giKBMUR3T33Xc8l5cqiFYIjIS6VrP5o3O3XANO_d7WstGgQA6kKHFnyYNMb8FN4pTyhRNyQ7dsZUhl8MdqG1JvpSeKzEg2NgglDBNlKBVx1DAwAws-eWnvPm6fsGM9QF8roHLQJ2U4nJD3SBo7gm9TtJCShMz-IQ\" alt=\"\"\/><figcaption>A grid with three columns. The middle column only takes up the smallest amount of space needed to display a single character, while the first and last columns take up all remaining space.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">max-content<\/h3>\n\n\n\n<p>The <code>max-content<\/code> sizing keyword has the opposite effect of <code>min-content<\/code>; <code>max-content<\/code> behaves similarly to <a href=\"https:\/\/coderpad.io\/blog\/development\/an-overview-of-five-css-math-functions\/#max\" target=\"_blank\" rel=\"noreferrer noopener\">a CSS function: <code>max()<\/code><\/a>. When applied to either a column or row, the track becomes as wide as possible for all the content within the grid items to display in one long unbroken line.&nbsp;<\/p>\n\n\n\n<p>The most significant benefit of using <code>max-content<\/code> is that you can let the content within our grid items breathe and spread out rather than wrapping them into new lines, which causes vertical text overflows.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-41\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">display<\/span>: grid;\n   <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-number\">1<\/span>fr max-content <span class=\"hljs-number\">1<\/span>fr;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-41\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The code above defines three columns and sets the width of the second column to the <code>max-content<\/code> keyword. If we add a lot of words to the grid items in the second column, the text in these grid items won&#8217;t overflow. Instead, the width of the second column will grow, and the first and third columns will shrink to accommodate it.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/PmBAo8AsGwCj0IxKzxmVjBH1c0ABLCLENdH4MQRAqDswyKH-33UzQNuYNrcfMIa6TIfAyfVOMLcnMosgaaI62-InUl8bHQ9dmyOvWvMAwvF7jpLaPmHKRk95n6SGuSh21VPu6kJcbtM_nYAA2ki2DhvEcEHonOXuzaXrPxbfVjZj5sAm6yhwNpBUew\" alt=\"\"\/><figcaption>A grid with three columns. The middle column has a sentence that&#8217;s not broken into newlines, but rather stretches to fill as much space as it needs and no more. The first and last columns take up all remaining space.<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"useful-css-functions\">Useful CSS functions to combine with CSS Grid<\/h2>\n\n\n\n<p>When working with the CSS Grid layout, there are a few handy functions you should familiarize yourself with to help increase the efficiency of your CSS.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">repeat()<\/h3>\n\n\n\n<p>The <code>repeat()<\/code> function lets us define recurring unit declarations for our grid container&#8217;s rows or columns in a more compact form.<\/p>\n\n\n\n<p>For example, suppose you repeat unit declarations when defining columns or rows using the <code>grid-template-rows<\/code> and <code>grid-template-column<\/code> properties. You can use the <code>repeat()<\/code> function to declare these recurring patterns more concisely.<\/p>\n\n\n\n<p>The function takes two arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>repeat count:<\/strong> this is an integer value of 1 or higher, or the keyword values <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/repeat#auto-fill\" target=\"_blank\" rel=\"noreferrer noopener\">auto-fill<\/a> or <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/repeat#auto-fit\" target=\"_blank\" rel=\"noreferrer noopener\">auto-fit<\/a>. It specifies the number of times the recurring pattern of the rows or columns should repeat.<\/li><li><strong>track(s) definition<\/strong>: this is where we specify the recurring pattern of rows or columns to repeat. We can pass one or more non-negative length values to it, where each value represents the size of a row or column.<\/li><\/ul>\n\n\n\n<p>Essentially, its syntax is this:<\/p>\n\n\n\n<p><code>repeat(&lt;number of columns\/rows&gt;, &lt;the height or width of each column \/row&gt;);<\/code><\/p>\n\n\n\n<p>Say we want to create a grid with six equal columns. Without the <code>repeat()<\/code> function, we&#8217;d have to explicitly define each column using the grid-template-columns property.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-42\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n     <span class=\"hljs-attribute\">display<\/span>: grid;\n     <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-number\">1<\/span>fr <span class=\"hljs-number\">1<\/span>fr <span class=\"hljs-number\">1<\/span>fr <span class=\"hljs-number\">1<\/span>fr <span class=\"hljs-number\">1<\/span>fr <span class=\"hljs-number\">1<\/span>fr;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-42\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, as the number of columns increases, this approach becomes unsustainable and verbose. We can rewrite this into a more compact form using the <code>repeat()<\/code> function.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-43\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n     <span class=\"hljs-attribute\">display<\/span>: grid;\n     <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-built_in\">repeat<\/span>(<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">1<\/span>fr);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-43\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here we use the <code>repeat()<\/code> function to create a column that spans over a fraction of our grid container, repeating the process six times. This drastically reduces the number of repetitive unit declarations in our CSS.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/eaLX5z6FTVI0VCldQBosUYDtTrPQYZe38y0O3YDVAMN29C_7Hya-M4lrZVeVp6wKNVnjuqO66fyp7-KcJU10eYh62mjE85Bsv-n5JZrmWnePdPnCAojX1-5lYq5oDSAbUUVuBnq0nQ6sfL85lcpyyct29rDSwiVeVObUM0SsjzkzgzOAdbQ9BA7k-Q\" alt=\"\"\/><figcaption>Six evenly spaced items in a row.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">minmax()<\/h3>\n\n\n\n<p>When defining a responsive grid layout, you likely want to give each grid track a minimum and maximum size to ensure that they scale up and down to fit their content as the viewport resizes. This is where the <code>minmax()<\/code> property comes in handy.<\/p>\n\n\n\n<p>The <code>minmax()<\/code> function lets us specify a minimum and maximum size for a grid track. As the grid is resized across viewports, the grid track will grow and shrink across that range. On smaller screens, it will shrink until it reaches the minimum size. And on larger screens, it will stretch until it reaches the maximum size.<\/p>\n\n\n\n<p>The <code>minmax()<\/code> function takes two parameters, <code>min<\/code> and <code>max<\/code>, where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>min is the minimum size of the track.<\/li><li>max is the maximum size of the track.<\/li><\/ul>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<p><code>minmax(&lt;minimum allowed value&gt;, &lt;maximum value&gt;);<\/code><\/p>\n\n\n\n<p>The <code>minmax()<\/code> function also accepts CSS Grid sizing units, keywords, length and percentage values.<\/p>\n\n\n\n<p>Let&#8217;s look at an example using the <code>minmax()<\/code> function.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-44\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n  <span class=\"hljs-attribute\">display<\/span>: grid;\n   <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-built_in\">repeat<\/span>(<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">1<\/span>fr);\n   <span class=\"hljs-attribute\">grid-template-rows<\/span>: <span class=\"hljs-built_in\">repeat<\/span>(<span class=\"hljs-number\">2<\/span>, minmax(<span class=\"hljs-number\">100px<\/span>, max-content));\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-44\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, we use the <code>minmax()<\/code> function to set the minimum height of the two rows in our grid container to 100px and the maximum height to <code>max-content<\/code>, which ensures each row stretches and becomes as wide as possible to contain its contents if it gets taller than <code>100px<\/code>.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/Fdl77Um2hpublshpNBR4l-G19W25NIIL7uVfSJgVpLdKFZK2Hi70gblaxeEGm-6btPY_7cP4n4YKpKZ913GR2cIAOFI837QPPtDpCrT0LqwfiA8uMWmpDSFnHtGoFbARcI0YevcCOkWq_IluvlE9Wzgk-OVZs_1VEFjM7VJY3vq-9QNf3zpq27Lp4w\" alt=\"\"\/><figcaption>Two rows and three columns. The three columns have equidistant widths. The first row has 100px height, and the second has the height of a paragraph inside one of the cells.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>A significant benefit of the <code>minmax()<\/code> function is that it reduces the need for media queries. Rather than relying mainly on media queries to control the size of grid tracks (columns and rows) across viewports, it lets you set up, to a degree, a responsive transition of values for a grid track.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">fit-content()<\/h3>\n\n\n\n<p>The <code>fit-content()<\/code> function operates similarly to the <code>minmax()<\/code> function. The difference is that with <code>fit-content()<\/code>, the minimum value is the size of the content within the grid item, and the maximum value would be the value we pass to it. This lets you clamp your content to the smallest value and scale it up to a certain value as needed.<\/p>\n\n\n\n<p>When applied to a grid track, it sets the size of the grid track to its intrinsic minimum width, which is the size of the smallest piece of content or text in its grid items. The caveat is that the smallest piece of content or text size is not larger than the value specified in the function.&nbsp;<\/p>\n\n\n\n<p>However, if the value of the intrinsic minimum width exceeds the value supplied to the function, the size of the grid track is set to the value passed to the <code>fit-content()<\/code> function, and the content of the grid items will begin to wrap.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-45\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">display<\/span>: grid;\n   <span class=\"hljs-attribute\">grid-template-columns<\/span>: <span class=\"hljs-built_in\">fit-content<\/span>(<span class=\"hljs-number\">200px<\/span>) <span class=\"hljs-built_in\">fit-content<\/span>(<span class=\"hljs-number\">300px<\/span>) <span class=\"hljs-built_in\">fit-content<\/span>(<span class=\"hljs-number\">400px<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-45\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The code above creates three columns with widths of 200px, 300px, and 400px, respectively, using the <code>fit-content()<\/code> function. This means that the size of each column will be equal to the size of the smallest piece of content or text in its grid items, but if that becomes greater than the value supplied to the <code>fit-content()<\/code> function, then the size of the columns will be set to the value passed to the <code>fit-content()<\/code> function.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/6_tL1f9PGuW_LJpOkE0BK7E1FCrhQUYZ6RQOq23zJuR2mJGrwyUSxgLNvEIUwxQzJpOc7o8lqmeZX0mNf-hKe_NEEo6dMo667TrCxGcD7IwlsEAYeiiYzBBeu7ETvgXNVfvQ8PkywlOXdABUfafHWSp5JfWaHDHQHa7b8NosxmA7Pl-Fh6PlpD9tCg\" alt=\"\"\/><figcaption>A grid with three columns. The first and third columns are only as wide as a single character, since that&#8217;s what they contain. The middle column is 300px wide because there&#8217;s a paragraph in the middle of it.<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"expand-grid-items\">Expanding grid items across multiple grid lines<\/h2>\n\n\n\n<p>Remember, when talking about grid lines, the horizontal lines reside on either side of a row (known as row grid lines), and the vertical lines reside on either side of a column (known as column grid lines) within the grid container.<\/p>\n\n\n\n<p>In the grid container, each grid line is given a number based on its position on the grid. The first grid line (row or column) is given the number 1, the second 2, and so on.&nbsp;<\/p>\n\n\n\n<p>For example, the image below displays the number of grid lines present on a grid container with three columns and two rows, where the column lines are the numbers inside the orange circle from 1 &#8211; 4, while the row lines are the ones inside the blue circle from 1 &#8211; 3.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/z1Txqjd8Ljujmfdol2JQpbX_WYI6yiO-ij5crG7QWYOqMJb9rHpHutND_CznY_u8e8dvom8ZWAnQq2dhSoLyZBtLmKU6Y-VRIKbxph93-LUxdsDHqX6omASowG18Pf4s-UfX9NHuVdDDzELqxyXbIJSyRz1Bz1fxuChkj8s-PyNaqXnoeDDauuad8g\" alt=\"\"\/><figcaption>A grid system with the lines numbered as described previously<\/figcaption><\/figure>\n<\/div>\n\n\n<p>The browser uses these grid lines to control the layout and position of items within the grid. For instance, in the image above, the first column spans from column line 1 to column line 2, and the first grid item will be placed between those lines and span that wide. However, a handful of CSS Grid properties let us control the position of grid items along these grid lines and how wide they would span horizontally and vertically across them.<\/p>\n\n\n\n<p>The properties you can use to control the position of grid items and how they span across these lines are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>grid-column-start<\/code><\/li><li><code>grid-column-end<\/code><\/li><li><code>grid-row-start<\/code><\/li><li><code>grid-row-end<\/code><\/li><\/ul>\n\n\n\n<p>Let&#8217;s go over each in detail.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">grid-column-start<\/h3>\n\n\n\n<p>The <code>grid-column-start<\/code> CSS property lets us specify the horizontal starting position of a grid item along the column grid lines within the grid container. This start position defines the start of the left edge of the grid item.<\/p>\n\n\n\n<p>Using the grid layout below, let&#8217;s target the first grid item and set its horizontal starting position to column line 2.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/cpqXH1DdnU-5uZRWzkZHbTR-FHwgxjHcChzyQgo1c4rGgoUNSCrMZGSLFWIiQKAgEJz0ZjldQ6FYAyVmzd989CneRZT15E3-KlhJbu6_AFBCeDw5zzfkGHJvc1RjzC6zkJM1np2f3XXB9t8UC4YYNZM0Th509l_0pwHtzH6IUSLXIMdoTOmn5YAzqQ\" alt=\"\"\/><figcaption>A grid with two rows and three columns with each cell area numbered. The first row is &#8220;1, 2, 3,&#8221; and the second row is &#8220;4, 5, 6&#8221;<\/figcaption><\/figure>\n<\/div>\n\n\n<p>We can target the <code>div<\/code> using <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/Pseudo-classes\" target=\"_blank\" rel=\"noreferrer noopener\">CSS pseudo-selectors<\/a>, or you can apply classes to the element and target it with that.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-46\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> <span class=\"hljs-selector-tag\">div<\/span><span class=\"hljs-selector-pseudo\">:nth-of-type(1)<\/span> {\n\u00a0\u00a0<span class=\"hljs-attribute\">grid-column-start<\/span>: <span class=\"hljs-number\">2<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-46\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Doing this, the horizontal starting position of the selected grid item will be on the second vertical grid line (column line 2).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/IjnVjdIP3ND3JhKeuh01Lpbvexj5tkOfa1CLQ9xrfuk0nR8nPwYBmO6aavZLV0vgsJYo_e4-a4pCUE-mG634mmbx3KmHvB44AT5f0RofdBtrocQgUHp3r1k-u82JmXk0lebhS35Sm354ky1lwoR--pOly7Deq6cnc0KUciSBASpPV98qqt6d1bNhKw\" alt=\"\"\/><figcaption>A grid with three rows and three columns, and six items. The first item in the grid starts on the second column, which shifts every numbered cell item to the right by 1, overflowing to the left.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>From the image above, the first grid item now begins at column line 2, while the other grid items are pushed over, wrapping onto new rows or creating empty spaces on the grid.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">grid-column-end<\/h3>\n\n\n\n<p>Along with specifying the horizontal starting position of a grid item with <code>grid-column-start<\/code>, we can set where the grid item should end using the <code>grid-column-end<\/code> property.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-47\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> <span class=\"hljs-selector-tag\">div<\/span><span class=\"hljs-selector-pseudo\">:nth-of-type(1)<\/span> {\n\u00a0\u00a0<span class=\"hljs-attribute\">grid-column-start<\/span>: <span class=\"hljs-number\">2<\/span>;\n\u00a0\u00a0<span class=\"hljs-attribute\">grid-column-end<\/span>: <span class=\"hljs-number\">4<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-47\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This makes the first grid item start on column line 2, span across two grid lines, and end on line 4. Using the <code>grid-column-start<\/code> and <code>grid-column-end<\/code> properties, we can effectively control the horizontal starting position of a grid item and how wide it spans across the grid.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/_SsdpX_A41oJJ9qjrDSOevdB-IcBBiY5Ovb4rSfknxyhnB0IyOA8-16aI70FUj6T5OYw8xCol6MUNKzWWaOuHNAQLiIlIZUY_GTwU5wS8Yei0Y73TCaAN3fDexh6PxK1buW7R-tTjohW5RRHfxpxj7nkiUijpKkqQn8DLivgGFBo76F5i5IHv79Ksg\" alt=\"\"\/><figcaption>A grid with three rows and three columns, and six items. The first item in the grid starts on the second column and spans into the third column, which shifts every numbered cell item to the right by 2, overflowing to the left.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">grid-row-start<\/h3>\n\n\n\n<p>The <code>grid-row-start<\/code> property lets us specify the vertical starting position of a grid item along the horizontal (row) grid lines within the grid container. This sets the line where the grid item begins.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-48\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> <span class=\"hljs-selector-tag\">div<\/span><span class=\"hljs-selector-pseudo\">:nth-of-type(2)<\/span> {\n\u00a0\u00a0<span class=\"hljs-attribute\">grid-row-start<\/span>: <span class=\"hljs-number\">1<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-48\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, we set the vertical starting position of the second <code>div<\/code> element to row line 1 using the <code>grid-row-start<\/code> property.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/mGMrXg1c-IJxlt0uQJ_GHVLEsekwlnbRx7Iq4dh9BFy8evS7mv3Y2DCqjtbEKd6n_w0BFmgHafVaiX86h-oxd6TayU99PV7dWHgFQ7WgW4cAYQGZlPNvhZpXqBVEUyGNQeNTy5lAafvQlmWEzEJiCrSlMrki51Vu7CzyWkg4w3YgFYZw_io6fhJ6Jg\" alt=\"\"\/><figcaption>A grid with three rows and three columns and six items. The order of the items is: &#8220;2, 1&#8221;, &#8220;3, 4, 5&#8221;, then &#8220;6&#8221; on each row, respectively.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">grid-row-end<\/h3>\n\n\n\n<p>The CSS <code>grid-row-end<\/code> property lets us specify the vertical ending position of a grid item along the horizontal (row) grid lines within the grid container.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-49\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> <span class=\"hljs-selector-tag\">div<\/span><span class=\"hljs-selector-pseudo\">:nth-of-type(2)<\/span> {\n   <span class=\"hljs-attribute\">grid-row-start<\/span>: <span class=\"hljs-number\">1<\/span>;\n   <span class=\"hljs-attribute\">grid-row-start<\/span>: <span class=\"hljs-number\">4<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-49\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This makes the second grid item start on the first-row line and span down three grid lines, ending on line 4. Using the <code>grid-row-start<\/code> and <code>grid-row-end<\/code> properties, we can effectively control the vertical starting position of a grid item and its height down the grid.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/c9H5Can2wpoOynFXmiqDSLDk8FDObICIT6lo2pC46JPouac-I8mvmLIdcoqscyjaFBd1ksfA3vhvNykq-dzOMdRzQKWKutGnJ_MINhxM1iznDTfvW0hQdQ-MbTtmkiXNcPqQsL5FW6m0sWYz6UHhccCtHr62SBtVLaH6fru00J0rIfThr-OFydVpOA\" alt=\"\"\/><figcaption>A grid with three rows and columns. The first item starts on the second column and stretches to the third column on the first row. The second item starts on the first row and stretches to the third row on the first column. The last four items make up a two-by-two square in the bottom left corner.<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"css-shorthand\">CSS Grid shorthands<\/h2>\n\n\n\n<p>Like most CSS properties, CSS Grid offers a handful of shorthand properties that provide a shorter and more forward way to set the values of multiple CSS Grid properties simultaneously. Using these shorthand properties, we can write more concise (and often, more readable) style sheets, saving time and energy.<\/p>\n\n\n\n<p>Let&#8217;s take a look at these properties.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using a single CSS rule to set all gutters in a grid<\/h3>\n\n\n\n<p>In this guide, we have seen how we can add gutters (spacing) between columns and rows to space them apart using the <code>column-gap<\/code> and <code>row-gap<\/code> grid properties. Using a single property \u2014 <code>gap<\/code>, we can set the values for both the <code>column-gap<\/code> and <code>row-gap<\/code> properties.<\/p>\n\n\n\n<p>The <code>gap<\/code> property is a shorthand for the row-gap and column-gap properties. It lets us add gutters (spacing) between columns and rows.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<p><code>gap: &lt;row-gap&gt; &lt;column-gap&gt;<\/code><\/p>\n\n\n\n<p>The gap property accepts two values where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>&lt;row-gap&gt;<\/code> is the first value passed to the gap property. It sets the value of the <code>row-gap<\/code> property.<\/li><li><code>&lt;column-gap&gt;<\/code> is the second value passed to the gap property and sets the value of the column-gap property. It is an optional value, and if omitted, it&#8217;s set to the same value as <code>&lt;row-gap&gt;<\/code>.&nbsp;<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-50\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> {\n   <span class=\"hljs-attribute\">display<\/span>: grid;\n   <span class=\"hljs-attribute\">gap<\/span>: <span class=\"hljs-number\">20px<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-50\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This adds a space of <code>20px<\/code> between all columns and rows in a grid container and is the same as the code below.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-51\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> { \n\t<span class=\"hljs-attribute\">display<\/span>: grid; \n\t<span class=\"hljs-attribute\">column-row<\/span>: <span class=\"hljs-number\">20px<\/span>;\n\t<span class=\"hljs-attribute\">row-gap<\/span>: <span class=\"hljs-number\">20px<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-51\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Combining horizontal and vertical alignments into single CSS rules<\/h3>\n\n\n\n<p>We talked about the six CSS Grid properties that let us control the alignment of grid items along the rows (incline axis) and columns (block axis) of a grid container. In this section, we&#8217;ll go over the shorthand properties that let us set the alignment of grid items much better.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">place-items<\/h4>\n\n\n\n<p><code>place-items<\/code> is a shorthand property that lets us set the values for the <code>align-items<\/code> and <code>justify-items<\/code> properties in a single declaration. It allows us to control the alignment of all grid items horizontally and vertically at once.<\/p>\n\n\n\n<p>It accepts two values: the first value sets the value of the align-items property, and the second sets that of the justify-items property. If the second value is not specified, the first value will be assigned as the value of both properties.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-52\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> { \n\t<span class=\"hljs-attribute\">display<\/span>: grid; \n\t<span class=\"hljs-attribute\">place-items<\/span>: center; \n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-52\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This sets the value of both the <code>align-items<\/code> and <code>justify-items<\/code> properties to <code>center<\/code>, placing all grid items at the center of their grid areas.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">place-content<\/h4>\n\n\n\n<p><code>place-content<\/code> is a shorthand property that lets us set the values for the grid properties <code>align-content<\/code> and <code>justify-content<\/code> in a single declaration\u2014enabling us to control the horizontal and vertical alignment of the entire grid within the grid container at once.<\/p>\n\n\n\n<p>It accepts two values: the first value sets the value of the <code>align-content<\/code> property, and the second sets that of the <code>justify-content<\/code> property. If the second value is not specified, the first value will be assigned as the value of both properties.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-53\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.container<\/span> { \n\t<span class=\"hljs-attribute\">display<\/span>: grid; \n\t<span class=\"hljs-attribute\">place-content<\/span>: center; \n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-53\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This sets the value of both the <code>align-content<\/code> and <code>justify-content<\/code> properties to <code>center<\/code>, centering the entire grid within the grid container horizontally and vertically.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">place-self<\/h4>\n\n\n\n<p><code>place-items<\/code> is a shorthand property that lets us set the values for the <code>align-self<\/code> and <code>justify-self<\/code> properties in a single declaration. It enables you to control an individual grid item&#8217;s horizontal and vertical alignment within its grid area.<\/p>\n\n\n\n<p>It accepts two values: the first value sets the value of the <code>align-self<\/code> property, and the second sets that of the <code>justify-self<\/code> property. If the second value is not specified, the first value will be assigned as the value of both properties.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-54\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.item<\/span> {\u00a0\n   <span class=\"hljs-attribute\">place-self<\/span>: end center;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-54\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, the value of the <code>align-self<\/code> property is set to &#8216;end,&#8217; which pushes the grid item vertically down to the bottom of its grid cell. And the <code>justify-self<\/code> property is set to <code>center<\/code>, which places the grid item horizontally at the center of its cell.<\/p>\n\n\n\n<p>The result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/AMQoM98peJpRTdHac4FwXFTFDOffwU2kQrm18tQJGcM3czqqFNfz0TAtnu7L_2hKbQRORM8LtjiabfcYybNHwrGtszZqPWsCRz7qNivoh6JvB8B8kJmPcTutZEptwsRjJM8wdIbyq34znfECuONXIVuTymfLAoyKrUfMGqOOM_WPjNNPUnutjTZFBw\" alt=\"\"\/><figcaption>The first grid item in the grid is aligned to the vertical end of the cell space and horizontally at the center, while all of the other items stretch to fill the cell area.<\/figcaption><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">Shortening the CSS for spanning grid items across columns and rows<\/h3>\n\n\n\n<p>Declaring the &#8216;grid-column-start,&#8217; &#8216;grid-column-end,&#8217; &#8216;grid-row-start,&#8217; and &#8216;grid-row-end&#8217; attributes anytime you want to span a grid item across some columns and rows can be somewhat redundant. Let&#8217;s look at some shorthand properties that can help us declare the CSS to span grid items more concisely.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">grid-column<\/h4>\n\n\n\n<p>The grid-column is a shorthand property for the <code>grid-column-start<\/code> and <code>grid-column-end<\/code> properties. It lets us specify the horizontal starting position of a grid item along the column grid lines within the grid container and where the grid item should end.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<p><code>grid-column: column-start \/ column-end;<\/code><\/p>\n\n\n\n<p>The grid-column property accepts two grid line values separated by a slash (\/) where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The first value, <code>column-start<\/code>, is the value of the <code>grid-column-start<\/code> property.<\/li><li>The second value after the slash, <code>column-end<\/code>, is the value of the <code>grid-column-end<\/code> property.<\/li><li>And the slash (\/) serves as a demarcation between these two values as both can contain one or more whitespaces. As a result, passing the slash is required to eliminate errors and ambiguity. Also, as a best practice, adding some whitespace at both sides of the slash (\/) is recommended to make our CSS more readable.<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-55\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.gird-item<\/span> {\n\u00a0\u00a0<span class=\"hljs-attribute\">grid-column<\/span>: <span class=\"hljs-number\">1<\/span> \/ <span class=\"hljs-number\">4<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-55\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The code above tells the grid item to span horizontally from column line 1 to column line 4 in the grid.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">grid-row<\/h4>\n\n\n\n<p>The <code>grid-row<\/code> property works just like the <code>grid-column<\/code> property. It is a shorthand property for the <code>grid-row-start<\/code> and <code>grid-row-end<\/code> properties. It lets us specify the vertical starting position of a grid item along the row grid lines in the grid container and where the grid item should end, spanning down the grid.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-56\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.gird-item<\/span> {\n\u00a0\u00a0<span class=\"hljs-attribute\">grid-column<\/span>: <span class=\"hljs-number\">2<\/span> \/ <span class=\"hljs-number\">5<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-56\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This causes the height of the grid item to span down from row line 2 to row line 5 in the grid.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Bringing it all to a single property: grid<\/h3>\n\n\n\n<p>Lastly, we come to the last of the CSS Grid shorthand properties \u2014 <code>grid<\/code>.<\/p>\n\n\n\n<p>The CSS property <code>grid<\/code> is a shorthand property that lets us set a value for each of the explicit grid container properties such as <code>grid-template-rows<\/code>, <code>grid-template-columns<\/code>, and <code>grid-template-areas<\/code>, and for each implicit grid container properties such as <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/grid-auto-rows\" target=\"_blank\" rel=\"noreferrer noopener\"><code>grid-auto-rows<\/code><\/a>, <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/grid-auto-columns\" target=\"_blank\" rel=\"noreferrer noopener\"><code>grid-auto-columns<\/code><\/a>, and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/grid-auto-flow\" target=\"_blank\" rel=\"noreferrer noopener\"><code>grid-auto-flow<\/code><\/a>, in a single declaration.<\/p>\n\n\n\n<p>&nbsp;Syntax:<\/p>\n\n\n\n<p><code>&lt;grid-template&gt; | &lt;grid-template-rows&gt; \/ [ auto-flow &amp;&amp; dense? ] &lt;grid-auto-columns&gt;? | [ auto-flow &amp;&amp; dense? ] &lt;grid-auto-rows&gt;? \/ &lt;grid-template-columns&gt;<\/code><\/p>\n\n\n\n<p>You can read more about it <a href=\"https:\/\/css-tricks.com\/snippets\/css\/complete-guide-grid\/#aa-grid\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Try out CSS Grid yourself!<\/h2>\n\n\n\n<p>Creating the perfect layout for your website has never been easier. With CSS Grid, you can create complex layouts with as few lines of CSS, minimal development time, and less dependence on external libraries and frameworks.<\/p>\n\n\n\n<p>You should now have a solid grasp of the CSS Grid layout, how it works, and how you can use it to lay out content on your websites. We&#8217;d love you to give it a try.<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed \"\n\tstyle=\"padding-top: 85%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?language=html\" width=\"640\" height=\"544\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p><strong><em>This article was written by<\/em><\/strong> <a href=\"https:\/\/twitter.com\/Victor_codejs\" target=\"_blank\" rel=\"noreferrer noopener\"><strong><em>Victor<\/em><\/strong><\/a><strong><em>. Victor is a front-end developer and technical writer interested in Jamstack. He has a knack for breaking down developer concepts into clear, understandable bits for others.<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Seriously, everything you ever wanted to know about CSS Grid is in this guide.<\/p>\n","protected":false},"author":12,"featured_media":17331,"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-17330","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\/17330","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\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=17330"}],"version-history":[{"count":4,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/17330\/revisions"}],"predecessor-version":[{"id":34512,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/17330\/revisions\/34512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/17331"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=17330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=17330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=17330"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=17330"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=17330"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=17330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}