<?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: Ravi Sharma</title>
    <description>The latest articles on DEV Community by Ravi Sharma (@rvisharma).</description>
    <link>https://dev.to/rvisharma</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F50973%2Fa074299b-4a10-4e6a-a258-76ec0369f017.jpeg</url>
      <title>DEV Community: Ravi Sharma</title>
      <link>https://dev.to/rvisharma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rvisharma"/>
    <language>en</language>
    <item>
      <title>CSS Grid Introduction and Terminologies</title>
      <dc:creator>Ravi Sharma</dc:creator>
      <pubDate>Sun, 10 Jan 2021 19:41:25 +0000</pubDate>
      <link>https://dev.to/rvisharma/css-grid-introduction-and-terminologies-hhl</link>
      <guid>https://dev.to/rvisharma/css-grid-introduction-and-terminologies-hhl</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pisB7R8Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gk811ywxz8tor529yyzw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pisB7R8Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gk811ywxz8tor529yyzw.png" alt="css-grid"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CSS Grid came into the limelight recently(&lt;a href="https://www.youtube.com/watch?v=Felq4z_rdPQ"&gt;a few years ago&lt;/a&gt;) but its presence has been there since old IE days. CSS Grid solves the problem area of two-dimensional layouts. There have been multiple attempts to solve the designing layout using other properties in CSS - position hacks, floats, table, flex are such very popular examples with comes with their own set of problems.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QEJGg-VP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hegj5i6o41cmbjnc8hhl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QEJGg-VP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/hegj5i6o41cmbjnc8hhl.png" alt="i-have-flex"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Syntactically and visually, we can achieve the 2D layouts using flexbox or tables then why do we need a grid in the picture? Grid brings a very unique quality with itself – &lt;strong&gt;Semantics&lt;/strong&gt;. Tables are semantically for table layouts or tabular structured data, flexbox is semantically there for 1-dimensional flexible layouts (navigation bars, etc). In that same essence, Grids are for 2-dimensional layouts.&lt;/p&gt;

&lt;p&gt;CSS is a declarative language, when you need something to be of let's say red color. You declare it as color red by adding a declaration &lt;code&gt;color: red;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iTKewffN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g817rotzengd47vj5j77.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iTKewffN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/g817rotzengd47vj5j77.png" alt="i-know-flexbox"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;excuse this poor imitation of xkcd, the original one for regex is a lot better - &lt;a href="https://xkcd.com/208/"&gt;https://xkcd.com/208/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the grid was not available we used tables and flexbox to create grid like layouts which was not semantically correct and also hard to pull off in some way. Tables and flexbox usages still hold place today for what they were meant to be. In the end, &lt;strong&gt;CSS Grid is just another tool in our toolbox&lt;/strong&gt;. And as the saying goes - &lt;em&gt;use the right tool, for the right job.&lt;/em&gt; It blends well with other layout systems like it is perfectly valid to create a flexbox container inside a CSS grid element.&lt;/p&gt;

&lt;p&gt;In this post, we will uncover the very fundamental terminologies of the CSS grid that we need to get acquainted with, and then in upcoming posts, we'll dive into other aspects.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Grid container

&lt;ul&gt;
&lt;li&gt;Formatting context&lt;/li&gt;
&lt;li&gt;Block level and Inline level grid container&lt;/li&gt;
&lt;li&gt;Block grid and Block containers&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Grid item&lt;/li&gt;
&lt;li&gt;Grid lines&lt;/li&gt;
&lt;li&gt;Grid track&lt;/li&gt;
&lt;li&gt;Grid cell&lt;/li&gt;
&lt;li&gt;Grid area&lt;/li&gt;
&lt;li&gt;Rows and columns&lt;/li&gt;
&lt;li&gt;The end&lt;/li&gt;
&lt;li&gt;References mentioned in the post&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Grid container
&lt;/h2&gt;

&lt;p&gt;So, what are grid container? In &lt;em&gt;simple&lt;/em&gt; terms, grid container is &lt;strong&gt;an element that defines &lt;em&gt;grid formatting context&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R2ml57kl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tmd5n2tguu0kq17wvlzq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R2ml57kl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tmd5n2tguu0kq17wvlzq.png" alt="grid-formatting"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What's this grid formatting context?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Formatting context
&lt;/h3&gt;

&lt;p&gt;To answer that, let's understand what is &lt;em&gt;formatting context&lt;/em&gt; in general. Formatting contexts define the rules on how the content is going to layout visually and with the combination of other properties, how it affects the layout. This is not something new in CSS, it has been around since the beginning. You may not have known the term before, but for sure you have experienced it.&lt;/p&gt;

