{"id":52,"date":"2017-11-08T04:07:41","date_gmt":"2017-11-08T04:07:41","guid":{"rendered":"http:\/\/local.nitsuga.com.blog\/?p=52"},"modified":"2017-12-23T04:47:39","modified_gmt":"2017-12-23T04:47:39","slug":"use-css-grid-to-layout-your-next-user-interface","status":"publish","type":"post","link":"http:\/\/nitsuga.com\/blog\/2017\/11\/08\/use-css-grid-to-layout-your-next-user-interface\/","title":{"rendered":"An Exploration Into CSS Grid"},"content":{"rendered":"<p>I&#8217;ve relied on building webpage layouts using a variety of techniques &#8211; tables, floating divs, and flexbox. Not to mention CSS and JavaScript hacks to make older versions of Internet Explorer render box model layouts correctly. Lately I&#8217;ve been learning a lot about CSS Layout. It is the latest CSS technology for building layouts optimized for the web and I\u2019m amazed at what it can do. Flexbox is great, but it has its limitations. It only allows you to build one-dimensional layouts. CSS Grid, however, enables you to build layouts in two dimensions simultaneously. Now that CSS Grid comes shipped with browsers such as Safari, Chrome, Opera, Firefox, and Edge, you can start using this new layout system in your projects today. This article is an exploration into CSS Grid, the latest in CSS technology, which I think will revolutionize the way designers and developers build interfaces for the web.<\/p>\n<h2>A Simple Grid Layout<\/h2>\n<p>Let\u2019s build a simple layout using CSS Grid. First, define the <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#grid-model\">grid container<\/a>. Set an element\u2019s display value to \u201cgrid\u201d.<\/p>\n<pre><code class=\"language-css\">#grid {\r\n   display: grid;\r\n}<\/code><\/pre>\n<p><a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#grid-items\">Grid items<\/a> are the children of the grid. The items of the grid we define here are the header, left navigation, content, and footer. Give each of them a name, or a <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#grid-placement-slot\">custom identifier<\/a>, by assigning it to their grid-area property.<\/p>\n<pre>\r\n<code class=\"language-css\">.header {\r\n   grid-area: header;\r\n}\r\n\r\n.left-nav {\r\n   grid-area: left-nav;\r\n}\r\n\r\n.content {\r\n   grid-area: content;\r\n}\r\n\r\n.footer {\r\n   grid-area: footer;\r\n}<\/code><\/pre>\n<p>You define the width of columns by setting sizing values to the grid-template-columns property. As for the height of rows, you set the sizing values to the grid-template-rows property. The grid\u2019s columns and rows are known as <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#grid-track-concept\">Grid Tracks.<\/a><\/p>\n<pre>\r\n<code class=\"language-css\">grid-template-rows: 100px auto 25%;\r\ngrid-template-columns: 200px auto;<\/code>\r\n<\/pre>\n<p>Set the values for the grid-template-areas property. In this property, each grid item&#8217;s name is arranged in an order in which you want them to be displayed on the grid. A row represents a string set. For example, &#8220;header header&#8221; is a row. And so is &#8220;left-nav content.&#8221; The columns are represented by names, separated by a space. Note that &#8220;header&#8221; is shown twice since it takes up both columns in that row.<\/p>\n<pre>\r\n<code class=\"language-css\">grid-template-areas: \"header header\"\r\n                     \"left-nav content\"\r\n                     \"footer footer\";<\/code><\/pre>\n<p>Here\u2019s the HTML. Each nested <code class=\"language-markup\" >&lt;div&gt;<\/code>represents a grid item.<\/p>\n<pre><code class=\"language-markup\" >&lt;div class=\"header\"&gt;header&lt;\/div&gt;\r\n&lt;div class=\"left-nav\"&gt;left-nav&lt;\/div&gt;\r\n&lt;div class=\"content\"&gt;content&lt;\/div&gt;\r\n&lt;div class=\"footer\"&gt;footer&lt;\/div&gt;<\/code><\/pre>\n<p>The result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/local.nitsuga.com.blog\/wp-content\/uploads\/2017\/11\/simple-grid.png\" alt=\"\" width=\"720\" height=\"425\" class=\"aligncenter size-full wp-image-132\" srcset=\"http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/simple-grid.png 720w, http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/simple-grid-300x177.png 300w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/p>\n<h2>Useful Grid Layout Features<\/h2>\n<p>As you can see, it didn\u2019t take much effort to get a simple grid layout prototype up and running. However, Grid Layout has much more to offer. Let\u2019s utilize other Grid Layout features to customize our grid even further.<\/p>\n<h3>Track Sizing Values<\/h3>\n<p>When you define the <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#track-sizing\">track sizing<\/a> of a grid, the <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#track-list\">track listing<\/a> in the grid-template-rows and grid-template-columns can also accept track size <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#track-sizing\">values<\/a> other than length, auto, and percentage. Consider the following example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/local.nitsuga.com.blog\/wp-content\/uploads\/2017\/11\/track-sizing.png\" alt=\"\" width=\"720\" height=\"425\" class=\"aligncenter size-full wp-image-139\" srcset=\"http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/track-sizing.png 720w, http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/track-sizing-300x177.png 300w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/p>\n<pre><code class=\"language-css\">grid-template-rows: 100px 2fr 1fr;\r\ngrid-template-columns: minmax(200px, 500px) auto;<\/code><\/pre>\n<p>Here, we can use a track size value returned by the <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#valdef-grid-template-columns-minmax\">minmax()<\/a> function (exclusive to Grid Layout) and a <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#fr-unit\">fr unit<\/a> value. They both are used to create a responsive grid.<\/p>\n<p>The minmax() function takes two parameters: a minimum value and a maximum value. In our example above, the minimum is 200px and the maximum is 500px. Resizing the viewport results in function returning column width values between the minimum and maximum values.<\/p>\n<p>The fr is known as a \u201cfractional unit.\u201d If a grid item has a track size value for either a row or column of one fractional unit, it takes up a fraction of available grid container space. In the example above, the remainder of the grid\u2019s height (the height of the grid minus the height of row 1), is distributed between row 2 and row 3. Two-thirds (2fr) of the height goes to row 2 and one-third (1fr) of the height goes to row 3.<\/p>\n<h3>Responsive grid<\/h3>\n<p>For a responsive grid, modify the following grid container properties per media query: grid-template-rows, grid-template-columns, and grid-template-areas.<\/p>\n<pre><code class=\"language-css\">\/* 321px and up *\/\r\n@media(min-width: 321px) {\r\n   #grid {\r\n      grid-template-rows: 100px 2fr 1fr;\r\n      grid-template-columns: 200px auto;\r\n      grid-template-areas: \"header header\"\r\n                           \"left-nav content\"\r\n                           \"footer footer\";\r\n   }\r\n}\r\n\r\n\/* 320px and below *\/\r\n@media(max-width: 320px) {\r\n   #grid {\r\n      grid-template-rows: 100px 1fr 3fr 100px;\r\n      grid-template-columns: 100%;\r\n      grid-template-areas: \"header\"\r\n                           \"left-nav\"\r\n                           \"content\"\r\n                           \"footer\";\r\n   }\r\n}<\/code><\/pre>\n<p>Notice how the grid-template-areas value is set up for the 320px and below media query. Each<br \/>\nnamed area forms one column, since this view is for mobile phone devices.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/responsive-grid-1.gif\" alt=\"\" width=\"720\" height=\"425\" class=\"aligncenter size-full wp-image-204\" \/><\/p>\n<h3>Named Lines<\/h3>\n<p><a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#grid-line-concept\">Although you can reference grid lines by numerical index<\/a>, Grid Layout allows you to create custom names for grid lines. This feature helps identify, maintain, and provide a meaningful reference to grid lines and areas in your stylesheet. Use the named lines as a value in the grid-template-rows property and grid-template-columns property of a grid. Provide any name you choose for a line, but \u2018<a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#named-lines\">span<\/a>\u2019. You surround the custom line name in brackets. The same line can also have multiple names. For example, you may want to refer to a line that signifies the end of a column and the start of an adjacent column. However, I found that multiple names can be overkill, so I like the idea of having one name per line.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/local.nitsuga.com.blog\/wp-content\/uploads\/2017\/11\/named-lines.png\" alt=\"\" width=\"720\" height=\"425\" class=\"aligncenter size-full wp-image-159\" srcset=\"http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/named-lines.png 720w, http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/named-lines-300x177.png 300w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/p>\n<pre><code class=\"language-css\">#grid {\r\n\r\n\tgrid-template-rows: [row1-start] 100px [row2-start] 2fr [row3-start] 1fr [row3-end];\r\n\tgrid-template-columns: [col1-start] minmax(200px, 500px) [col2-start] auto [col2-end];\r\n}<\/code><\/pre>\n<p>Interestingly, you can have multiple lines with the same name. But how do you reference a specific line if there are lines sharing the same name? Let&#8217;s see an example of how to accomplish this. In this example, we will need to pinpoint the correct line names to place our grid items on specific locations on the grid.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/local.nitsuga.com.blog\/wp-content\/uploads\/2017\/11\/same-named-lines.png\" alt=\"\" width=\"720\" height=\"425\" class=\"aligncenter size-full wp-image-172\" srcset=\"http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/same-named-lines.png 720w, http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/same-named-lines-300x177.png 300w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/p>\n<p>Let&#8217;s use the <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#repeat-notation\">repeat()<\/a> function to create a grid of 3 rows and 2 columns:<\/p>\n<pre><code class=\"language-css\">#grid {\r\n\r\n\tdisplay: grid;\r\n\tgrid-template-rows: repeat(3, [row-start] 1fr [row-end]);\r\n\tgrid-template-columns: repeat(2, [col-start] 1fr [col-end]);\r\n}<\/code><\/pre>\n<p>To place each item on the grid, we set the values for their grid-row property and grid-column property. The values consists of the line name and a number. The number corresponds to the row or column number of the grid:<\/p>\n<pre><code class=\"language-css\">.header {\r\n\r\n\tgrid-row: row-start 1\/row-end 1;\r\n\tgrid-column: col-start 1\/col-end 2;\r\n}\r\n\r\n.left-nav {\r\n\r\n\tgrid-row: row-start 2\/row-end 2;\r\n\tgrid-column: col-start 1\/col-end 1;\r\n}\r\n\r\n.content {\r\n\r\n\tgrid-row: row-start 2\/row-end 2;\r\n\tgrid-column: col-start 2\/col-end 2;\r\n}\r\n\r\n.footer {\r\n\r\n\tgrid-row: row-start 3\/row-end 3;\r\n\tgrid-column: col-start 1\/col-end 2;\r\n}<\/code><\/pre>\n<h3>The span keyword<\/h3>\n<p>You can place an item on the grid by number. The number references the grid&#8217;s column or row number. Then, you can use the <a href=\"https:\/\/www.w3.org\/TR\/css-grid-1\/#common-uses-numeric\">span<\/a> keyword to span across adjacent rows or columns. The item can also span into an already defined area that occupies that space on the grid. For example, if we span the left-nav from row two into row 3, it is essentially spanning itself into the area defined as the footer. To visually see the overlapping areas, just set the z-index property for both items.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/local.nitsuga.com.blog\/wp-content\/uploads\/2017\/11\/span-keyword.png\" alt=\"\" width=\"720\" height=\"425\" class=\"aligncenter size-full wp-image-176\" srcset=\"http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/span-keyword.png 720w, http:\/\/nitsuga.com\/blog\/wp-content\/uploads\/2017\/11\/span-keyword-300x177.png 300w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/p>\n<pre><code class=\"language-css\">.left-nav {\r\n\r\n    grid-row: 2\/span 3;\r\n    grid-column: 1;\r\n    z-index: 2;\r\n}\r\n\r\n.footer {\r\n\r\n    grid-row: 3;\r\n    grid-column: 1\/span 2;\r\n    z-index: 1;\r\n}<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Creating a simple layout and getting started with CSS Grid couldn&#8217;t be any easier. You have full control of the position, size, alignment, and layering (via z-index) of the items you place on the grid. However, the specification comes with so many more features I didn\u2019t go over in this article (I&#8217;ll save them for future articles). Until then, my Grid exploration continues. I hope this article was inspiration enough for you to go and explore for yourself, the latest in CSS technology, that will revolutionize the way designers and developers build for the web.\n<\/p><\/div>\n<div class=\"credit-wrap\">\n<p class=\"photo-credit\">Featured photo by &nbsp;<\/p>\n<p><a style=\"background-color:#1191B0;color:white;text-decoration:none;padding:4px 6px;font-family:-apple-system, BlinkMacSystemFont, &quot;San Francisco&quot;, &quot;Helvetica Neue&quot;, Helvetica, Ubuntu, Roboto, Noto, &quot;Segoe UI&quot;, Arial, sans-serif;font-size:12px;font-weight:bold;line-height:1.2;display:inline-block;border-radius:3px;\" href=\"https:\/\/unsplash.com\/@markusspiske?utm_medium=referral&amp;utm_campaign=photographer-credit&amp;utm_content=creditBadge\" target=\"_blank\" rel=\"noopener noreferrer\" title=\"Download free do whatever you want high-resolution photos from Markus Spiske\"><span style=\"display:inline-block;padding:2px 3px;\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"height:12px;width:auto;position:relative;vertical-align:middle;top:-1px;fill:white;\" viewBox=\"0 0 32 32\"><title>unsplash-logo<\/title><path d=\"M20.8 18.1c0 2.7-2.2 4.8-4.8 4.8s-4.8-2.1-4.8-4.8c0-2.7 2.2-4.8 4.8-4.8 2.7.1 4.8 2.2 4.8 4.8zm11.2-7.4v14.9c0 2.3-1.9 4.3-4.3 4.3h-23.4c-2.4 0-4.3-1.9-4.3-4.3v-15c0-2.3 1.9-4.3 4.3-4.3h3.7l.8-2.3c.4-1.1 1.7-2 2.9-2h8.6c1.2 0 2.5.9 2.9 2l.8 2.4h3.7c2.4 0 4.3 1.9 4.3 4.3zm-8.6 7.5c0-4.1-3.3-7.5-7.5-7.5-4.1 0-7.5 3.4-7.5 7.5s3.3 7.5 7.5 7.5c4.2-.1 7.5-3.4 7.5-7.5z\"><\/path><\/svg><\/span><span style=\"display:inline-block;padding:2px 3px;\">Markus Spiske<\/span><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve relied on building webpage layouts using a variety of techniques &#8211; tables, floating divs, and flexbox. Not to mention<\/p>\n<a class=\"read-more-link\" href=\"http:\/\/nitsuga.com\/blog\/2017\/11\/08\/use-css-grid-to-layout-your-next-user-interface\/\">Read more<\/a>","protected":false},"author":1,"featured_media":184,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":113,"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":208,"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/posts\/52\/revisions\/208"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/media\/184"}],"wp:attachment":[{"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nitsuga.com\/blog\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}