<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luis Santiago</title>
    <description>The latest articles on DEV Community by Luis Santiago (@luisg_santiago).</description>
    <link>https://dev.to/luisg_santiago</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F897792%2F78029b21-3f09-487d-bbe2-c77fe2d86877.jpeg</url>
      <title>DEV Community: Luis Santiago</title>
      <link>https://dev.to/luisg_santiago</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luisg_santiago"/>
    <language>en</language>
    <item>
      <title>How to Start Working with CSS Grid</title>
      <dc:creator>Luis Santiago</dc:creator>
      <pubDate>Wed, 27 Jul 2022 17:29:56 +0000</pubDate>
      <link>https://dev.to/devsatasurion/how-to-start-working-with-css-grid-4h7k</link>
      <guid>https://dev.to/devsatasurion/how-to-start-working-with-css-grid-4h7k</guid>
      <description>&lt;p&gt;When I started working on new features at the beginning of my software development career, I mainly focused on application logic. Whenever I needed to change the styling I would quickly update the CSS, ensured it looked the way it was required, and switch back to the development portion.&lt;/p&gt;

&lt;p&gt;With this approach, I didn't feel like I was getting a good understanding of how my CSS code was behaving in the background.&lt;/p&gt;

&lt;p&gt;I decided to dive deeper into CSS. In this article, I will show you how to build a simple layout using CSS grid properties. We will see how CSS grid is a great layout system for content arrangement and application responsiveness.&lt;/p&gt;

&lt;h2&gt;
  
  
  Grid VS Flexbox
&lt;/h2&gt;

&lt;p&gt;Before I jump into the code for a CSS grid example, let's take a look at a high-level comparison between flex and grid layouts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flex layout&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fce6vcui8x58rnmetvnw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fce6vcui8x58rnmetvnw7.png" alt="Flex layout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Grid layout&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5wm0jeocv8rnswxpzqg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5wm0jeocv8rnswxpzqg.png" alt="Grid layout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you see in the image above, Flexbox behaves in a one-dimensional flow when arranging content. Rows are independent of others.&lt;/p&gt;

&lt;p&gt;On the other hand, Grid is two-dimensional, with rows and columns affecting each other to form the entire layout.&lt;/p&gt;

&lt;p&gt;The previous industry recommendation was to use Flexbox for UI elements but to use Grid for major layouts. The current industry recommendation is to &lt;strong&gt;use Flexbox and Grid where they make the most sense&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  What we are Learning
&lt;/h2&gt;

&lt;p&gt;Let's build a quick layout, explore grid properties, and understand how they help us improve our application.&lt;/p&gt;

&lt;p&gt;To follow the lines of code below, you will only need some foundational experience with Javascript, React, and CSS.&lt;/p&gt;

&lt;p&gt;Here is what the final layout is supposed to look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzfp979d2n4b1slpgvkse.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzfp979d2n4b1slpgvkse.png" alt="Required Layout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After looking at the required layout I start thinking about translating this into a two-dimensional grid layout. Here is a drawing of what the layout looks like in my head before I start writing the first line of code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffyev81hz4ux2mvj0k7lx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffyev81hz4ux2mvj0k7lx.jpeg" alt="Initial mock"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To avoid distractions and help you focus on the CSS code, I have moved all content like images, styling, and text to their respective components&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Image /&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;DeviceDescription /&amp;gt;&lt;/code&gt;, and &lt;code&gt;&amp;lt;DeviceDeductible/&amp;gt;&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here is what our layout without CSS grid looks like so far:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmka32u3h3iaplbeosxz2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmka32u3h3iaplbeosxz2.png" alt="Initial layout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how I placed each of these components inside a &lt;code&gt;div&lt;/code&gt; element and assigned &lt;code&gt;device-image&lt;/code&gt;, &lt;code&gt;device-description&lt;/code&gt;, and &lt;code&gt;device-deductible-container&lt;/code&gt; as their className properties, respectively.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;display: grid&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Let's apply &lt;code&gt;display: grid&lt;/code&gt; to &lt;code&gt;replacement-container&lt;/code&gt;. This will allow us to treat all &lt;code&gt;div&lt;/code&gt; elements within this &lt;code&gt;div&lt;/code&gt; as a grid.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffmgf2chfylfrtsnwtzol.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffmgf2chfylfrtsnwtzol.png" alt="Initial grid"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since no other properties are assigned to our grid, the CSS grid system does its best placing each &lt;code&gt;div&lt;/code&gt; within a row by default. Leaving us with one column and three rows.&lt;/p&gt;

&lt;p&gt;CSS Grid counts rows and columns by starting with the number 1. Notice how the first row goes from 1 to 2. The second row goes from 2 to 3. The third row goes from 3 to 4. This is important to understand when we want our content to span across the grid.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;grid-template-columns&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;We want our final layout to have two columns. We can tell CSS grid how many columns we want in our layout by assigning a column size value per column separated by space using &lt;code&gt;grid-template-columns&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0hrjg3vccclkrjkdmo9y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0hrjg3vccclkrjkdmo9y.png" alt="Template columns"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case, the keyword auto (line 3) specifies the size for the first column to be automatically assigned based on the content inside the grid cell.&lt;/p&gt;

&lt;p&gt;We have assigned &lt;code&gt;1fr&lt;/code&gt; to the second column. This means the column takes one entire fraction of the space available between the two columns.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;grid-column-gap&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;Let's include &lt;code&gt;grid-column-gap&lt;/code&gt; to add a space of &lt;code&gt;20px&lt;/code&gt; between the two columns:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flap6d5mslzyh861hsnwj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flap6d5mslzyh861hsnwj.png" alt="Column gap"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;grid-row&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;So far, our layout has two columns. But our image needs to span across the first and second row to move the deductible section to the second column.&lt;/p&gt;

&lt;p&gt;For this, we need the &lt;code&gt;grid-row&lt;/code&gt; property. The syntax to span our image grid cell is &lt;code&gt;1/3&lt;/code&gt; . With this, CSS grid knows we are spanning our grid cell &lt;code&gt;device-image&lt;/code&gt; across the first row (1 to 2) and our second row (2 to 3).&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ctygxxzebjr61f4tu3u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ctygxxzebjr61f4tu3u.png" alt="Image span"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our layout is almost done. The only section left to arrange is the deductible description. And for this, we'll use a grid within a grid. &lt;/p&gt;

&lt;p&gt;A &lt;code&gt;div&lt;/code&gt; inside the &lt;code&gt;DeviceDeductible&lt;/code&gt; component with className of &lt;code&gt;device-deductible&lt;/code&gt; contains the deductible description. We will make this element a grid and place its content in one row and three columns. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fszzik113c50yo7393sj3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fszzik113c50yo7393sj3.png" alt="Device deductible"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how we applied the same Grid properties in &lt;code&gt;replacement-container&lt;/code&gt; with a difference in the syntax for &lt;code&gt;grid-template-columns&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The  syntax &lt;code&gt;repeat(3, 1fr)&lt;/code&gt; is equivalent to &lt;code&gt;1fr 1fr 1fr&lt;/code&gt;. This distributes one fraction of the available space to each column evenly.&lt;/p&gt;

&lt;p&gt;That is all I have for now. I hope this gives you a good understanding of the layout possibilities available when using CSS Grid and confidence for you to have another tool under your development belt. Have fun building with CSS grid.&lt;/p&gt;

&lt;p&gt;Keep coding, keep learning, and never give up.&lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>cssgrid</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