&lt;p&gt;For example, when you use &lt;code&gt;display: flex;&lt;/code&gt; it sets the element to be of &lt;em&gt;flex formatting context&lt;/em&gt; and then by using flex related properties we can change the layout of its child. There are many such formatting contexts, like when you use a &lt;code&gt;&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;/code&gt; tag, it creates an &lt;em&gt;inline formatting context&lt;/em&gt; which alters the behavior of the box model as compared to the default &lt;em&gt;block formatting context&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;There are many helpful resources available on internet to understand formatting context. I would recommend an &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flow_Layout/Intro_to_formatting_contexts"&gt;introductory article&lt;/a&gt; on MDN and this article by Rachel Andrew on &lt;a href="https://www.smashingmagazine.com/2017/12/understanding-css-layout-block-formatting-context/"&gt;CSS Layout And The Block Formatting Context&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Coming back to grid formatting context. How do we create such a thing? By adding the declaration &lt;code&gt;display: grid;&lt;/code&gt; or  &lt;code&gt;display: inline-grid;&lt;/code&gt; to an element. With this, we create a new grid formatting context and the element will be known as &lt;strong&gt;grid container&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Block level and Inline level grid container
&lt;/h3&gt;

&lt;p&gt;We can create grid containers in two fashion – block-level and inline-level. Block-level grid follows the block direction (top-to-bottom for &lt;em&gt;most&lt;/em&gt; languages) and gets placed on its own line like a &lt;code&gt;div, section, p&lt;/code&gt;. Inline level grid, follows the inline direction (left-to-right) placed along with other inline elements for eg. &lt;code&gt;span, strong&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/rvisharma/embed/abmjaVa?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📌 If an inline-grid container has &lt;code&gt;position: absolute;&lt;/code&gt; or &lt;code&gt;float&lt;/code&gt; in its properties, then &lt;code&gt;inline&lt;/code&gt; gets dropped and it becomes a regular block grid container.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Block grid and Block containers
&lt;/h3&gt;

&lt;p&gt;Block grids look like they behave similar to the regular block containers which are created by elements such as - div, main, section, etc... But there are few differences between block-level grid and block containers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interaction with floated elements&lt;/li&gt;
&lt;li&gt;Margin collapsing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Interaction with floated elements&lt;/strong&gt; — When a parent element contains a floated element, the child  &lt;code&gt;div&lt;/code&gt; (or any other block element) goes under the floated element. But in the case of the grid container, &lt;strong&gt;it does not&lt;/strong&gt;. Check out the pen below.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/rvisharma/embed/yLaqQgy?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Margin collapsing&lt;/strong&gt; — Grid containers &lt;strong&gt;do not&lt;/strong&gt; collapse vertical margin, whereas block elements do. Margin collapsing is itself an interesting topic. To know more about margin and margin-collapse and how it affects layout, check out this super awesome post &lt;em&gt;again&lt;/em&gt; by Rachel Andrew - &lt;a href="https://www.smashingmagazine.com/2019/07/margins-in-css/"&gt;Margins in CSS&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Grid item
&lt;/h2&gt;

&lt;p&gt;Now that we understand what grid containers are, grid elements are straightforward. Direct children (including anonymous) of a grid container are known are grid elements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y2wroLld--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s1cczqelqf1wezs785gw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y2wroLld--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s1cczqelqf1wezs785gw.png" alt="grid-item"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is no such restriction on defining the layout of grid items. A grid item can be a flexbox container by adding declaration &lt;code&gt;display: flex;&lt;/code&gt;. Heck, we can go ahead and add &lt;code&gt;display: grid;&lt;/code&gt; to the grid item, and it behaves as a grid container for its child item! This means it behaves as a grid-item for its parent and grid-container for its children. You can call it a nested grid. (&lt;em&gt;not subgrid - that's different&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nojN7xTO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t83l77dwbxwcva1agsva.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nojN7xTO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t83l77dwbxwcva1agsva.png" alt="grid-item-flex-container"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Grid lines
&lt;/h2&gt;

&lt;p&gt;Grid lines are the basic unit of the grid on which other parts of the grid are built upon. How do we create grid lines? We don't. It's something that gets created (&lt;em&gt;logically&lt;/em&gt;) when we define grid template in case of the explicit grid and when grid automatically places content in case of the implicit grid. We'll uncover what explicit and implicit grid is in another post.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dNnKR_2w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k4sb74y6g1uuios5myk0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dNnKR_2w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k4sb74y6g1uuios5myk0.png" alt="grid-lines"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can refer to the grid lines as numbers via CSS and place the grid items. They start as 1 from the starting edge of flow direction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Grid track
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fQfJdpyL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bt4sb2gnbfgahm8m4sh5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fQfJdpyL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bt4sb2gnbfgahm8m4sh5.png" alt="grid-tracks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The area between 2 adjacent grid lines is known as a grid track. The direction of the grid tracks can be vertical and horizontal. The vertical direction is the columns and the horizontal direction is rows. (this changes based on the writing mode)&lt;/p&gt;

&lt;h2&gt;
  
  
  Grid cell
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0DOH0wIf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0fzw77pqp7v9by3050ta.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0DOH0wIf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0fzw77pqp7v9by3050ta.png" alt="grid-cells"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The area between 4 grid lines is called a grid cell, it is the smallest unit of area in a grid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Grid area
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cQUtMxkg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/odj5emwg1uyugrqmzjfi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cQUtMxkg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/odj5emwg1uyugrqmzjfi.png" alt="grid-area"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A grid area is the combination of one or more grid cells together. These are accessible via CSS grid properties. We can define the grid areas and place our grid items over those areas.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📌 There is no default direct relation between grid areas and grid items. They are not bounded together by default. Using CSS, we attach the grid items to the grid areas. You can think of grid items are physical elements and grid areas as logical.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since the control of assigning grid items to grid areas is on us, we can keep some of the grid cells as empty or overlap them with other grid items.&lt;/p&gt;

&lt;p&gt;If we don't define any grid area, a default grid area will be created from start to end and our content will be placed on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rows and columns
&lt;/h2&gt;

&lt;p&gt;In an explicit grid, we define the track size and the number of rows/columns using &lt;code&gt;grid-template-columns&lt;/code&gt; and &lt;code&gt;grid-template-rows&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is how we define 3 columns and 2 rows with their corresponding track sizes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.grid-container&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c"&gt;/* 3 columns */&lt;/span&gt;
  &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt; &lt;span class="m"&gt;200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c"&gt;/* 2 Rows */&lt;/span&gt;
  &lt;span class="py"&gt;grid-template-rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt; &lt;span class="m"&gt;300px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Few things to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The track size values can be fixed widths (px, em, rem, %, ch, ...) or flexible widths (fractional spaces, content-based space)&lt;/li&gt;
&lt;li&gt;There is no limitation on defining the number of rows and columns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here we have defined only 2 rows, those rows/tracks are part of the &lt;strong&gt;explicit grid&lt;/strong&gt;. If the number of grid items increases which cannot fit inside the explicit area of the grid, then the grid will generate a new row automatically as part of &lt;strong&gt;implicit grid&lt;/strong&gt;. Similarly for columns.&lt;/p&gt;

&lt;p&gt;If we place a grid item outside of the defined row, columns. New grid lines and tracks will be created as part of the implicit grid.&lt;/p&gt;

&lt;p&gt;How do we place grid items on grid areas? We'll cover that in the upcoming post.&lt;/p&gt;

&lt;h2&gt;
  
  
  The end
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rrst2tHr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3nxneqqugky9ng3b2ufg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rrst2tHr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3nxneqqugky9ng3b2ufg.jpg" alt="canvas"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🎨 All drawings were made using &lt;a href="https://excalidraw.com/#json=5806952810545152,KdPJk6L2kcXKiisdRSFSiA"&gt;Excalidraw&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References mentioned in the post
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;📚 &lt;a href="https://meyerweb.com/eric/books/css-tdg/"&gt;CSS - The definitive guide&lt;/a&gt; — &lt;em&gt;Eric A. Meyer &amp;amp; Estelle Weyl&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;📹 &lt;a href="https://www.youtube.com/watch?v=Felq4z_rdPQ"&gt;Start using CSS Grid Layout&lt;/a&gt; — &lt;em&gt;Rachel Andrew&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;🔖 &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flow_Layout/Intro_to_formatting_contexts"&gt;Introduction to formatting contexts&lt;/a&gt; — &lt;em&gt;MDN&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;🔖 &lt;a href="https://www.smashingmagazine.com/2017/12/understanding-css-layout-block-formatting-context/"&gt;Understanding CSS Layout And The Block Formatting Context&lt;/a&gt; — &lt;em&gt;Rachel Andrew&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;🔖 &lt;a href="https://www.smashingmagazine.com/2019/07/margins-in-css/"&gt;Margins in CSS&lt;/a&gt; — &lt;em&gt;Rachel Andrew&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>grid</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Where do you prefer your conditionals – inside or outside?</title>
      <dc:creator>Ravi Sharma</dc:creator>
      <pubDate>Sat, 26 Dec 2020 10:53:43 +0000</pubDate>
      <link>https://dev.to/rvisharma/where-do-you-prefer-your-conditionals-inside-or-outside-52c0</link>
      <guid>https://dev.to/rvisharma/where-do-you-prefer-your-conditionals-inside-or-outside-52c0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;span&gt;Cover photo by &lt;a href="https://unsplash.com/@emilymorter?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Emily Morter&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/question?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Many times when i'm writing a piece of logic, i face this dilemma while calling a function. If a function needs to be invoked under certain condition, should i put the conditional check inside the function and do early return, OR first do an conditional check and the invoke.&lt;/p&gt;

&lt;p&gt;Both have their pros and cons, IMO&lt;/p&gt;

&lt;h2&gt;
  
  
  Conditional inside the method
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Pros - My execution path remains free of branches and the flow looks linear and simple to read.&lt;/li&gt;
&lt;li&gt;Cons - The intent and actual execution would differ in some cases.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XPmUyMOF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wcp8tv8o4qbrckcvfdyq.png" alt="carbon"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conditional before calling the function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Pros - Intent is clear, i want to invoke this function only when a certain condition is met.&lt;/li&gt;
&lt;li&gt;Cons - Branches and readability.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qOJRPB2A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9f8hiq08fxaapdzojxo8.png" alt="carbon (2)"&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What are your thoughts on this?
&lt;/h3&gt;

</description>
      <category>discuss</category>
      <category>watercooler</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
