<?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: Bharati Subramanian</title>
    <description>The latest articles on DEV Community by Bharati Subramanian (@bharati21).</description>
    <link>https://dev.to/bharati21</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%2F329895%2Fef570501-6290-42bb-b204-ccbed3633a84.jpg</url>
      <title>DEV Community: Bharati Subramanian</title>
      <link>https://dev.to/bharati21</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bharati21"/>
    <language>en</language>
    <item>
      <title>Deep Diving into JavaScript Variables II</title>
      <dc:creator>Bharati Subramanian</dc:creator>
      <pubDate>Fri, 20 May 2022 07:15:32 +0000</pubDate>
      <link>https://dev.to/bharati21/deep-diving-into-javascript-variables-ii-i26</link>
      <guid>https://dev.to/bharati21/deep-diving-into-javascript-variables-ii-i26</guid>
      <description>&lt;p&gt;In the previous article, I explained about &lt;code&gt;scope&lt;/code&gt; and the behaviour of declarations that are done using &lt;code&gt;var&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;In this post, we will explore &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; declarations, their differences, and how they are better than &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  CONTENTS
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
let Keyword

&lt;ol&gt;
&lt;li&gt;Syntax&lt;/li&gt;
&lt;li&gt;Scope of let&lt;/li&gt;
&lt;li&gt;let Declaration and the Global Object&lt;/li&gt;
&lt;li&gt;let Declaration and Hoisting&lt;/li&gt;
&lt;li&gt;let and Redeclarations&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;
const Keyword

&lt;ol&gt;
&lt;li&gt;const Declaration Syntax&lt;/li&gt;
&lt;li&gt;Scope of const&lt;/li&gt;
&lt;li&gt;const Declaration and the Global Object&lt;/li&gt;
&lt;li&gt;const Declaration and hoisting&lt;/li&gt;
&lt;li&gt;const and Redeclarations&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Keyword let
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If you ever want a placeholder to hold values that changes over time, use &lt;code&gt;let&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;While &lt;code&gt;var&lt;/code&gt; is the black sheep, &lt;code&gt;let&lt;/code&gt; is a beloved amongst the JavaScript family.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; is stricter and more rigid than &lt;code&gt;var&lt;/code&gt; in it's syntax rules and usage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;let variableName [= value];&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables declared with &lt;code&gt;let&lt;/code&gt; have a block scope.&lt;/li&gt;
&lt;li&gt;What this means is that, if a variable using &lt;code&gt;let&lt;/code&gt; is declared within a block, then it is accessible anywhere &lt;em&gt;only&lt;/em&gt; within that block and the sub-blocks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Scope of let
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We'll understand how the scope works using some examples&lt;br&gt;
 &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Oe3AK23r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fawx6d4w8qwj36gnwfn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Oe3AK23r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fawx6d4w8qwj36gnwfn8.png" alt="block scope of let" width="880" height="551"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the above example, the variable declared inside the test()  and the one declared within the if block are 2 different variables. That is their references (memory address) are different.&lt;/li&gt;
&lt;li&gt;The output for the above code is -&lt;/li&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rrf85X2R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8p8hmvd9ruwwl5ndrlz.png" alt="block scope let output" width="800" height="189"&gt;&lt;/li&gt;
&lt;li&gt;Let me show you this through the console.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IyZ1hY5l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ow5ui50t7rysdlmmrcdl.gif" alt="let scope GIF" width="600" height="338"&gt;
&lt;/li&gt;
&lt;li&gt;Hence, any change made to the variable declared within the if block &lt;em&gt;does not reflect&lt;/em&gt; in the variable defined within the function. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;



&lt;h3&gt;
  
  
  let Declaration and the Global Object
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Unlike &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; is not attached to the global object (or &lt;code&gt;globalThis&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This means that in any JavaScript environment, you &lt;strong&gt;&lt;em&gt;cannot&lt;/em&gt;&lt;/strong&gt; access a variable declared using &lt;code&gt;let&lt;/code&gt; using the global object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;e.g., in browsers, you have the global &lt;code&gt;window&lt;/code&gt; and you cannot do the following- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the following example, where &lt;code&gt;x&lt;/code&gt; is declared globally, you cannot use &lt;code&gt;window.x&lt;/code&gt; to get the value of x.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2L5-JMPw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2dnjfuv1fgj0pz9eh6p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2L5-JMPw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2dnjfuv1fgj0pz9eh6p.png" alt="code for let global scoped" width="880" height="676"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YLyiniqF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvso214dzywfvnayf2a2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YLyiniqF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvso214dzywfvnayf2a2.png" alt="let with global scope cannot be accessed via global object" width="880" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is the reason variables declared using &lt;code&gt;let&lt;/code&gt; are preferred over those declared with &lt;code&gt;var&lt;/code&gt;. They provide a layer of protection and ensure that they remain local scoped.

&lt;ul&gt;
&lt;li&gt;If you see the scope tab in the console, you will find that the globally declared x appears under the &lt;code&gt;Script&lt;/code&gt; scope.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kVokfist--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5s2ft227l3cncceyaqc.png" alt="Scope of globally declared let" width="880" height="590"&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  let Declaration and Hoisting
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Variable declarations using let &lt;strong&gt;&lt;em&gt;ARE HOISTED&lt;/em&gt;&lt;/strong&gt;, but the hoisting works quite differently from variables that are hoisted when declared using using &lt;code&gt;var&lt;/code&gt;. 
   &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zE3Sgl8Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0wwex3xoqzhuhcimttc.png" alt="code for let hoisting demo" width="880" height="795"&gt;
&lt;/li&gt;
&lt;li&gt;If you run the above code, you'll get a &lt;code&gt;ReferenceError&lt;/code&gt; that the variable cannot be accessed before initializing.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--shEJ6GK---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lhu69hbbh7u91hubt5r2.png" alt="Reference error when let declaration is accessed before initializing" width="880" height="376"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note the error message could be different depending on the browser.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;ul&gt;
&lt;li&gt;But, we just saw that let declarations are hoisted. And in the case of &lt;code&gt;var&lt;/code&gt;, the hoisted declarations were allocated memory prior to the code execution and also auto initialized to &lt;code&gt;undefined&lt;/code&gt;. What about &lt;code&gt;let&lt;/code&gt; declarations?&lt;/li&gt;
&lt;li&gt;Let us check if memory allocation and auto initialization happens with &lt;code&gt;let&lt;/code&gt; declarations before code execution, on the chrome browser console.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dixlkV5n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9afy38pyqbrd2eads30p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dixlkV5n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9afy38pyqbrd2eads30p.png" alt="let hoisting console" width="880" height="585"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If I add a debugger on the first line in the above code and run, you'll see that in the scope tab, the &lt;code&gt;count&lt;/code&gt; and &lt;code&gt;user&lt;/code&gt; variables are allocated memory before the code execution and auto initialized to undefined. &lt;/li&gt;
&lt;li&gt;Moreover, you will also see a detail that unlike the variable &lt;code&gt;admin&lt;/code&gt;, the &lt;code&gt;count&lt;/code&gt; variable has a scope &lt;code&gt;Script&lt;/code&gt;. 

&lt;ul&gt;
&lt;li&gt;This shows that &lt;code&gt;let&lt;/code&gt; variables are not part of the global object, they are allocated memory in a different scope (In this case &lt;code&gt;Script&lt;/code&gt;). &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;As a rule, &lt;code&gt;let&lt;/code&gt; declarations cannot be accessed before initializing and when you do so, you'll receive a ReferenceError.&lt;/li&gt;
&lt;li&gt;Now, a variable declared using &lt;code&gt;let&lt;/code&gt; is said to be in the &lt;strong&gt;&lt;em&gt;Temporal Dead Zone&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;TDZ&lt;/em&gt;&lt;/strong&gt; from the time it was allocated memory to the time it is initialized.&lt;/li&gt;
&lt;li&gt;The below screenshot shows the TDZ time for the variable &lt;code&gt;count&lt;/code&gt; for the above code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6J5AqO7n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qsgy34xd1eo3ynlm5e8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6J5AqO7n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qsgy34xd1eo3ynlm5e8.png" alt="TDZ for the a variable declared using let" width="880" height="641"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So, when a variable is in it's TDZ, you cannot access it. If you do so, you'll get a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A variable in TDZ be like- *&lt;strong&gt;&lt;em&gt;Can't access&lt;/em&gt;&lt;/strong&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nM6P3av0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://66.media.tumblr.com/3b24f6a0bd141a8cb3da917db251b41f/tumblr_o7uqqgfPJF1ufum4to2_250.gif" alt="In the zone GIF" width="245" height="165"&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  let and Redeclarations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You cannot re-declare a variable using let that was already declared with the same name within the same scope. If you do so, you will receive a SyntaxError. &lt;/li&gt;
&lt;li&gt;Check out the code snippet below.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i2C9yMFp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/re9051qcwwb55pfpb8gh.png" alt="Code snippet Redeclaration using let in the same scope" width="620" height="708"&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check out the output for the above code-&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vpS1bQn5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q554hv5n5zptlfi4t2zh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vpS1bQn5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q554hv5n5zptlfi4t2zh.png" alt="Error for re declaration" width="880" height="204"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You also cannot declare a variable using &lt;code&gt;let&lt;/code&gt; that was previously declared using &lt;code&gt;var&lt;/code&gt; with the same name and within the same scope. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Keyword const
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What if you have a value and you know that it's never going to change and will always remain the same?&lt;/li&gt;
&lt;li&gt;This is where a 3rd type of declaration comes in - constant declarations using keyword &lt;code&gt;const&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;As the name suggests, &lt;code&gt;const&lt;/code&gt; is used to declare constant values that will never change over time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  const Declaration Syntax
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;const variableName = value;&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is &lt;strong&gt;&lt;em&gt;required&lt;/em&gt;&lt;/strong&gt; to initialize a constant with a value during declaration.&lt;/li&gt;
&lt;li&gt;Variables declared with &lt;code&gt;const&lt;/code&gt; also have a block scope.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What this means is that, If a variable is declared using &lt;code&gt;const&lt;/code&gt; within a block, then it is accessible anywhere &lt;em&gt;only&lt;/em&gt; within that block and the sub-blocks.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Then what's the difference between let and const? The difference is that - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You cannot re-assign any values to a constant variable&lt;/li&gt;
&lt;li&gt;Constant variables need to be initialized when they are declared.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The below code snippet shows how you declare a constant.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IyzU9hjE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvjusgtgjjouuvkq3ley.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IyzU9hjE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvjusgtgjjouuvkq3ley.png" alt="constant declaration" width="880" height="283"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Output for the above code -&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6B5mLxHQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myjfmdxa5wmudk47eo9q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6B5mLxHQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myjfmdxa5wmudk47eo9q.png" alt="output for const declaration" width="880" height="194"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Like I mentioned above, you need to initialize a constant variable when you declare. If not you will receive a &lt;code&gt;SyntaxError&lt;/code&gt; as shown in the screenshot for the below code snippet.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---5GvULBP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rotc8yx66yrdkhsjwhnv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---5GvULBP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rotc8yx66yrdkhsjwhnv.png" alt="Code snippet for declaration and then initializing" width="880" height="309"&gt;&lt;/a&gt;&lt;br&gt;
-Output for the code snippet above -&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MYYWzDqL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pe381bsdry1z9audj4l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MYYWzDqL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pe381bsdry1z9audj4l.png" alt="Error when constants are not initialized during declaration" width="880" height="554"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Scope of const
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The scope for &lt;code&gt;const&lt;/code&gt; also works similar to the scope of &lt;code&gt;let&lt;/code&gt;. &lt;br&gt;
 &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---yMlCYV1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a366v9qf7sn2jvo53b4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---yMlCYV1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a366v9qf7sn2jvo53b4g.png" alt="Scope of const" width="880" height="360"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let us see the scope of the variable via console-&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q6zHpaW6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80cfxigsy3w56jrbloxl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q6zHpaW6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80cfxigsy3w56jrbloxl.png" alt="Scope tab of const variable in the console" width="880" height="472"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As you can see, similar to declarations with &lt;code&gt;let&lt;/code&gt;, globally defined constants are also allocated memory in the &lt;code&gt;Script&lt;/code&gt; scope. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let us look at another example. Let us trying a duplicate variable inside if() block and see what happens.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a6-NL5Gr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u8t0bykvvugy8dw54f2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a6-NL5Gr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u8t0bykvvugy8dw54f2.png" alt="Block scope of constant" width="880" height="515"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The output for the above code is -&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vzlBifBe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53pwhcubg6y3zswro5hk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vzlBifBe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53pwhcubg6y3zswro5hk.png" alt="block scope of const output" width="880" height="216"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In the above example, the constant declared outside the if block and the one declared within the if block are 2 different constants. That is their references (memory address) are different.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let me show you the scope of this constant through the console.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GnaJbUX2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kows52uuc0gb2ib55j6g.gif" alt="const scope GIF" width="600" height="338"&gt;
&lt;/li&gt;
&lt;li&gt;Hence, any change made to the constant declared within the if block &lt;em&gt;does not reflect&lt;/em&gt; in the constant defined within the function.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  const Declaration and the Global Object
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Similar to &lt;code&gt;let&lt;/code&gt;, variables declared using &lt;code&gt;const&lt;/code&gt; are not attached to the global object (or &lt;code&gt;globalThis&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This means that in any JavaScript environment, you &lt;strong&gt;&lt;em&gt;cannot&lt;/em&gt;&lt;/strong&gt; access a variable declared using &lt;code&gt;const&lt;/code&gt; using the global object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;e.g., in browsers, you have the global &lt;code&gt;window&lt;/code&gt; and you cannot do the following- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the following example, where &lt;code&gt;x&lt;/code&gt; is declared globally, you cannot use &lt;code&gt;window.x&lt;/code&gt; to get the value of x.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iTW0Gqt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx9qa1kfogbgbugcr9dd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iTW0Gqt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx9qa1kfogbgbugcr9dd.png" alt="code for const globally scoped variable" width="880" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4fRf1bi0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca0jomfvgo0xfmrusj89.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4fRf1bi0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca0jomfvgo0xfmrusj89.png" alt="const variable declared with global scope cannot be accessed via global object" width="839" height="434"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  const Declaration and hoisting
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Variable declarations using const &lt;strong&gt;&lt;em&gt;ARE ALSO HOISTED&lt;/em&gt;&lt;/strong&gt;, and the hoisting works similarly to the variables that are hoisted when declared using using &lt;code&gt;let&lt;/code&gt;. 
   &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f6Mb2zRJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u46larqgv0u1xcdzhynf.png" alt="code for const hoisting demo" width="880" height="615"&gt;
&lt;/li&gt;
&lt;li&gt;If you run the above code, you'll get a &lt;code&gt;ReferenceError&lt;/code&gt; that the variable cannot be accessed before initializing.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--axpR0sJu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6uurkato39p3kmikf745.png" alt="Reference error when const declaration is accessed before initializing" width="827" height="249"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note the error message could be different depending on the browser. &lt;/p&gt;
&lt;/blockquote&gt;



&lt;ul&gt;
&lt;li&gt;Similar to &lt;code&gt;let&lt;/code&gt; declarations, as a rule, &lt;code&gt;const&lt;/code&gt; declarations cannot be accessed before initializing and when you do so, you'll receive a ReferenceError.&lt;/li&gt;
&lt;li&gt;The variable declared using `const is said to be in the &lt;strong&gt;&lt;em&gt;Temporal Dead Zone&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;TDZ&lt;/em&gt;&lt;/strong&gt; from the time it was allocated memory (from the beginning of it's scope) to the time it is initialized.&lt;/li&gt;
&lt;li&gt;The below screenshot shows the TDZ time for the variable &lt;code&gt;USER&lt;/code&gt; for the above code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--boIHx1ta--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vldcl6ekjri941z9c5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--boIHx1ta--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vldcl6ekjri941z9c5v.png" alt="TDZ for the a variable declared using const" width="880" height="692"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  const and Redeclarations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You cannot re-declare a variable using const that was already declared with the same name within the &lt;em&gt;same scope&lt;/em&gt;. If you do so, you will receive a SyntaxError. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check out the code snippet below.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--csSVQ0-z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4cde833vm1h85wzeyqn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--csSVQ0-z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4cde833vm1h85wzeyqn.png" alt="Code snippet Redeclaration using const in the same scope" width="880" height="487"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check out the output for the above code-&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L8_iTaSJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7za8xj92madwyul8vm89.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L8_iTaSJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7za8xj92madwyul8vm89.png" alt="Error for const re declaration" width="880" height="148"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Just like let, you also cannot declare a variable using &lt;code&gt;const&lt;/code&gt; that was previously declared using &lt;code&gt;var&lt;/code&gt; with the same name and &lt;em&gt;within the same scope&lt;/em&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  SUMMARY
&lt;/h2&gt;

&lt;p&gt;That's all for this blog. Phew! Now, I know that this series was a lot, but then this is one of the most important concepts and is often asked in interviews.&lt;/p&gt;

&lt;p&gt;To make things simpler, here's a flow chart citing the differences between &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, and 'var` declarations.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RWsHKZxP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dk3rfrzo72f1g4pkho3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RWsHKZxP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dk3rfrzo72f1g4pkho3.png" alt="Differences Chart" width="880" height="137"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for sticking till the end.&lt;br&gt;
Please do share if this helped you and also I would love to hear your thoughts!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Deep Dive into JavaScript Variables I</title>
      <dc:creator>Bharati Subramanian</dc:creator>
      <pubDate>Thu, 24 Feb 2022 14:01:49 +0000</pubDate>
      <link>https://dev.to/bharati21/declaring-variables-in-javascript-1nfa</link>
      <guid>https://dev.to/bharati21/declaring-variables-in-javascript-1nfa</guid>
      <description>&lt;h2&gt;
  
  
  What is this blog series about?
&lt;/h2&gt;

&lt;p&gt;Through this blog series, I hope to explain variable declarations in JavaScript using &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt;. I intend to cover various rules, properties, and scenarios to explain the 3 types of declarations and hopefully ease understanding of variables in JavaScript!&lt;/p&gt;

&lt;p&gt;This blog would be probably end up becoming a 2 or 3 part series since there are a lot of concepts to cover!&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic JavaScript syntax&lt;/li&gt;
&lt;li&gt;Concept of scope&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;
What are Variables?

&lt;ol&gt;
&lt;li&gt;What is a Scope&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;code&gt;var&lt;/code&gt; Keyword

&lt;ol&gt;
&lt;li&gt;Syntax&lt;/li&gt;
&lt;li&gt;Scopes of var&lt;/li&gt;
&lt;li&gt;var and it's Unhealthy Attachments&lt;/li&gt;
&lt;li&gt;var and its Affair with undefined&lt;/li&gt;
&lt;li&gt;Implicit Global Variables&lt;/li&gt;
&lt;li&gt;Redeclarations using var&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As humans, we see names everywhere - names we give to people, pets, and even inanimate objects. Even machines refer to names within their memory. The only difference is that their names are addresses.&lt;/p&gt;

&lt;p&gt;Even in the world of programming, we have names. And these names are called &lt;code&gt;variables&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;h2&gt;
  
  
  What are Variables?
&lt;/h2&gt;

&lt;p&gt;Variables are placeholders we use to store values. These values could be any of the primitive types or objects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To declare (define) variables in JavaScript, you write the following code-&lt;br&gt;
&lt;code&gt;"keyword" variable_name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;keyword&lt;/code&gt; in JavaScript could be - &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, or &lt;code&gt;let&lt;/code&gt;. &lt;/p&gt;



&lt;p&gt;Before we move on, let us try to understand another term often used when talking about variables - &lt;code&gt;scope&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Scope
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A scope is the context or region where a value is visible.&lt;/li&gt;
&lt;li&gt;When we talk about the scope of variables, it is the region in the program where it can be referenced and used.&lt;/li&gt;
&lt;li&gt;Let's understand scope with an analogy. 

&lt;ul&gt;
&lt;li&gt;John Doe is from India. He's planning on moving to Japan since he got an opportunity to work there (and being a lover of anime was an added advantage!). &lt;/li&gt;
&lt;li&gt;Now, once he moves to Japan, the Indian Rupee is no longer &lt;em&gt;in scope&lt;/em&gt; in Japan (unless of course he exchanges the currency!).&lt;/li&gt;
&lt;li&gt;Also, the Japanese Yen he earns there is not in scope when he visits his home in India.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Similarly, in JavaScript where you declare your variables reflects where and how you can use them. More to this in the post.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;In this article, let us understand about variables declared using'var`.&lt;/p&gt;




&lt;h2&gt;
  
  
  var Keyword
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Variables declared with &lt;code&gt;var&lt;/code&gt; have a global or function scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;var variableName [= value];&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialization while declaring is optional.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Variabled have a global or function scope - what this means is that, if a variable using &lt;code&gt;var&lt;/code&gt; is-&lt;br&gt;
Declared within a function then, it is accessible anywhere within that function (even enclosing blocks such as if statements).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declared Withing the script (globally), then it is accessible in all enclosing functions and blocks of all the script files associated with the current page.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;If you come from languages such as Java, you can visualize these variables as &lt;code&gt;publically defined variables&lt;/code&gt; or public variables.&lt;br&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Scopes of var
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let us look at some examples now-
 &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%2Fgfen2arxm9ucdypwkwbp.png" alt="var global and function scope"&gt; 

&lt;ul&gt;
&lt;li&gt;In the above example, the variable &lt;code&gt;x&lt;/code&gt; has a global scope. Hence the variable is accessible both- outside and inside the function. But variable y has a function scope as it's declared within the function. You cannot access it outside the function.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Let us look at another example that highlights the behavior of variables declared using &lt;code&gt;var&lt;/code&gt; in blocks.&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%2F24h6l9csqhdsq8gixef3.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%2F24h6l9csqhdsq8gixef3.png" alt="Behavior of var in block"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The output for the program above is - &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%2F14pazoukpnexsvpoy1cr.png" alt="Behavior of var in block output"&gt;
&lt;/li&gt;
&lt;li&gt;Even though a different variable &lt;code&gt;var y = 100&lt;/code&gt; was declared within the if block, look how it changes the value of the variable &lt;code&gt;var y = 20&lt;/code&gt; that was declared within the function.&lt;/li&gt;
&lt;li&gt;This is because variables declared with &lt;code&gt;var&lt;/code&gt; have function scope, and any variables declared within a block are scoped to the function or script that has the block.&lt;/li&gt;
&lt;li&gt;So this means that both the variables have the same reference (memory address). &lt;/li&gt;
&lt;li&gt;Let me show you this through the console. 
&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%2Fpyybkjzvrfz0qmrd3dag.gif" alt="var behaviour in block scope in console"&gt; &lt;/li&gt;
&lt;li&gt;Hence, any change made to the variable declared within the if block reflects in the variable defined within the function.&lt;/li&gt;
&lt;li&gt;This also means that redeclaration of the same variable is accepted for variables declared with &lt;code&gt;var&lt;/code&gt; (concept covered later in this article).

&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  var and its unhealthy attachments!
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Another interesting property of a variable declared globally using &lt;code&gt;var&lt;/code&gt; is that it is bound to the global object. &lt;/li&gt;
&lt;li&gt;This means that in any JavaScript environment, you can access the variable using the global object.&lt;/li&gt;
&lt;li&gt;e.g., in browsers, you can use the global &lt;code&gt;window&lt;/code&gt; object to access the variable.&lt;/li&gt;
&lt;li&gt;In the above example, where &lt;code&gt;x&lt;/code&gt; is declared globally, you can use &lt;code&gt;window.x&lt;/code&gt; to get the value of x.&lt;/li&gt;
&lt;/ul&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%2Fr1h53dmnzg6iexnb67wt.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%2Fr1h53dmnzg6iexnb67wt.png" alt="var with global scope can be accessed via global object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In some places you will see this as well - &lt;code&gt;globalThis.x&lt;/code&gt;. This also works and it means the same thing.&lt;/em&gt; &lt;br&gt;
Since there are many JavaScript environments and each of them has different global objects, &lt;code&gt;globalThis&lt;/code&gt; provides unified access to the object.   &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;But note that variables declared globally that are attached to the global object are bad. &lt;/li&gt;
&lt;li&gt;Why? It's simple. Since it's attached to the global object it is visible and accessible globally by any JavaScript code. &lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;So if you have used global variables before... &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fc.tenor.com%2FyGhUqB860GgAAAAC%2Fworriedface.gif" 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%2Fc.tenor.com%2FyGhUqB860GgAAAAC%2Fworriedface.gif" alt="michael scott when you do something wrong gif"&gt;&lt;/a&gt; Now you know to avoid it.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  var and its affair with undefined
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's look at the following code - 
&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%2F9rq7u9p0tgdev4shb6p3.png" alt="var hoisting"&gt;
&lt;/li&gt;
&lt;li&gt;Notice how the variable &lt;code&gt;count&lt;/code&gt; is being accessed even before it's declaration.&lt;/li&gt;
&lt;li&gt;Now, if you came to JavaScript from another language, you'd say- 'Of course, this is an error!`&lt;/li&gt;
&lt;li&gt;But in JavaScript, that is not the case.&lt;/li&gt;
&lt;/ul&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%2Frbv3siy8ruazc9wtq9k0.gif" 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%2Frbv3siy8ruazc9wtq9k0.gif" alt="picking your hair and irritated gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The JavaScript performs something called &lt;code&gt;hoisting&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Hoisting&lt;/em&gt;&lt;/strong&gt; is the process in which all variable, function, and class declararions are moved to the top their scope. &lt;/li&gt;
&lt;li&gt;The declarations are allocated memory much prior to the execution of code. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;This means that you can access them before the actual declaration appears in the script.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So the above code after hoisting is as follows-&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%2F5v537n1pjuzgn7jfhtkd.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%2F5v537n1pjuzgn7jfhtkd.png" alt="var declaration after hoisting"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;That's fine, but the next question would be, what would &lt;br&gt;
&lt;code&gt;console.log(count)&lt;/code&gt; print? &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The assignment to the &lt;code&gt;count&lt;/code&gt; variable happens only after this line.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is where &lt;code&gt;undefined&lt;/code&gt; comes in. JavaScript auto initializes variables declared using &lt;code&gt;var&lt;/code&gt; with &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The value &lt;code&gt;undefined&lt;/code&gt; means that - "Hey, there is memory allocated for this variable. But it's not been initialized yet." &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;The final output would be -
&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%2Fhryvseevwn6p620s84ym.png" alt="output of hoisted variable"&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implicit global variables
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Although not a good practice, it's worth mentioning that a variable that's never declared but initialized with a value is automatically considered as a 'global variable'.&lt;/li&gt;
&lt;li&gt;This would not generate errors and works fine in &lt;code&gt;non-strict mode&lt;/code&gt;. This mode is when the JavaScript interpreter understands features previous to the ES5 version, i.e., the default mode.
&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%2Fz61kg2keriid3u5vngv3.png" alt="implicit global in non strict mode"&gt;
&lt;/li&gt;
&lt;li&gt;But in the strict mode that uses features introduced in ES5+, this would generate errors.
&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%2Fn7vmx7m4bvzz76bxhnsv.png" alt="implicit global generates error in strict mode"&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Redeclarations using var
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If you have come to JavaScript after exploring a few other languages, you know that a variable cannot be declared with the same name twice.&lt;/li&gt;
&lt;li&gt;But JavaScript is special. JavaScript let's you declare a variable with the same name if you do so with the &lt;code&gt;var&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;For e.g., the code below works perfectly fine and no error is thrown.
&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%2F3wkp3z3wysklatrk5xm9.png" alt="Redeclaring a variable with the same name"&gt;
&lt;/li&gt;
&lt;li&gt;If you think about it, this is bad news. &lt;/li&gt;
&lt;li&gt;Say you are working on a project as a team and on your day off, your fellow mate comes and redeclares a variable you've already done so, but, with a new value. &lt;/li&gt;
&lt;li&gt;The interpreter indicates no error, and your team mate is happy that the code does what was expected. &lt;/li&gt;
&lt;li&gt;The next day you come and see that your part of the logic no longer works! And all because somewhere in the code a variable was re-declared. All you'll end up doing is wonder why!
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fc.tenor.com%2F_XOJRgZ2M8QAAAAC%2Fsheldon-cooper.gif" alt="I don't need sleep, I need answers"&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;ul&gt;
&lt;li&gt;That's all for this post! Hope you found this blog helpful and were able to understand &lt;code&gt;var&lt;/code&gt; in a much better sense.&lt;/li&gt;
&lt;li&gt;The key takeaway you should have from this post is that, never trust &lt;code&gt;var&lt;/code&gt;, and avoid using &lt;code&gt;var&lt;/code&gt; in your programs. &lt;/li&gt;
&lt;li&gt;The JavaScript community realized issues with &lt;code&gt;var&lt;/code&gt; and that's why new alternatives - &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; - were introduced in ES6.&lt;/li&gt;
&lt;li&gt;The keywords, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; will be covered in the next part of the blog!&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;scope&lt;/code&gt; is the region of the code where any value is visible or accessing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; declarations when defined within a function are accessible anywhere inside that function. &lt;code&gt;var&lt;/code&gt; declarations that are global can be accessed anywhere within the script.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; declarations are attached to the global object of all JavaScript environments and can be accessed using the &lt;code&gt;globalThis&lt;/code&gt; object or the &lt;code&gt;window&lt;/code&gt; object in browsers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; declarations are hoisted. This means that they are allocated memory even before the code is executed and are auto initialized with &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A variable that is not declared but directly initilized becomes an implicit global variable and is attched to the global object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; declarations can be redeclared. That is, 2 variables that have the same name and belong to the same scopes can be declared using &lt;code&gt;var&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Hope you found this blog post helpful. If yes, help me out by hitting that unicorn/ pin/ heart button, comment down your thoughts, and share it among the dev community.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript loading techniques &amp; Performance</title>
      <dc:creator>Bharati Subramanian</dc:creator>
      <pubDate>Sun, 18 Jul 2021 18:09:50 +0000</pubDate>
      <link>https://dev.to/bharati21/javascript-loading-techniques-performance-56lp</link>
      <guid>https://dev.to/bharati21/javascript-loading-techniques-performance-56lp</guid>
      <description>&lt;p&gt;Adding external script files to your HTML document is simple that you could do it in your sleep.&lt;/p&gt;

&lt;p&gt;But this is not as trivial as you think. Where and how you add your script file majorly influences the performance of your website.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is this blog post about?
&lt;/h2&gt;

&lt;p&gt;In this post, we will go through the techniques to include external script files to your HTML and look at how this can affect the performance.&lt;/p&gt;

&lt;p&gt;We will compare which technique is preferable and efficient over others in varying situations. &lt;/p&gt;



&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;This blog post assumes you are familiar with basic HTML, CSS and JavaScript syntax. &lt;br&gt;
We will also learn about the attributes: &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt;.&lt;/p&gt;



&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;As you might already know, external JavaScript files can be included in the:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;head&lt;/li&gt;
&lt;li&gt;body&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Before we continue and discuss about these techniques in depth, let's understand what happens when a browser loads a webpage. &lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;



&lt;ol&gt;
&lt;li&gt;The browser fetches the requested HTML file, and it is parsed.&lt;/li&gt;
&lt;li&gt;The parsed HTML contains references to external scripts and stylesheets.&lt;/li&gt;
&lt;li&gt;These external references are fetched, and are parsed/ loaded.&lt;/li&gt;
&lt;li&gt;Once loaded, the styles from the sheet are applied to the DOM elements, and &lt;/li&gt;
&lt;li&gt;Then the loaded scripts are executed and applied to the page, and the user views the completed visual structure.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Essentially, this should be the order in which fetching, parsing, loading and execution happens.&lt;/li&gt;
&lt;li&gt;JavaScript files are meant to be applied finally once DOM is complete. But this might vary depending on where you add the script file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now enough with all this! Let's get to the actual post!!&lt;/p&gt;



&lt;h3&gt;
  
  
  Including Script in the body
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This is the most preferred technique since this strategy ensures that the HTML parses before the script file.&lt;/li&gt;
&lt;li&gt;This order becomes necessary when your script manipulates the DOM element.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;JavaScript reference inside body&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
     &lt;span class="c"&gt;&amp;lt;!-- DOCUMENT CONTENT --&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./src/main.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Since I started learning JavaScript, I have always added the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; within the HTML body.&lt;/li&gt;
&lt;li&gt;But I did not know, until recently, that this is an old-fashioned way and surprisingly not the recommended way anymore.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/n5bn2qnHEp3bO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/n5bn2qnHEp3bO/giphy.gif" alt="RDJ Surprised"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adding the script reference in the body may give time for the DOM content to load, but a major problem is that the JavaScript loading is blocked.&lt;/li&gt;
&lt;li&gt;When you have multiple (and huge!) scripts in your website it might turn into a nightmare since users will have to wait for scripts to be loaded AND then executed.&lt;/li&gt;
&lt;li&gt;Not only does this degrade the performance of the website, it also frustrates users. &lt;/li&gt;
&lt;li&gt;Because users hate waiting for websites to load!
&lt;img src="https://i.giphy.com/media/OiC5BKaPVLl60/giphy.gif" alt="Monkey frustrated with computer"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How do we manage to load JavaScript files, and at the same time retain user experience and optimize website performance? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The simple answer to this is: &lt;strong&gt;Add script references inside the head&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/aqUzXfaJ2dElJGQeDw/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/aqUzXfaJ2dElJGQeDw/giphy.gif" alt="Jimmy Fallon "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Including Script in the head
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Yes, you read it right. Add script references within the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;JavaScript reference inside body&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
     &lt;span class="c"&gt;&amp;lt;!-- Add script file source here --&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./src/main.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
     &lt;span class="c"&gt;&amp;lt;!-- DOCUMENT CONTENT --&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;But then it's not that simple. Yet another problem is that when you add the script files to your &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;, the script files are fetched &lt;em&gt;before&lt;/em&gt; the HTML DOM is parsed and loaded completely. &lt;/li&gt;
&lt;li&gt;Below shown image depicts an example webpage that displays a message in &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; when user clicks the button. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;Look what happens when you add the script source in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&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%2Fn9ntjags559akwtj2vtz.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%2Fn9ntjags559akwtj2vtz.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You get an error &lt;strong&gt;"cannot read property addEventListener of null"&lt;/strong&gt;. This happens because the DOM is loaded &lt;em&gt;&lt;em&gt;after&lt;/em&gt;&lt;/em&gt; JavaScript is fetched, and hence there is no reference to the button. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;But this could be avoided as well. How? By doing this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMContentLoaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You clicked me!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&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;ul&gt;
&lt;li&gt;The above code adds an event listener to the body which listens for DOM content to be loaded. &lt;/li&gt;
&lt;li&gt;Once the contents are loaded, all the code within the handler function gets executed thud ensuring that JavaScript is executed only after the DOM is loaded completely. &lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;And now if user clicks the buton, there is no error:&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%2F6za8c61z9ehyvwg7a9b4.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%2F6za8c61z9ehyvwg7a9b4.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
This is yet again an old technique. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;HTML5 provides two new, modern features that prevents blocking of HTML parse and JavaScript load.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The two attributes: &lt;code&gt;async&lt;/code&gt; and (or) &lt;code&gt;defer&lt;/code&gt; are added to the script tag when it is included in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Both the attributes ask the browser to load the script file in a separate thread without blocking the HTML file from being parsed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  1. async
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;JavaScript reference inside body&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
     &lt;span class="c"&gt;&amp;lt;!-- Add script file source here --&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./src/main.js"&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
     &lt;span class="c"&gt;&amp;lt;!-- DOCUMENT CONTENT --&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This attribute ensures that the script file is loaded without affecting the HTML from being parsed.&lt;/li&gt;
&lt;li&gt;That is, the browser loads/ fetches the script file simultaneously while the HTML is being parsed.&lt;/li&gt;
&lt;li&gt;The HTML parse is not paused, and hence loading of script file does not block the DOM from loading.&lt;/li&gt;
&lt;li&gt;But once the script file is loaded completely, the HTML parse is paused and the script is immediately executed, now blocking the DOM from loading.&lt;/li&gt;
&lt;li&gt;When your webpage has multiple scripts, there is no guarantee that the order in which scripts are fetched, loaded and executed is the same order in which the scripts appear in HTML file.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Thus use this attribute when:

&lt;ol&gt;
&lt;li&gt;Script fetching, loading and execution are independent of each other. That is code in one script does not affect code in another.&lt;/li&gt;
&lt;li&gt;When you need scripts to perform initialization tasks that are required before the actual execution begins.
&lt;/li&gt;
&lt;li&gt;When you have scripts that do not manipulate the DOM.
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;   &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
     &lt;span class="c"&gt;&amp;lt;!-- Add script file source here --&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://code.jquery.com/jquery-3.6.0.min.js"&lt;/span&gt; &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js"&lt;/span&gt; &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF"&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./src/init.js"&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;For example: in the above code, there is no guarantee that jQuery will be fetched and executed first, then Bootstrap and then the init script.&lt;/li&gt;
&lt;li&gt;The order could be say: Bootstrap is executed first, then init and finally jQuery script.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. defer
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;JavaScript reference inside body&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
     &lt;span class="c"&gt;&amp;lt;!-- Add script file source here --&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./src/main.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
     &lt;span class="c"&gt;&amp;lt;!-- DOCUMENT CONTENT --&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;defer, as the name suggests, loads the script file in a separate thread, but defers the execution of the script file.&lt;/li&gt;
&lt;li&gt;Unlike &lt;code&gt;async&lt;/code&gt;, script is not executed immediately once the file is loaded, and the DOM load is not blocked. &lt;/li&gt;
&lt;li&gt;This attribute ensures the script is executed only when the DOM is completely loaded. &lt;/li&gt;
&lt;li&gt;The order in which the scripts are fetched, loaded, and executed, is the same order in which they appear in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Thus use this attribute when:

&lt;ol&gt;
&lt;li&gt;The script files in your web page are dependent on each other, and the execution of one script affects the other.&lt;/li&gt;
&lt;li&gt;When your script manipulates the DOM content.
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;   &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
     &lt;span class="c"&gt;&amp;lt;!-- Add script file source here --&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://code.jquery.com/jquery-3.6.0.min.js"&lt;/span&gt; &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js"&lt;/span&gt; &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF"&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./src/main.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The execution of scripts in the above code is in the following order: &lt;strong&gt;jQuery script, Bootstrap, and finally the main script file&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;As a thumb rule, I would suggest to add script sources within the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; only if the script your website uses is minimal.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you have multiple scripts that are heavy, refer to it within the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; as sourcing within the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; blocks JavaScript from loading, thereby affecting the performance of your website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use async in case the scripts in your website are independent of each other, and you want to execute code before the main JavaScript loads.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use defer when you have scripts that rely on parsing of HTML and manipulation of DOM elements. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Here's a visual representation of HTML parsing, and  JavaScript loading and execution from the &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript" rel="noopener noreferrer"&gt;MDN docs&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&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%2Fyhb4xxe0xij69o8syu84.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%2Fyhb4xxe0xij69o8syu84.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Thank you so much for your support and reading this blog post.&lt;br&gt;
Help me out by sharing this to your friends, and comment down what you felt about this post.&lt;/p&gt;

&lt;p&gt;Do heart, save, unicorn, or do all of it if you enjoyed and learned rom this post!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>performance</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Demystifying Position Property</title>
      <dc:creator>Bharati Subramanian</dc:creator>
      <pubDate>Sun, 11 Jul 2021 06:38:20 +0000</pubDate>
      <link>https://dev.to/bharati21/demystifying-position-property-30kd</link>
      <guid>https://dev.to/bharati21/demystifying-position-property-30kd</guid>
      <description>&lt;p&gt;CSS provides the &lt;strong&gt;&lt;code&gt;position&lt;/code&gt;&lt;/strong&gt; property that specifies how an element should appear in the document. This property is helpful when you want to position elements in the DOM outside of normal flow.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is this blog post about?
&lt;/h2&gt;

&lt;p&gt;This blog post covers the &lt;strong&gt;&lt;code&gt;position&lt;/code&gt;&lt;/strong&gt; property with the &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt; properties that determine the final position of an element.&lt;/p&gt;

&lt;p&gt;Since this property has a notorious tendency to be misunderstood, let's try to figure out this property together!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Check out the demos I explained in this post &lt;a href="https://demystifying-css-position.netlify.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prerequisites for the blog
&lt;/h2&gt;

&lt;p&gt;This blog post assumes that you are familiar with the syntax and basics of HMTL and CSS.&lt;/p&gt;



&lt;h2&gt;
  
  
  What is the CSS position property?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;&lt;code&gt;position&lt;/code&gt;&lt;/strong&gt; property specifies &lt;em&gt;how&lt;/em&gt; an element is placed in the DOM.&lt;/li&gt;
&lt;li&gt;Along with this, you can use &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt; properties that specify &lt;em&gt;where&lt;/em&gt; the element is placed.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The &lt;strong&gt;&lt;code&gt;position&lt;/code&gt;&lt;/strong&gt; property can take &lt;em&gt;five&lt;/em&gt; values:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;static&lt;/li&gt;
&lt;li&gt;absolute&lt;/li&gt;
&lt;li&gt;relative&lt;/li&gt;
&lt;li&gt;fixed&lt;/li&gt;
&lt;li&gt;sticky&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Before we jump in, let's understand in short what &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt; are.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt;, properties specify how far away from the top/ right/ bottom/ left and element should be positioned. 

&lt;ul&gt;
&lt;li&gt;By default all these properties have value &lt;em&gt;auto&lt;/em&gt;, and the element is placed in a Static Position.&lt;/li&gt;
&lt;li&gt;It accepts both negative and positive integer values and common units such as &lt;em&gt;px&lt;/em&gt;, &lt;em&gt;rem&lt;/em&gt;, &lt;em&gt;em&lt;/em&gt;, &lt;em&gt;%&lt;/em&gt; etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt; is used to specify the stack level, and layer elements. 

&lt;ul&gt;
&lt;li&gt;The default value for &lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt; is 0, and will not work unless you apply position on the element (except static).&lt;/li&gt;
&lt;li&gt;Elements with a larger &lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt; value overlaps elements with smaller &lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt; value.&lt;/li&gt;
&lt;li&gt;It accepts positive and negative integers without any unit.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


 
&lt;h3&gt;
  
  
  1. Static
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;&lt;code&gt;position: static;&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; is the default value of this property, and the elements appear in their normal flow in the document.&lt;/li&gt;
&lt;li&gt;Properties: &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt; have no effect on an element positioned &lt;strong&gt;&lt;code&gt;static&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Check the example below. The blue colored &lt;code&gt;div&lt;/code&gt; with class &lt;code&gt;static&lt;/code&gt; has a static position.
&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%2Fg0p8fin4xqk78g6hi0s9.png" alt="Element with static position"&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The code for the above example:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.static&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;static&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* Need not specify as this is default */&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Check out the codepen here: &lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/MWmjOJa?height=600&amp;amp;default-tab=html,css&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Relative
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When &lt;strong&gt;&lt;em&gt;&lt;code&gt;position: relative;&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; is applied to an element, it appears in the normal flow of the document.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;But unlike &lt;strong&gt;&lt;em&gt;static position&lt;/em&gt;&lt;/strong&gt;, it accepts accepts values for &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The element is then placed relative to &lt;em&gt;itself&lt;/em&gt; depending on the values of &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt;, and stacked in layers depending on the value of &lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;In the example shown below, the blue colored &lt;code&gt;div&lt;/code&gt; element with class &lt;code&gt;relative&lt;/code&gt; has a relative position, with top and left offset values.&lt;br&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%2Fyqg1m3ujunhi7s1poehl.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%2Fyqg1m3ujunhi7s1poehl.png" alt="Element with relative position"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The code for the above example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.relative&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25%&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;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check out the codepen here: &lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/mdmrwJK?height=600&amp;amp;default-tab=html,css&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Absolute
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When &lt;strong&gt;&lt;em&gt;&lt;code&gt;position: absolute;&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; is applied to an element, it is taken &lt;em&gt;out&lt;/em&gt; of the the normal flow of the document.&lt;/li&gt;
&lt;li&gt;It remains in it's original position unless &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt; values are specified.&lt;/li&gt;
&lt;li&gt;The other elements ignore the element, and behave as if it is not present at all. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;z-index&lt;/code&gt;&lt;/strong&gt; can also be specified to stack and layer elements.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the example below, the blue colored &lt;code&gt;div&lt;/code&gt; with class &lt;code&gt;absolute&lt;/code&gt; is given a relative position, and no values of &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt; are specified.&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%2Fh3no7oyzye5fdczfvsg1.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%2Fh3no7oyzye5fdczfvsg1.png" alt="Element with absolute position"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;See what happens when this blue box is given &lt;strong&gt;&lt;code&gt;z-index: -1;&lt;/code&gt;&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%2Fv7dpra2xwqfkodw2gb3o.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%2Fv7dpra2xwqfkodw2gb3o.png" alt="absolute positioned element with z-index -1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The parent of this &lt;code&gt;div.absolute&lt;/code&gt; is the section with class &lt;code&gt;section&lt;/code&gt; and &lt;code&gt;background-color: lightskyblue;&lt;/code&gt; has &lt;code&gt;z-index: 0;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;strong&gt;`z-index&lt;/strong&gt;&lt;code&gt; of &lt;/code&gt;section.section&lt;code&gt; is greater than that of &lt;/code&gt;div.absolute&lt;code&gt;, the &lt;/code&gt;div` is stacked below it's parent.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Typically an element with &lt;strong&gt;&lt;code&gt;position: absolute;&lt;/code&gt;&lt;/strong&gt; is placed relative to it's closest positioned ancestor (other than static).&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If no such ancestor is found, it is simply placed relative to the document.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check out the example below, and notice how the element is placed relative to the document.
&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%2Fqs4cf9en28zdafhzj569.png" alt="Element with absolute position relative to document"&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;The code for the above example:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.absolute-two&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2em&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;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Check out what happens, if we give the parent (or an ancestor) of an element with absolute position: &lt;code&gt;position: relative;&lt;/code&gt;.&lt;br&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%2Fpqjfrb093smnra71dfzr.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%2Fpqjfrb093smnra71dfzr.png" alt="Element with position absolute within an ancestor positioned absolute"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check the red colored &lt;code&gt;div&lt;/code&gt; with a class &lt;code&gt;absolute-three&lt;/code&gt;, and it's lightskyblue colored parent &lt;code&gt;section&lt;/code&gt; with class &lt;code&gt;section&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Note how this time the absolute &lt;code&gt;div.absolute-three&lt;/code&gt; is positioned relatively within the parent and not the document.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;The code for the above example is:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="nc"&gt;.section&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.absolute-three&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&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;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Check out the final codepen here: &lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/KKmgqON?height=600&amp;amp;default-tab=html,css&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Fixed
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When &lt;strong&gt;&lt;em&gt;&lt;code&gt;position: fixed;&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; is applied to an element, it is taken &lt;em&gt;out of the the normal flow of the document&lt;/em&gt; similar to Absolute Position.

&lt;ul&gt;
&lt;li&gt;The main difference is that the &lt;strong&gt;&lt;em&gt;&lt;code&gt;position: fixed;&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; element is not given space in the layout.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The fixed element is not affected by scrolling, and remains in the specified position depending on the &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt; values.&lt;/li&gt;

&lt;li&gt;The positioned element is relative to the document except when it's ancestors have &lt;strong&gt;&lt;code&gt;transform&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;perspective&lt;/code&gt;&lt;/strong&gt;, or &lt;strong&gt;&lt;code&gt;filter&lt;/code&gt;&lt;/strong&gt; property applied with a value other than &lt;strong&gt;&lt;em&gt;none&lt;/em&gt;&lt;/strong&gt;. 

&lt;ul&gt;
&lt;li&gt;Then, the ancestor behaves as the relative parent.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Check out the example below. The text &lt;code&gt;div&lt;/code&gt; with blue background has a class &lt;code&gt;fixed&lt;/code&gt; and &lt;strong&gt;&lt;code&gt;position: fixed&lt;/code&gt;&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%2Fop5t9su8dwx0wn0d9e6z.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%2Fop5t9su8dwx0wn0d9e6z.png" alt="Element with fixed position"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The code for the above example is:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.fixed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;fixed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;40vh&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25vw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25vw&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;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check out the codepen here&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/KKmgXpe?height=600&amp;amp;default-tab=html,css&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Sticky
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When &lt;strong&gt;&lt;em&gt;&lt;code&gt;position: sticky;&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; is applied to an element, it is positioned in the the normal flow of the document similar to &lt;a href="https://dev.to!#2-relative"&gt;Relative Position&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;But as the element is scrolled within the positioned ancestor, it becomes fixed once the offset value specified in &lt;strong&gt;&lt;code&gt;top&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;right&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;bottom&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;left&lt;/code&gt;&lt;/strong&gt; properties are crossed.&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;For e.g. look at the image below of a header.&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%2F3g2zdyizf0azdtxfgs15.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%2F3g2zdyizf0azdtxfgs15.png" alt="Header with sticky positione"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The header's parent is the document, and it's &lt;strong&gt;&lt;em&gt;"sticky"&lt;/em&gt;&lt;/strong&gt; within it. As you scroll and the header is &lt;em&gt;0 away from top&lt;/em&gt;, i.e. &lt;code&gt;top: 0;&lt;/code&gt;, it becomes a fixed positioned element.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;The code for the above example is:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nt"&gt;header&lt;/span&gt;&lt;span class="nc"&gt;.hero&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sticky&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&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;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Let's deal with another scenario. But this time let's use an element that is positioned &lt;strong&gt;sticky&lt;/strong&gt; within another positioned ancestor.
&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%2Fm5erk9eb3fsumbe9fzse.png" alt="Element with sticky position within a relative positioned ancestor"&gt;

&lt;ul&gt;
&lt;li&gt;In this scenario, the sticky positioned element (text with blue colored background) has a class of &lt;code&gt;sticky&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;It's parent is the &lt;code&gt;section&lt;/code&gt; with class &lt;code&gt;section&lt;/code&gt;, and has a &lt;code&gt;lightskyblue background&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;As you scroll down and &lt;code&gt;div.sticky&lt;/code&gt; is &lt;code&gt;5px&lt;/code&gt; away from the top, it becomes a fixed element. Before that it remains as relatively positioned element.&lt;/li&gt;
&lt;li&gt;But there is a catch here. As the parent &lt;code&gt;section.section&lt;/code&gt; scrolls away from the layout, so does the div with &lt;strong&gt;sticky&lt;/strong&gt; position of. It no longer remains fixed, and now becomes relative again.
&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%2F9iecnxamfkq9lethmxnk.png" alt="Element with position sticky within relative positioned ancestor"&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hence the sticky (in particular fixed) behavior of an element remains only within it's closest positioned parent.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The code for the above example is:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nt"&gt;section&lt;/span&gt;&lt;span class="nc"&gt;.section&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.sticky&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sticky&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&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;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check the entire codepen here:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/BaRLmaG?height=600&amp;amp;default-tab=html,css&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;



&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;Phew! That was a lot. Wasn't it?&lt;/p&gt;

&lt;p&gt;I understand that sometimes CSS can be overwhelming but trust me, as you practice the concepts become clearer.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;position&lt;/code&gt;&lt;/strong&gt; property is very important, and comes in handy in various scenarios.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Check out the demos I explained in this post &lt;a href="https://demystifying-css-position.netlify.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;relative&lt;/code&gt; and &lt;code&gt;absolute&lt;/code&gt; positions are often used, and mostly together when you have pseudo elements. 

&lt;ul&gt;
&lt;li&gt;These positions are also often used to stack items. &lt;/li&gt;
&lt;li&gt;The combination of &lt;code&gt;absolute&lt;/code&gt; position within &lt;code&gt;relative&lt;/code&gt; position is common, and is used to add styles such as shadows to elements.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The &lt;code&gt;sticky&lt;/code&gt; position can be used for headers and navbar as shown in the example for Sticky above.

&lt;ul&gt;
&lt;li&gt;It could also be used in cases when you want to keep an element fixed on the page as long as the positioned parent/ ancestor is visible on the viewport.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;These are not the only available use cases of &lt;code&gt;position&lt;/code&gt; property. I highly recommend you to practice and get your hands dirty to understand these concepts in depth.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Thank you so much for reading this post! &lt;/p&gt;

&lt;p&gt;This was my final blog post on CSS. In the coming posts, I will focus on JavaScript and try to explain few vital concepts.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;If you liked this post then please help me out giving it a heart, unicorn or a pin!&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Do share it among your dev friends, and comment down what you feel!&lt;/p&gt;





&lt;h2&gt;
  
  
  RESOURCES:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://youtu.be/P6UgYq3J3Qs" rel="noopener noreferrer"&gt;Absolute and Relative Position Explained by Kevin Powell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/lUaw-AA9HnA" rel="noopener noreferrer"&gt;Practical Examples of Absolute Position by Kevin Powell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://youtu.be/8TyoihVGErI" rel="noopener noreferrer"&gt;Things you can do with Position Sticky by Kevin Powell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/position" rel="noopener noreferrer"&gt;CSS Position MDN docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>beginners</category>
      <category>html</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Responsive Fonts in CSS</title>
      <dc:creator>Bharati Subramanian</dc:creator>
      <pubDate>Tue, 29 Jun 2021 14:34:31 +0000</pubDate>
      <link>https://dev.to/bharati21/responsive-fonts-in-css-m44</link>
      <guid>https://dev.to/bharati21/responsive-fonts-in-css-m44</guid>
      <description>&lt;p&gt;CSS provides a wide range of units to choose from while developing web applications. &lt;br&gt;
Many properties such as padding, margin, font-size, width, height, etc., take &lt;em&gt;&lt;strong&gt;length&lt;/strong&gt;&lt;/em&gt; as values followed by a unit.&lt;/p&gt;

&lt;p&gt;A good developer should be able to decide how and when to use these units.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is this blog post about?
&lt;/h2&gt;

&lt;p&gt;We will discuss some of the common units in CSS that are used with fonts, and also understand how to make font-sizes responsive.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Find the demo site for the examples explained in this post &lt;a href="https://common-css-font-units.netlify.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Prerequisites for this blog
&lt;/h2&gt;

&lt;p&gt;This post assumes that you are familiar with the basics of HTML and CSS.&lt;/p&gt;



&lt;h2&gt;
  
  
  Font Units in CSS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Generally, &lt;strong&gt;px&lt;/strong&gt;, &lt;strong&gt;rem&lt;/strong&gt;, &lt;strong&gt;em&lt;/strong&gt;, and &lt;strong&gt;%&lt;/strong&gt; are the most sought out units in CSS.&lt;/li&gt;
&lt;li&gt;Although there are many other units available in CSS, the above-mentioned are preferred for &lt;strong&gt;&lt;em&gt;&lt;code&gt;font-size&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Let's discuss these four units, and try to understand how and when they can be used.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Pixel
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;px&lt;/strong&gt; is an absolute unit. That is, it defines fixed values that do not scale when screen/ device width changes.&lt;/li&gt;
&lt;li&gt;These are commonly used as it is easy to understand, and effortless to use.&lt;/li&gt;
&lt;li&gt;It corresponds to 1/96&lt;sup&gt;th&lt;/sup&gt; of an inch, and usually, the default size of the text in a browser is &lt;em&gt;16 pixels&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Let's use the image below to understand &lt;strong&gt;px&lt;/strong&gt; better.&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%2Fm7gl9w6b5ts1nj9jyijw.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%2Fm7gl9w6b5ts1nj9jyijw.png" alt="Paragraph with font-size in pixels"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.pixel&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&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;ul&gt;
&lt;li&gt;The &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; has a &lt;code&gt;font-size: 20px&lt;/code&gt;. Now look at this image below that shows the same paragraph in a device with smaller screen size.
&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%2F8bdslz4x8hbsff6cve4c.png" alt="Paragraph with font-size in pixels in smaller viewport"&gt;
&lt;/li&gt;
&lt;li&gt;Notice how the &lt;code&gt;font-size&lt;/code&gt; remains &lt;code&gt;20px&lt;/code&gt; even when the viewport size decreases. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;This is the issue with using pixels for any of the properties that takes a length. It is non-responsive, and does not scale according to the viewport size.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check out the pen for the above code here: &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/KKmPGqR?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
 &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Rem
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;rem&lt;/strong&gt; in a relative unit. That is it scales as the viewport size changes.&lt;/li&gt;
&lt;li&gt;This unit is highly flexible and is recommended by developers.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;r&lt;/code&gt; in rem stands for &lt;strong&gt;root&lt;/strong&gt;. 

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;1 rem unit is the font-size of the root element, i.e. &lt;strong&gt;&lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Let's understand &lt;strong&gt;rem&lt;/strong&gt; with an example. Consider the code below.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.rem&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* 2 * 16 = 32px */&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;ul&gt;
&lt;li&gt;Check out the pen for the above example here: &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/eYWYBXX?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;If you do not override the default font-size of the root element, i.e. &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;, it will have a font-size of &lt;code&gt;16px&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;The font-size of &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;, in the above example, would be 2 times the size of the root element. That is: 2 * 16 = &lt;strong&gt;&lt;code&gt;32px&lt;/code&gt;&lt;/strong&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%2Fv43e41kj74m679qj5ayw.png" alt="Computed font-size of paragraph with rem unit"&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Another example could be overriding the default &lt;code&gt;16px&lt;/code&gt; of the root element.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.rem&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c"&gt;/* 0.5 * 20 = 10px */&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;ul&gt;
&lt;li&gt;Check out the pen for the above example here: &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/oNWNOOQ?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
 &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;As stated above &lt;strong&gt;rem&lt;/strong&gt; is highly responsive and is UX friendly.&lt;/li&gt;
&lt;li&gt;To check for responsiveness, try this out.

&lt;ol&gt;
&lt;li&gt;Go to your &lt;code&gt;browser settings&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Under &lt;code&gt;appearance&lt;/code&gt;, select &lt;code&gt;Font size&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Try changing the &lt;code&gt;Font size&lt;/code&gt; of your browser, and look at the computed result for the tag who's unit length is defined using &lt;code&gt;rem&lt;/code&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%2F2ss2sjo4kvostvq6j2za.gif" alt="GIF demonstrating how to change font size of browser"&gt;
&lt;/li&gt;
&lt;li&gt;You will see that the font-size becomes responsive, and changes according to the chosen font size of the browser.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE&lt;/em&gt;&lt;/strong&gt;: If you override the default font-size of root element, changing the font-size of the browser will have no effect on the font-size of the content defined. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Font-size defined using &lt;strong&gt;rem&lt;/strong&gt; will be relative to the manual value that overrides the default root font-size.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;



&lt;h3&gt;
  
  
  Em
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;em&lt;/strong&gt; is very similar to &lt;strong&gt;rem&lt;/strong&gt;, but the difference is that &lt;strong&gt;em&lt;/strong&gt; is &lt;em&gt;relative to it's parent container&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Let's use the same paragraph example and follow the code below.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.em&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.em&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c"&gt;/* 2 * 15 = 30px */&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;ul&gt;
&lt;li&gt;Check out the pen for the above example here: &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/mdmdgQY?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Since &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; is the parent of the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;, the font-size of the content within &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; would be twice that of font-size defined for &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;.` 

&lt;ul&gt;
&lt;li&gt;That is, 2 * 15 = &lt;code&gt;30px would be the font-size of &lt;/code&gt;&lt;p&gt;`.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The image below shows the results of the above code
&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%2F3d37745hytpxmgzcinrv.png" alt="Paragraph with font-size defined using em"&gt;
&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Now, consider the scenario with code below:&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This is the HTML code&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"em"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
       &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;1. Lorem&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
       &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;1.1. Lorem&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
               &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;1.1.1. Lorem&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
           &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
       &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This is the CSS code&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.em&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2rem&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;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Guess what would happen to the content within &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt;. What would be the font-size?&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%2Fcbx7jpwfggst1s056bba.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%2Fcbx7jpwfggst1s056bba.png" alt="Nested li font size when defined using em"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Unexpected right? &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This is when &lt;strong&gt;em&lt;/strong&gt; gets tricky and out of hand. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;When you have multiple nested elements, you need to be careful with the lengths you assign.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Check out the pen for the above &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; example here: &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/qBmBRNN?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
 &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;To check for responsiveness, you can try changing the font-size of the browser (as shown above for &lt;strong&gt;rem&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE&lt;/em&gt;&lt;/strong&gt;: If you override the default font-size of root element, changing the font-size of the browser will have no effect on the font-size of the content defined. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Font-size defined using &lt;strong&gt;em&lt;/strong&gt; will be relative to the parent of that element. &lt;em&gt;If the parent is the root, then the font-size of the element will be relative to the overridden font-size of root&lt;/em&gt;. &lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Another important thing to note if you use &lt;strong&gt;em&lt;/strong&gt; is that, when you use it for properties like width or padding, the length is derived from the font-size of the element itself.

&lt;ul&gt;
&lt;li&gt;For e.g. say you have a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; element within a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, and the font-size of &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; is 12px, &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; is &lt;code&gt;2em&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;And you declare padding for &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; as 2em. Then, the padding of&lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; would be &lt;code&gt;48px&lt;/code&gt; and NOT &lt;code&gt;24px&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;This is because the other properties multiply the value of font-size of the element itself&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;So keep this in mind when you use &lt;strong&gt;em&lt;/strong&gt; as the unit for font-sizes.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Percentage
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;%&lt;/strong&gt; is a relative unit, and is &lt;em&gt;relative to the font-size of the parent element&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using a similar scenario like we did for &lt;strong&gt;em&lt;/strong&gt;, let's assume we have a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; within a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.percentage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;150%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nc"&gt;.percentage&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200%&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;ul&gt;
&lt;li&gt;Find the pen for the above code here: &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/QWvWRGv?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Let's see what the font-size of &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; will be.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The font-size for &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; depends on &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, and the font-size of the &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; depends on it's parent. (In this case: &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Since the font-size for &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; is 20px, the font-size of &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; should be 1.5 * 20 = &lt;code&gt;30px&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;And the font-size of &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; would be 30 * 2 = &lt;code&gt;60px&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Look at the image below to see the results of the above code: &lt;br&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%2Fchh102kbhk42ahzdcrsj.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%2Fchh102kbhk42ahzdcrsj.png" alt="Paragraph defined using p"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;To check for responsiveness, you can try changing the font-size of the browser (as shown above for &lt;strong&gt;rem&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;NOTE&lt;/em&gt;&lt;/strong&gt;: If you override the default font-size of root element, changing the font-size of the browser will have no effect on the font-size of the content defined. 

&lt;ul&gt;
&lt;li&gt;Font-size defined using &lt;strong&gt;%&lt;/strong&gt; will be relative to the parent of that element. &lt;em&gt;If the parent is the root, then the font-size of the element will be relative to the overridden font-size of root&lt;/em&gt;. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;We have seen four different units that can be used with fonts in CSS. &lt;br&gt;
As stated previously, there are many more units to explore and use. But the above explained ones are most commonly used by developers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Find the demo site for the examples explained in this post &lt;a href="https://common-css-font-units.netlify.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/strong&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;A thumb of rule that you can follow is use &lt;strong&gt;rem&lt;/strong&gt; everywhere for fonts. As it inherits from the root element it's easy to use, manage and compute the size of the element.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Moreover, it does not get exponentially multiplied (unlike &lt;strong&gt;em&lt;/strong&gt;) when you define font-size for nested elements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;em can be used when you want the child element to inherit font-size and behave like the parent.&lt;/em&gt;&lt;/strong&gt; Just beware of the inheritance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;As for &lt;strong&gt;%&lt;/strong&gt;, it is very similar to &lt;strong&gt;em&lt;/strong&gt;. But the main difference is that &lt;strong&gt;em&lt;/strong&gt; scales faster than &lt;strong&gt;%&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%2Fkyleschaeffer.com%2Fassets%2Fimg%2Ffont-size-2.gif" 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%2Fkyleschaeffer.com%2Fassets%2Fimg%2Ffont-size-2.gif" alt="em and percentage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There will hardly be any difference between &lt;strong&gt;em&lt;/strong&gt; and &lt;strong&gt;%&lt;/strong&gt; when users set the browser text size to “medium”.&lt;/li&gt;
&lt;li&gt;But as you can see in the picture above, when browser text-size is set to &lt;strong&gt;smallest&lt;/strong&gt;, then &lt;strong&gt;em&lt;/strong&gt; is hardly legible when compared to &lt;strong&gt;%&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;And when browser size is set to &lt;strong&gt;largest&lt;/strong&gt;, &lt;strong&gt;em&lt;/strong&gt; is displayed much larger than &lt;strong&gt;%&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Most people use &lt;strong&gt;px&lt;/strong&gt; as well for font-size. But I do not recommend using it at all as it's not user friendly and not responsive. &lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;In general I would suggest,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;rem &amp;gt; percentage &amp;gt; em &amp;gt; px&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/css/css_units.asp" rel="noopener noreferrer"&gt;W3Schools CSS Units&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.udemy.com/course/modern-html-css-from-the-beginning/" rel="noopener noreferrer"&gt;Udemy Modern HTML and CSS From the Beginning Course&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Values_and_units" rel="noopener noreferrer"&gt;MDN Web Docs CSS values and units
&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/font-size" rel="noopener noreferrer"&gt;MDN Web Docs Font Sizes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kyleschaeffer.com/css-font-size-em-vs-px-vs-pt-vs-percent" rel="noopener noreferrer"&gt;Blog on CSS Font-Size by Kyle Schaeffer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thank you for reading this blog post. &lt;/p&gt;

&lt;p&gt;I hope you were able to understand the common units for font-sizes in CSS, and I am sure this would be very helpful while developing responsive websites in the future!&lt;/p&gt;

&lt;p&gt;Do give this post a like, pin or save it, and share it with your dev friends!&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>design</category>
      <category>html</category>
    </item>
    <item>
      <title>Introduction to Responsive Design</title>
      <dc:creator>Bharati Subramanian</dc:creator>
      <pubDate>Wed, 16 Jun 2021 17:34:17 +0000</pubDate>
      <link>https://dev.to/bharati21/introduction-to-responsive-design-35hc</link>
      <guid>https://dev.to/bharati21/introduction-to-responsive-design-35hc</guid>
      <description>&lt;p&gt;A good website should be readable and appealing when used across all devices of any size.&lt;br&gt;
Such a website must fit and scale automatically to screens of different sizes.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is this blog about?
&lt;/h2&gt;

&lt;p&gt;In this blog we will be focusing on &lt;em&gt;some&lt;/em&gt; of the techniques used to make a website responsive and create best user experiences.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites for this blog
&lt;/h2&gt;

&lt;p&gt;This blog assumes that you are familiar with the basics of HTML and CSS.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is &lt;strong&gt;&lt;em&gt;Responsive Design&lt;/em&gt;&lt;/strong&gt;?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Responsive web design is the concept of using HTML and CSS to build layouts that adapt to all screen sizes, resolutions, and devices.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h2&gt;
  
  
  Responsive Design Techniques
&lt;/h2&gt;

&lt;p&gt;Three techniques are discussed in this blog that are used to make websites and pages more responsive:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting Viewport&lt;/li&gt;
&lt;li&gt;Fluid widths and layouts&lt;/li&gt;
&lt;li&gt;Media Queries&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check out the website and code shown in the blog &lt;a href="https://responsive-design-demo.netlify.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Setting the &lt;strong&gt;&lt;em&gt;viewport&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What is &lt;strong&gt;Viewport&lt;/strong&gt;?

&lt;ul&gt;
&lt;li&gt;A viewport is the area of the screen that is visible to the user. &lt;/li&gt;
&lt;li&gt;This area is smaller in devices such as mobile phones or tablets when compared to a PC.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Why should you &lt;strong&gt;set the viewport&lt;/strong&gt;?

&lt;ul&gt;
&lt;li&gt;The viewport gives instructions to the browser on how to set the width of the screen such that it scales to the width of the device&lt;/li&gt;
&lt;li&gt;It also informs to zoom the content such that it's intended for the device at hand.&lt;/li&gt;
&lt;li&gt;This ensures that the content does not appear zoomed out and does not appear as it does on a desktop layout.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;How do you &lt;strong&gt;set the viewport&lt;/strong&gt;?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The viewport is set inside the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; using the &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt; tag.
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;What does the above code mean?

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;width=device-width&lt;/code&gt;: sets the width of the screen to the width of the device.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;initial-scale=1.0&lt;/code&gt;: sets the initial zoom/ scale to 1&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Look at the images below, and see the difference in screen width (in mobile browsers) and content size&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Without  viewport tag
&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%2Fpvq9urk5hiasyaogwot4.png" alt="Mobile screen browser width without viewport"&gt;

&lt;ul&gt;
&lt;li&gt;Notice how the content is zoomed out and the image does not adjust to the screen size and width of the device.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;With  viewport tag
&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%2Fi6i4rymtyolgbm0rmgwh.png" alt="Mobile screen browser width with viewport"&gt;

&lt;ul&gt;
&lt;li&gt;Although the image does scale to the size of the screen, the image is not responsive. To handle this, we use Fluid Widths.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  2. &lt;strong&gt;&lt;em&gt;Fluid Widths&lt;/em&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What is &lt;strong&gt;fluid width&lt;/strong&gt;?

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fluid-widths&lt;/strong&gt; are relative measurements, that are responsive to change in screen size and viewports.&lt;/li&gt;
&lt;li&gt;This is in contrast to &lt;strong&gt;fixed-layouts&lt;/strong&gt;, which are static measurements and are non-responsive.&lt;/li&gt;
&lt;li&gt;Fluid widths use the entire width of the browser, and such a layout is called &lt;strong&gt;&lt;em&gt;liquid layout&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;fluid layout&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Why should we &lt;strong&gt;use fluid width&lt;/strong&gt;? 

&lt;ul&gt;
&lt;li&gt;Take the example of a sample page in the image below. 
&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%2F7u5qt0rwsu9k2dd69r8e.png" alt="Sample website with image and scrollbar"&gt;
&lt;/li&gt;
&lt;li&gt;Notice how when the size of the browser shrinks, a horizontal tab appears to view the entire image. 
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ftenor.com%2Fview%2Fwell-that-wont-do-at-all-not-good-enough-not-okay-not-helpful-lucifer-morningstar-gif-16834111.gif" alt="That won't do at all"&gt;
&lt;/li&gt;
&lt;li&gt;This is definitely not looking good, and does not provide a good user experience. &lt;/li&gt;
&lt;li&gt;Since the image is not responsive, it does not shrink in size when the browser width (or viewport width) reduces. This is where fluid width comes into play.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;How can we use &lt;strong&gt;fluid-widths&lt;/strong&gt;?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Instead of using pixels (px), use percentages (%) , viewport height (vh) and viewport width (vw).&lt;/li&gt;
&lt;li&gt;Image in the example website above uses 500px as it's width. &lt;/li&gt;
&lt;li&gt;To make this a fluid image, we can use percentages or vw to set a width.
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;80%&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="m"&gt;50vw&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;ul&gt;
&lt;li&gt;Percentages occupy space relative to width or height of the parent element. Whereas vw / vh take up space relative to width or height of the viewport.&lt;/li&gt;
&lt;li&gt;Now, as the size of viewport decreases, the image also shrinks relatively. &lt;/li&gt;
&lt;li&gt;There comes a point when the image shrinks and ends up becoming very small.&lt;/li&gt;
&lt;li&gt;To tackle this, we can set a minimum height or width, below which, the image should not be shrunk.
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&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;ul&gt;
&lt;li&gt;Now the image will not shrink below &lt;code&gt;300px&lt;/code&gt; even if the viewport size becomes smaller than this. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fluid widths and layouts are incredibly useful when you have to handle multimedia content in your site. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Another simple use of fluid width is when you set a max-width for a container in the body and center the content within it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;max-width ensures that the content will not overflow beyond this width, and will be shrunk if it becomes greater than the specified width.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  3. Media Queries
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What are &lt;strong&gt;media queries&lt;/strong&gt;?

&lt;ul&gt;
&lt;li&gt;Media queries are series of CSS styles applied to elements in the page for a particular viewport or screen size.&lt;/li&gt;
&lt;li&gt;Multiple media queries can be added to the same style sheet each focusing on viewport or device of a particular size.&lt;/li&gt;
&lt;li&gt;The size of screen at which a media query is added are called &lt;strong&gt;&lt;em&gt;breakpoints&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You can create web pages initially for mobile devices with minimal content, and then extend the same page to devices of larger size. 

&lt;ul&gt;
&lt;li&gt;This is called &lt;strong&gt;&lt;em&gt;mobile-first design&lt;/em&gt;&lt;/strong&gt; and is highly common among developers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Why do we need &lt;strong&gt;media-queries&lt;/strong&gt;?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's take the sample webpage in the image below, and try to introduce media queries.
&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%2Fqyi7bdhq1n3165o63zou.png" alt="Sample webpage with an image and text displayed side by side"&gt;
&lt;/li&gt;
&lt;li&gt;Obviously there is nothing wrong with the image or text in the image above. &lt;/li&gt;
&lt;li&gt;But when you view this page in a smaller device (a tablet or mobile), the image and text shrink to become so small that it looks unappealing. 
&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%2F43lwgyz9l1w4qdpbbp3q.png" alt="Sample webpage with an image and text displayed side by side in a mobile device"&gt;
&lt;/li&gt;
&lt;/ul&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%2Fmedia1.tenor.com%2Fimages%2Ffd959fd0c2d6d9b2cc9fc049f2296489%2Ftenor.gif%3Fitemid%3D18699813.gif" 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%2Fmedia1.tenor.com%2Fimages%2Ffd959fd0c2d6d9b2cc9fc049f2296489%2Ftenor.gif%3Fitemid%3D18699813.gif" alt="Kinda messy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Situations like these can easily be handled using &lt;strong&gt;media queries.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;How do we add &lt;strong&gt;media queries&lt;/strong&gt;?&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;768px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nc"&gt;.flex-container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;;&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;ul&gt;
&lt;li&gt;What does the above code mean?

&lt;ul&gt;
&lt;li&gt;The above code targets all devices whose width is less than 768px (mobile devices).&lt;/li&gt;
&lt;li&gt;Then applies styling to the flex-container such that the image and text are displayed vertically instead of horizontally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;NOTE&lt;/em&gt;&lt;/strong&gt;: This style is applied to the &lt;code&gt;flex-container&lt;/code&gt; class only if the web page is displayed as &lt;strong&gt;&lt;em&gt;screen&lt;/em&gt;&lt;/strong&gt; (not printed document) &lt;strong&gt;AND&lt;/strong&gt; the &lt;strong&gt;&lt;em&gt;width of the viewport is at most 768px&lt;/em&gt;&lt;/strong&gt; (width &amp;lt;= 768px).&lt;/li&gt;
&lt;li&gt;Once the breakpoint is crossed (769px), the image and text are displayed horizontally.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We have seen three highly used techniques for creating a good  responsive website.&lt;/li&gt;
&lt;li&gt;Viewports and liquid layouts are highly useful when breakpoints in a website are not continuous. &lt;/li&gt;
&lt;li&gt;That is when there is a gap between in the viewport size where media query is introduced. &lt;/li&gt;
&lt;li&gt;Although all styles and media queries can be added to the same stylesheet, it is a good practice to support media queries and generic styles.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are modern techniques such as &lt;em&gt;grid&lt;/em&gt; and &lt;em&gt;flexbox layout&lt;/em&gt;, which are also gaining popularity.&lt;br&gt;
They handle gaps between breakpoints better than the above mentioned technology.&lt;/p&gt;

&lt;p&gt;But nevertheless, the 3 discussed techniques are not &lt;strong&gt;obsolete&lt;/strong&gt;. They are still practiced by developers, and are often used along with the modern techniques.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In conclusion, &lt;strong&gt;&lt;em&gt;responsive design does not end here&lt;/em&gt;&lt;/strong&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are many resources available on the internet that are helpful in mastering responsive design.&lt;br&gt;
I suggest doing a Google search to explore more about this topic.&lt;/p&gt;

&lt;p&gt;Lastly, I would also recommend using the best of both worlds! The 3 technologies discussed here as well as the modern techniques.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Be responsible and make your sites responsive!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hope you had a good read. Do leave a heart, pin or save it, and share this among your dev friends!&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>html</category>
      <category>design</category>
    </item>
    <item>
      <title>Common Use Cases of CSS Flexbox</title>
      <dc:creator>Bharati Subramanian</dc:creator>
      <pubDate>Wed, 02 Jun 2021 12:43:03 +0000</pubDate>
      <link>https://dev.to/bharati21/common-use-cases-of-css-flexbox-39ek</link>
      <guid>https://dev.to/bharati21/common-use-cases-of-css-flexbox-39ek</guid>
      <description>&lt;p&gt;The last two posts of this series on &lt;strong&gt;&lt;em&gt;CSS Flexbox&lt;/em&gt;&lt;/strong&gt; focused on all the properties of flexbox applied to flex-container and flex-items.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;em&gt;In this post, we will go through some of the popular and common use cases of CSS flexbox.&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This post is quite long as I have explained all the use cases in detail.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;At the end of this blog post, I will also share some useful resources to learn CSS Flexbox in the easiest possible way!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  COMMON USE CASES OF CSS FLEXBOX
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Aligning items with space between&lt;/li&gt;
&lt;li&gt;Aligning items horizontally&lt;/li&gt;
&lt;li&gt;Centering content&lt;/li&gt;
&lt;li&gt;Splitting a container halfway&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;We will use the example in the images below and try to see how flexbox was used create this webpage.&lt;/em&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%2Fxc2hsgqo5jtlhkwn3tef.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%2Fxc2hsgqo5jtlhkwn3tef.png" alt="Alt Text"&gt;&lt;/a&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%2Fxmtqsnzjpuhl21u3qc5y.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%2Fxmtqsnzjpuhl21u3qc5y.png" alt="Alt Text"&gt;&lt;/a&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%2F4zimr00lk705n32w0o6e.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%2F4zimr00lk705n32w0o6e.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/bhaaratii/pen/qBrxRNe" rel="noopener noreferrer"&gt;Find the template code for the website here&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  1. ALIGNING ITEMS WITH SPACE BETWEEN
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;One use of flexbox is to align elements with space between them.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the example website, navbar in the header has 2 sections: &lt;strong&gt;&lt;em&gt;logo&lt;/em&gt;&lt;/strong&gt; and the &lt;strong&gt;&lt;em&gt;navigation links&lt;/em&gt;&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;They are placed such that there is &lt;em&gt;space between&lt;/em&gt; them, and are pushed to either side of the navbar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the template code below for the navbar, let's see how the logo and navbar can be pushed with space between them.&lt;br&gt;&lt;br&gt;
  &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/YzZEVay?height=600&amp;amp;default-tab=html,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To convert this into a proper navbar, we need to focus on the parent of both &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt;: &lt;strong&gt;&lt;em&gt;the div with &lt;code&gt;navbar&lt;/code&gt; class&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nc"&gt;.navbar&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;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&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;/li&gt;

&lt;li&gt;&lt;p&gt;That's all! 3 lines of code and then logo and navigation links are pushed to either sides with space between. &lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;display: flex&lt;/code&gt; converts the &lt;em&gt;navbar&lt;/em&gt; container into a flex-container. 

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;nav&amp;gt;&lt;/code&gt; are placed horizontally. &lt;code&gt;flex-direction: row;&lt;/code&gt; is the default main-axis.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;justify-content: space-between;&lt;/code&gt; separates the flex-items with space between them and pushes them to opposite sides of the container (main-start and main-end).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;align-items: center;&lt;/code&gt; places the flex-items in the vertical center of the flex-container.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;NOTE&lt;/em&gt;&lt;/strong&gt;: When &lt;code&gt;justify-content: space-between;&lt;/code&gt; is used on a container with more than 2 flex-items, then they are spread out within the container, with 1&lt;sup&gt;st&lt;/sup&gt; item near the main-start and last item pushed to main-end. &lt;/li&gt;
&lt;li&gt;This can be used in the situation when header only has navlinks.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;a href="https://dev.to/bharati21/css-flexbox-making-layout-design-easy-part-1-24l5"&gt;Go through this post to learn about justify-content&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Check out the GIF below to understand how the above code works. &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%2F8ob9jxt803mc7li750tr.gif" 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%2F8ob9jxt803mc7li750tr.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the final look of the navbar with space between the logo and navlinks.&lt;br&gt;
  &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/MWpOoBg?height=600&amp;amp;default-tab=html,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  2. ALIGNING ITEMS HORIZONTALLY
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Another common use case of flexbox is to align items horizontally within a container&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the example website, the navlinks have been placed horizontally within the navbar. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the example use case 1 above, logo and the navlinks have been placed properly, but the navlinks are still displayed &lt;em&gt;vertically&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the code below as template, let's see how the navigation links can be aligned horizontally.&lt;br&gt;
  &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/MWpOoBg?height=600&amp;amp;default-tab=html,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To align all the navlinks horizontally, we need to focus on the &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; within &lt;code&gt;.navbar nav&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nc"&gt;.navbar&lt;/span&gt; &lt;span class="nt"&gt;nav&lt;/span&gt; &lt;span class="nt"&gt;ul&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;flex&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;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;display: flex&lt;/code&gt; makes sure the &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; becomes a flex-container, and by default all the children, &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; are horizontally aligned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check out the GIF below to understand how this works. &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%2Fnxd3nhs3o4jir2u8d98s.gif" 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%2Fnxd3nhs3o4jir2u8d98s.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the final look of the navlinks within the navbar.&lt;br&gt;
  &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/eYveRjN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  3. CENTERING CONTENT
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This is probably the most common use case of flexbox: to center content vertically and horizontally.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the example webpage above, notice how text within the showcase image is centered vertically as well as horizontally.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the below starter code, let's see how text (or any other content!) can be centered within a container.&lt;br&gt;
  &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/gOmXXGv?height=600&amp;amp;default-tab=html,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need to grab the parent container of the content that needs to be centered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In this case, parent element of &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; is the &lt;strong&gt;&lt;em&gt;div with id &lt;code&gt;showcase-content&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nf"&gt;#showcase-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;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&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;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Here the main-axis is column, so applying flex to the &lt;code&gt;#showcase-container&lt;/code&gt; stacks &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; vertically.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;justify-content: center;&lt;/code&gt; centers content along the main-axis: column. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;align-items: center;&lt;/code&gt; aligns content to the center along the cross-axis: row.

&lt;ul&gt;
&lt;li&gt;Using these together centers content vertically and horizontally.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;&lt;em&gt;NOTE&lt;/em&gt;&lt;/strong&gt;: You can play around with combinations of &lt;code&gt;justify-content&lt;/code&gt; and &lt;code&gt;align-items&lt;/code&gt; to test various alignment of elements within the parent container. &lt;/li&gt;

&lt;li&gt;&lt;a href="https://dev.to/bharati21/css-flexbox-making-layout-design-easy-part-1-24l5"&gt;Check out this post to learn more about justify-content and align-items&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Check out the GIF below to understand how centering content works. &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%2Fdcia4o5f578ano7z4t6l.gif" 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%2Fdcia4o5f578ano7z4t6l.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the final look of the showcase and text within.&lt;br&gt;
  &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/LYWOQxv?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  4. SPLITTING A CONTAINER HALFWAY
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The main agenda behind flexbox is that it easily aligns elements even if the initial size is unknown or dynamic.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the example webpage above, the cards in the main section are split such that one half has an image and the other half content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the below starter code, let's see how we can into two equal sections.&lt;br&gt;
  &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/YzZYPZv?height=600&amp;amp;default-tab=html&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As usual let's grab the parent container of the container that needs to be split.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The parent of &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;h3&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; container with class &lt;code&gt;card-body-column&lt;/code&gt;, which is the &lt;strong&gt;&lt;em&gt;div with class &lt;code&gt;card-body&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nc"&gt;.card-body&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;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;flex-wrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&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;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;The above code makes both the div with class &lt;code&gt;card-body-column&lt;/code&gt; flex-items.

&lt;ul&gt;
&lt;li&gt;Although these 2 items are supposed to be aligned horizontally they are wrapped onto different lines since their initial width overflows the container. This behavior is due to &lt;code&gt;flex-wrap: wrap;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Let's now focus on aligning the items horizontally for which we need to grab the container with class &lt;code&gt;card-body-column&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nc"&gt;.card-body&lt;/span&gt; &lt;span class="nc"&gt;.card-body-column&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nc"&gt;.card-body&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;object-fit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cover&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;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Adding &lt;code&gt;flex: 1;&lt;/code&gt; to &lt;code&gt;.card-body-column&lt;/code&gt; makes the container grow or shrink from same initial &lt;code&gt;width: 0&lt;/code&gt;. Hence, now we obtain 2 equal containers that grows equally.

&lt;ul&gt;
&lt;li&gt;But the image does not occupy the entire part of the container, hence we set the width and height to 100%. &lt;/li&gt;
&lt;li&gt;To ensure that width to height ratio is not distorted as the container shrinks or grows we also add &lt;code&gt;object-fit: cover;&lt;/code&gt;. This places the image as a cover in it's part of the container. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;NOTE&lt;/em&gt;&lt;/strong&gt;: We can split the containers into sections of different ratios by increasing the value of &lt;code&gt;flex&lt;/code&gt; for one half of the container.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Check out the GIF below to understand how splitting of content works. &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%2Fvypa2eujdlkp2e3s2l9u.gif" 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%2Fvypa2eujdlkp2e3s2l9u.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out the GIF below how &lt;code&gt;object-fit: cover;&lt;/code&gt; helps the image.&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%2Fxmidf7z69k4szc69vbrt.gif" 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%2Fxmidf7z69k4szc69vbrt.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the final look of the showcase and text within. &lt;br&gt;
  &lt;iframe height="600" src="https://codepen.io/bhaaratii/embed/JjWMzJj?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
 &lt;/p&gt;



&lt;h1&gt;
  
  
  FLEXBOX AND MEDIA QUERIES
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;A good website is one that works seamlessly on all devices of all sizes.&lt;/li&gt;
&lt;li&gt;Although flexbox makes layouts easy, we need to add responsiveness. This can be done using media queries.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's look at the image within the card container mentioned above in use case 4.&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%2Fwgkjfjcvx891dyamvyyt.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%2Fwgkjfjcvx891dyamvyyt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Notice how this image shrinks in screens of small sizes. This obviously isn't very appealing. So let's tackle this issue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's set a max width and ensure that once the screen width becomes smaller than the max-width, we will change the &lt;code&gt;flex-direction&lt;/code&gt; to column.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This way when the screen size becomes too small, the card content will be stacked on top of each other hence avoids shrinking.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;540px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;.card-body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;;&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;ul&gt;
&lt;li&gt;As the screen size becomes less than 541px, the main-axis becomes column. Otherwise, it's the default row.
&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%2Fd53o1jenk7bipetd2d04.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same responsiveness can be applied to the logo and navlinks to be stacked vertically and in the center the navbar.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Using just 4 common use cases, we managed to build a small webpage! &lt;a href="https://codepen.io/bhaaratii/pen/dyvdNpP" rel="noopener noreferrer"&gt;Find the completed website code here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are so many more use of flexbox out there which you can easily discover by tinkering with flexbox properties and creating websites.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;As as a side note, different developers use varying combinations of flex properties to bring out the same effect.&lt;/em&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For e.g. to separate the logo and navlinks, you may see that &lt;code&gt;justify-content: space-between&lt;/code&gt; may not be used, but rather &lt;code&gt;align-self&lt;/code&gt; is used on the individual flex-items.

&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Another major takeaway is that flexbox is not the end of responsive design, and this may not be the easiest solution for certain situations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For e.g., say you want to split your website into header, main, sidebar and footer. In such cases, &lt;strong&gt;&lt;em&gt;CSS GRID&lt;/em&gt;&lt;/strong&gt; is much more &lt;em&gt;simpler&lt;/em&gt; and &lt;em&gt;faster&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;THERE IS NO SINGLE RIGHT WAY TO DO THINGS! YOU ADAPT AND LEARN AS YOU EXPLORE!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h1&gt;
  
  
  RESOURCES
&lt;/h1&gt;

&lt;p&gt;As promised, I'll drop a few resources that helped me in learning CSS Flexbox easily and efficiently. Hope this helps you.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout" rel="noopener noreferrer"&gt;CSS MDN Web Docs&lt;/a&gt;: Take your time to read through the docs as the explanations are highly detailed and precise.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://css-tricks.com/snippets/css/a-guide-to-flexbox/" rel="noopener noreferrer"&gt;CSS Tricks&lt;/a&gt;: Another gem of a resource where basics for flexbox are explained as well.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.w3.org/TR/css-flexbox-1/" rel="noopener noreferrer"&gt;W3C Flexbox Docs&lt;/a&gt;: A very detailed and in-depth understanding to CSS Flexbox&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.w3schools.com/css/css3_flexbox.asp" rel="noopener noreferrer"&gt;W3Schools&lt;/a&gt;: Another resource having short explanations with good examples&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These aren't enough as once you grasp the theory, you will have to apply them somewhere. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Below are some fascinating games and challenges which will help you understand flexbox practically AND visually!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.flexboxdefense.com/" rel="noopener noreferrer"&gt;Flexbox Defense&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;A wonderful fantasy game where you use flex properties to stop enemies from getting into your defenses.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="http://flexboxfroggy.com/" rel="noopener noreferrer"&gt;Flexbox Froggy&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Another game where flex properties are used to help froggy and friends to guide them to their lillipads!&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://the-echoplex.net/flexyboxes/" rel="noopener noreferrer"&gt;Flexbox Code Generator&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;This site allows you to visualize flex properties, and play around by changing combination of properties&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://www.flexboxpatterns.com/" rel="noopener noreferrer"&gt;Building UI with CSS Flex&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;This site has challenges for you to build neat, and awesome UIs with the help of CSS Flexbox.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;a href="https://devchallenges.io/" rel="noopener noreferrer"&gt;Dev Challenges&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Another website that has front-end and responsive web developer challenges to build components and sites!&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;p&gt;And that's a (flex) wrap on the CSS Flexbox series.&lt;/p&gt;

&lt;p&gt;Thank you so much for going through the blog post and sticking till the end. &lt;br&gt;
Please do leave a like and share it with your dev friends!&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>webdev</category>
      <category>flexbox</category>
    </item>
    <item>
      <title>Making Layouts Easy With CSS Flexbox: Flex-Item Properties</title>
      <dc:creator>Bharati Subramanian</dc:creator>
      <pubDate>Mon, 24 May 2021 08:28:58 +0000</pubDate>
      <link>https://dev.to/bharati21/css-flexbox-making-layouts-easy-part-2-4ok2</link>
      <guid>https://dev.to/bharati21/css-flexbox-making-layouts-easy-part-2-4ok2</guid>
      <description>&lt;h1&gt;
  
  
  What is Flexbox CSS?
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;It is a responsive design layout that distributes elements within their parent container, even if the size of items is unknown or dynamic (hence the term "flex")&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the &lt;a href="https://dev.to/bharati21/css-flexbox-making-layout-design-easy-part-1-24l5"&gt;previous post&lt;/a&gt;, I covered all the properties that are applied to the parent flex container item.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To reiterate, &lt;strong&gt;&lt;em&gt;these are the properties applied to flex container (parent)&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;display&lt;/li&gt;
&lt;li&gt;flex-direction&lt;/li&gt;
&lt;li&gt;flex-wrap&lt;/li&gt;
&lt;li&gt;flex-flow&lt;/li&gt;
&lt;li&gt;justify-content&lt;/li&gt;
&lt;li&gt;align-items&lt;/li&gt;
&lt;li&gt;align-content

&lt;ul&gt;
&lt;li&gt;For detailed explanation, go through &lt;a href="https://dev.to/bharati21/css-flexbox-making-layout-design-easy-part-1-24l5"&gt;this post&lt;/a&gt;.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;In this post, we will go through the properties that are applied to flex items (children of flex parent container).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can check out the entire demo here: &lt;a href="https://demo-flexbox.netlify.app/" rel="noopener noreferrer"&gt;https://demo-flexbox.netlify.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check out the entire code here: &lt;a href="https://github.com/bharati-21/Flexbox-CSS" rel="noopener noreferrer"&gt;https://github.com/bharati-21/Flexbox-CSS&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h1&gt;
  
  
  BASICS
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Before explaining the properties applied to flex items, there are a few concepts that determine how much space a flex item occupies in it's container.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;POSITIVE AND NEGATIVE FREE SPACE&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Positive Free Space&lt;/strong&gt;: The extra space available when the flex container has more than required space to display flex items within the container. 

&lt;ul&gt;
&lt;li&gt;This space can be distributed among the items so as to occupy the entire container.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Negative Free space&lt;/strong&gt;: Opposite of &lt;em&gt;positive free space&lt;/em&gt;, this is when the space required by flex items combined is more than the flex container can accommodate. 

&lt;ul&gt;
&lt;li&gt;This space can be removed from flex-items so as to occupy the exact size of the container.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the &lt;code&gt;flex-direction: row&lt;/code&gt;, the size of flex items is determined by their width. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the &lt;code&gt;flex-direction: column&lt;/code&gt;, the size of flex items is determined by their height.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  PROPERTIES APPLIED TO FLEX ITEMS (CHILDREN)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Following are the properties applied to flex items:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;order&lt;/li&gt;
&lt;li&gt;flex-basis&lt;/li&gt;
&lt;li&gt;flex-grow&lt;/li&gt;
&lt;li&gt;flex-shrink&lt;/li&gt;
&lt;li&gt;flex&lt;/li&gt;
&lt;li&gt;align-self&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Let's use the following code to demonstrate how each of these properties work:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;1&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;2&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;3&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;4&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-5"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;5&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;order&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By default, all the flex items are displayed in the order of HTML source code.&lt;/li&gt;
&lt;li&gt;The order of flex-items appearing within the container can be modified using the &lt;strong&gt;&lt;em&gt;order&lt;/em&gt;&lt;/strong&gt; property.&lt;/li&gt;
&lt;li&gt;The default order for every flex items is: &lt;strong&gt;0&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;This property takes a number that specifies the order, and items are placed accordingly from the items having lowest order values first&lt;/em&gt;&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Order can be a &lt;strong&gt;negative value&lt;/strong&gt;, which becomes useful when one item needs to be placed before other items.&lt;/li&gt;
&lt;li&gt;If more than one item is assigned the same order value, then within that group, items are placed according to HTML source order.
&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%2Fvfrut4vub2o2mh582ohd.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;   &lt;span class="nc"&gt;.flex-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nl"&gt;order&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;default&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="nf"&gt;#item1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nl"&gt;order&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4&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;ul&gt;
&lt;li&gt;In the above image, the first container shows the original order of elements as per HTML source code.&lt;/li&gt;
&lt;li&gt;This order is altered using the &lt;strong&gt;&lt;em&gt;order&lt;/em&gt;&lt;/strong&gt; property in the second container and a code snippet is shown above.

&lt;ul&gt;
&lt;li&gt;Each of the flex items are displayed from lowest to highest values of &lt;strong&gt;&lt;em&gt;order&lt;/em&gt;&lt;/strong&gt;. (3 &amp;lt; 2 &amp;lt; 4 &amp;lt; 5 &amp;lt; 1)&lt;/li&gt;
&lt;li&gt;Notice how 2 and 4 have the same &lt;strong&gt;&lt;em&gt;order&lt;/em&gt;&lt;/strong&gt; and are displayed according to HTML source order.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;flex-basis&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This property specifies the initial size of the flex item before positive space in the container is distributed among the items.&lt;/li&gt;
&lt;li&gt;The default value of this property is &lt;strong&gt;auto&lt;/strong&gt;, meaning the browser looks at the height/ width of the flex item or the max-content size (in supporting browsers) to distribute space.&lt;/li&gt;
&lt;li&gt;It also takes value &lt;strong&gt;content&lt;/strong&gt;, which specifies that the flex-item should occupy space within the container based on it's content. 

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;This is a newer implementation and has less browser support&lt;/em&gt;. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The property also accepts an absolute value for its length. For e.g. 1rem, 200px, 15% etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-basis&lt;/em&gt;&lt;/strong&gt; can also be set to &lt;strong&gt;0&lt;/strong&gt;: This is same as specifying &lt;code&gt;width: 0;&lt;/code&gt;. Hence the flex item will be collapsed to its smallest possible size based on the content.
&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%2Fae5jqdswja8gbjda381u.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;   &lt;span class="nc"&gt;.flex-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nl"&gt;flex-basis&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;default&lt;/span&gt; &lt;span class="err"&gt;is&lt;/span&gt; &lt;span class="err"&gt;auto&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="nf"&gt;#item3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nl"&gt;flex-basis&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;200px&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;ul&gt;
&lt;li&gt;In the above image, container 1 shows the default case when &lt;code&gt;flex-basis: auto;&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;The flex items are auto-sized and in supporting browsers, the flex items are set to initial size based on their content.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The second container has flex items set to varying &lt;strong&gt;&lt;em&gt;flex-basis&lt;/em&gt;&lt;/strong&gt; values using absolute values.&lt;/li&gt;
&lt;li&gt;A code snippet for the example is shown above.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;flex-grow&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This property dictates the factor by which a flex item &lt;em&gt;grows&lt;/em&gt; relative to other items in the container when the positive space is filled.&lt;/li&gt;
&lt;li&gt;The default value for &lt;strong&gt;&lt;em&gt;flex-grow&lt;/em&gt;&lt;/strong&gt; is &lt;strong&gt;0&lt;/strong&gt;. The extra space remains unoccupied.&lt;/li&gt;
&lt;li&gt;If the same &lt;strong&gt;&lt;em&gt;flex-grow&lt;/em&gt;&lt;/strong&gt; value is set for all the items, the positive space in the container is &lt;em&gt;equally distributed&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;It takes a &lt;strong&gt;&lt;em&gt;positive value&lt;/em&gt;&lt;/strong&gt; which defines the relative growth factor.&lt;/li&gt;
&lt;li&gt;The positive value is a ratio and could be any number: 2, 200, 50 or 1.5.
&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%2Fgf0e775g2nej4xnspovj.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;      &lt;span class="nf"&gt;#container2&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;.flex-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nl"&gt;flex-grow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nf"&gt;#container3&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;#item1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nl"&gt;flex-grow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&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;ul&gt;
&lt;li&gt;In the image above, the first container depicts the default case where, &lt;code&gt;flex-grow: 0;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the second container, all the flex items are set the same &lt;code&gt;flex-grow: 1;&lt;/code&gt; value. 

&lt;ul&gt;
&lt;li&gt;Hence the positive space is distributed to all the items and they &lt;em&gt;grow&lt;/em&gt; equally &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;In the third container, each of the flex-items are set with different &lt;strong&gt;&lt;em&gt;flex-grow&lt;/em&gt;&lt;/strong&gt; value, and hence their growth varies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt; flex-grow deals with the situation where the container has more space than required to fit the flex items. 

&lt;ul&gt;
&lt;li&gt;Hence, it &lt;strong&gt;adds&lt;/strong&gt; space to the flex items to fit to the container.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;flex-shrink&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This property indicates the relative factor by which a flex item &lt;em&gt;shrinks&lt;/em&gt; when negative space is distributed among items in a container.&lt;/li&gt;
&lt;li&gt;The default value for this property is &lt;strong&gt;1&lt;/strong&gt;, which specifies the browser to shrink the item by a factor of its &lt;strong&gt;&lt;em&gt;flex-basis&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;This property takes a &lt;strong&gt;&lt;em&gt;positive value&lt;/em&gt;&lt;/strong&gt;, and flex items are shrunk accordingly by and do not overflow the container.&lt;/li&gt;
&lt;li&gt;When all the flex items are provided the same flex-shrink ratio, they shrink and the negative space is evenly distributed among them.&lt;/li&gt;
&lt;li&gt;When &lt;strong&gt;&lt;em&gt;flex-shrink&lt;/em&gt;&lt;/strong&gt; is set to &lt;strong&gt;0&lt;/strong&gt;, none of the items shrink and overflow if the size of the container is smaller than all the items.&lt;/li&gt;
&lt;li&gt;Similar to &lt;strong&gt;&lt;em&gt;flex-grow&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;flex-shrink&lt;/em&gt;&lt;/strong&gt; takes a value which is a relative factor and could be any positive number: 2, 200, 50 or 1.5.
&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%2Foes466cr9dcqm910yakt.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;   &lt;span class="nf"&gt;#container2&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;.flex-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nl"&gt;flex-shrink&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;default&lt;/span&gt; &lt;span class="err"&gt;is&lt;/span&gt; &lt;span class="err"&gt;1&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="nf"&gt;#container3&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;#item5&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nl"&gt;flex-shrink&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2.5&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;ul&gt;
&lt;li&gt;In the image above, the first container depicts the default case where, &lt;code&gt;flex-shrink: 1;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In the second container, all the flex items are set to &lt;code&gt;flex-shrink: 0;&lt;/code&gt;, and the items overflow the container. &lt;/li&gt;
&lt;li&gt;In the third container, each of the flex-items are set with different &lt;strong&gt;&lt;em&gt;flex-shrink&lt;/em&gt;&lt;/strong&gt; values, and hence the factor by which they shrink varies.

&lt;ul&gt;
&lt;li&gt;A code snippet for the example in the image is shown above.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt; flex-shrink deals with situations where the initial flex-basis value of all flex-items is greater than the available space in the flex-container. 

&lt;ul&gt;
&lt;li&gt;Hence, it &lt;strong&gt;takes away&lt;/strong&gt; the space from flex items to fit the container. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;flex&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is a shorthand property that combines &lt;strong&gt;&lt;em&gt;flex-grow&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;flex-shrink&lt;/em&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;em&gt;flex-basis&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The flex-shrink and flex-basis values are &lt;em&gt;optional&lt;/em&gt; in this property.&lt;/li&gt;
&lt;li&gt;This property can take values in the following manner:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-initial&lt;/em&gt;&lt;/strong&gt;: &lt;code&gt;flex: 0 1 auto | 0 auto; //default&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;This makes the flex item partially flexible. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-basis&lt;/em&gt;&lt;/strong&gt; is set to auto and items are auto-sized if there is no main-size (width/ height) specified.&lt;/li&gt;
&lt;li&gt;Item shrinks by a factor of 1 if the container cannot accommodate the flex items, but the item does not grow if there is positive space to be filled&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-auto&lt;/em&gt;&lt;/strong&gt;: &lt;code&gt;flex: auto | 1 1 auto;&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;This sets the &lt;strong&gt;&lt;em&gt;flex-basis&lt;/em&gt;&lt;/strong&gt; based on initial height/ width properties, but the item is flexible and grows or shrinks when necessary.&lt;/li&gt;
&lt;li&gt;This property can take positive values other than 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-none&lt;/em&gt;&lt;/strong&gt;: &lt;code&gt;flex: none| 0 0 auto;&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;This sets the &lt;strong&gt;&lt;em&gt;flex-basis&lt;/em&gt;&lt;/strong&gt; based on initial height/ width properties, but the item is inflexible and &lt;strong&gt;does not&lt;/strong&gt; grow or shrink.&lt;/li&gt;
&lt;li&gt;Hence items overflow if the container size is not enough to fit the items.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex&lt;/em&gt;&lt;/strong&gt; can also take values accepted by the individual properties.
&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%2Fn6om0zuxzhoiirudnx6b.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;   &lt;span class="nf"&gt;#container1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;.flex-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;default&lt;/span&gt; &lt;span class="err"&gt;is&lt;/span&gt; &lt;span class="err"&gt;flex-initial&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="nf"&gt;#container2&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;.flex-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="nf"&gt;#container3&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;.flex-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="nb"&gt;auto&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;ul&gt;
&lt;li&gt;The above code snippet shows the cases &lt;strong&gt;&lt;em&gt;flex-initial&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;flex-auto&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;flex-none&lt;/em&gt;&lt;/strong&gt; explained above and shown in the image (first three containers in the image).
&lt;pre&gt;
&lt;/pre&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;   &lt;span class="nf"&gt;#container4&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;.flex-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;flex-grow&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;flex-basis&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;flex-grow&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;flex-basis&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;flex-grow&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;flex-shrink&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;flex-grow&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;flex-shrink&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;flex-basis&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex&lt;/em&gt;&lt;/strong&gt; can also take combination of &lt;strong&gt;&lt;em&gt;flex-grow&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;flex-shrink&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;flex-basis&lt;/em&gt;&lt;/strong&gt; values as shown in the code snippet above and last container in the image.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;align-self&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This property controls the alignment of a flex item along the cross-axis.&lt;/li&gt;
&lt;li&gt;It overrides the &lt;strong&gt;&lt;em&gt;align-items&lt;/em&gt;&lt;/strong&gt; value for a specific flex item&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;align-self&lt;/em&gt;&lt;/strong&gt; accepts all the values assigned to align-items plus value of &lt;strong&gt;auto&lt;/strong&gt; which is the default.&lt;/li&gt;
&lt;li&gt;The following are the values accepted by the property"

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;auto&lt;/em&gt;&lt;/strong&gt;: (default) inherits the &lt;strong&gt;&lt;em&gt;align-items&lt;/em&gt;&lt;/strong&gt; value of the parent property.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;stretch&lt;/em&gt;&lt;/strong&gt;: stretches the cross-size of the flex item to fit the entire cross axis of the container &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-start&lt;/em&gt;&lt;/strong&gt;: flex item is placed at the start of cross axis (cross-start)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-end&lt;/em&gt;&lt;/strong&gt;: flex item is placed at the end of cross axis (cross-end)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;center&lt;/em&gt;&lt;/strong&gt;: flex item is placed in the center of cross axis&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;baseline&lt;/em&gt;&lt;/strong&gt;: flex item is aligned such that the baselines of items displayed as &lt;code&gt;align-self: baseline;&lt;/code&gt; are aligned. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/bharati21/css-flexbox-making-layout-design-easy-part-1-24l5"&gt;Check out this post to understand align-items, cross-axis, cross-size and baselines&lt;/a&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%2Fu78dco246j8l4jgxs0rc.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nf"&gt;#item1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;align-self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;default&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;#item2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;align-self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex-start&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;#item3&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;align-self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex-end&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;#item4&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;align-self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;#item5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;#item6&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;align-self&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;baseline&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;ul&gt;
&lt;li&gt;The above code snippet aligns the flex items within the container as shown in the image above.

&lt;ul&gt;
&lt;li&gt;Since &lt;strong&gt;stretch&lt;/strong&gt; is the &lt;strong&gt;&lt;em&gt;align-items&lt;/em&gt;&lt;/strong&gt; value for the parent property, the first flex item set to &lt;strong&gt;auto&lt;/strong&gt;, inherits this value and stretches across the column.&lt;/li&gt;
&lt;li&gt;Notice how baselines for flex items 5 and 6 are aligned. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ol&gt;




&lt;p&gt;Thank you for reading the part 2 of the blog on CSS Flexbox series. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can check out the entire demo here: &lt;a href="https://demo-flexbox.netlify.app/" rel="noopener noreferrer"&gt;https://demo-flexbox.netlify.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check out the entire code here: &lt;a href="https://github.com/bharati-21/Flexbox-CSS" rel="noopener noreferrer"&gt;https://github.com/bharati-21/Flexbox-CSS&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;In the next blog, I will focus on common use cases and scenarios where CSS Flexbox is used.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Do leave a thumbs up, save and pin this blog if it helped you in understanding the properties applied to flex items. &lt;br&gt;
Hopefully you might now be able to use these concepts in creating better layouts.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>design</category>
      <category>flexbox</category>
    </item>
    <item>
      <title>Making Layouts Easy With CSS Flexbox: Flex-Container Properties</title>
      <dc:creator>Bharati Subramanian</dc:creator>
      <pubDate>Sun, 16 May 2021 14:33:06 +0000</pubDate>
      <link>https://dev.to/bharati21/css-flexbox-making-layout-design-easy-part-1-24l5</link>
      <guid>https://dev.to/bharati21/css-flexbox-making-layout-design-easy-part-1-24l5</guid>
      <description>&lt;p&gt;&lt;em&gt;CSS Flexbox is a responsive design layout that helps in aligning HTML elements efficiently&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Flexbox distributes elements within their parent container, even if the size of items is unknown or dynamic (hence the term "flex").&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;There are various properties associated with this model, that are either applied to the parent flex container or the children flex items. &lt;/li&gt;
&lt;li&gt;In this post, I will try to explain all the properties that are applied to the flex-container.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;You can check out the entire demo here: &lt;a href="https://demo-flexbox.netlify.app/" rel="noopener noreferrer"&gt;https://demo-flexbox.netlify.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check out the entire code here: &lt;a href="https://github.com/bharati-21/Flexbox-CSS" rel="noopener noreferrer"&gt;https://github.com/bharati-21/Flexbox-CSS&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;This could be a long blog post as there are lots of basics to cover!&lt;/em&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  BASICS
&lt;/h1&gt;

&lt;p&gt;While working with flex, items are aligned either horizontally or vertically.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The parent container that holds items is referred to as the &lt;strong&gt;&lt;em&gt;flex-container&lt;/em&gt;&lt;/strong&gt;, and the items/ children as the &lt;strong&gt;&lt;em&gt;flex-items&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;When elements are displayed as flex, they are laid out along two axes: 

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;main-axis&lt;/em&gt;&lt;/strong&gt;: The direction along which items are laid. This depends on the &lt;strong&gt;&lt;em&gt;flex-direction&lt;/em&gt;&lt;/strong&gt; property.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;cross-axis&lt;/em&gt;&lt;/strong&gt;: The direction perpendicular to the main axis. This also depends on the &lt;strong&gt;&lt;em&gt;flex-direction&lt;/em&gt;&lt;/strong&gt; property.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;A flex container's or item's height or width, whichever is the main-axis direction, is the item's &lt;strong&gt;&lt;em&gt;main-size&lt;/em&gt;&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;A flex item's height or width, whichever is the cross-axis direction, is the item's &lt;strong&gt;&lt;em&gt;cross-size&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The flex items are laid along the main-axis starting from the &lt;strong&gt;&lt;em&gt;main-start&lt;/em&gt;&lt;/strong&gt; towards &lt;strong&gt;&lt;em&gt;main-end&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The container has flex lines along the cross axis, and items are laid along these lines from &lt;strong&gt;&lt;em&gt;cross-start&lt;/em&gt;&lt;/strong&gt; towards &lt;strong&gt;&lt;em&gt;cross-end&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&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%2F2bhbwznir1tf67wxo7tf.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%2F2bhbwznir1tf67wxo7tf.png" alt="Image Explaining Flex Layout and Basic Terms"&gt;&lt;/a&gt; The image above explains flex layout and basic terminologies.&lt;/p&gt;



&lt;h1&gt;
  
  
  FLEXBOX PROPERTIES
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's use the following code to understand various properties of flex layout:&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;1&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;2&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-3"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;3&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;4&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"flex-item"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"item-5"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;5&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&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%2Fg75jio75t2jl02ix4mbf.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%2Fg75jio75t2jl02ix4mbf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The above image is the default alignment of the container in the browser. &lt;code&gt;display: block;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;h3&gt;
  
  
  PROPERTIES APPLIED TO FLEX-CONTAINER (PARENT)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;These are the properties applied to parent flex container&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;display&lt;/li&gt;
&lt;li&gt;flex-direction&lt;/li&gt;
&lt;li&gt;flex-wrap&lt;/li&gt;
&lt;li&gt;flex-flow&lt;/li&gt;
&lt;li&gt;justify-content&lt;/li&gt;
&lt;li&gt;align-items&lt;/li&gt;
&lt;li&gt;align-content
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.flex-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;flex&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;inline-flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;row-reverse&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;column-reverse&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;flex-wrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;nowrap&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;wrap-reverse&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex-start&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-end&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-around&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-evenly&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;stretch&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-start&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-end&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;baseline&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;stretch&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;align-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;stretch&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-start&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-end&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-around&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nl"&gt;flex-flow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;‘&lt;/span&gt;&lt;span class="n"&gt;flex-direction&lt;/span&gt;&lt;span class="err"&gt;’&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;||&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;‘&lt;/span&gt;&lt;span class="n"&gt;flex-wrap&lt;/span&gt;&lt;span class="err"&gt;’&amp;gt;&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;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;display&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This property applies &lt;em&gt;flexbox&lt;/em&gt; to the container and all the children become flex items. &lt;/li&gt;
&lt;li&gt;It overrides the default

&lt;code&gt;display: block;&lt;/code&gt;

property on the container.&lt;/li&gt;
&lt;li&gt;The items are automatically aligned horizontally, making the row default main-axis.&lt;/li&gt;
&lt;li&gt;The items stretch to fit the height of the container (since the default main-axis is a row), but items do not stretch across the main-axis. &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This property can take the following values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex&lt;/em&gt;&lt;/strong&gt;: the parent is displayed as a block-level flexbox container but the children are laid out as flex items. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;inline-flex&lt;/em&gt;&lt;/strong&gt;: container's children are laid out as flex-items and the container as an inline item.
&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%2F9tt0h3ghyvjbz0pqnfl8.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;  &lt;span class="nc"&gt;.flex-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;flex&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;inline-flex&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;ul&gt;
&lt;li&gt;When parent div with the class &lt;em&gt;"container"&lt;/em&gt; is displayed as a &lt;strong&gt;flex container&lt;/strong&gt;, all the children of this container (the 5 divs with class &lt;em&gt;"item"&lt;/em&gt;) become &lt;strong&gt;flex items&lt;/strong&gt;. By default, the container's main-axis becomes &lt;strong&gt;row&lt;/strong&gt;, flex-wrap is set to &lt;strong&gt;no-wrap&lt;/strong&gt; and flex-basis to &lt;strong&gt;auto&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;flex-direction&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This property controls how items are displayed in the container, i.e it defines the main-axis.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This property can take the following values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;row&lt;/em&gt;&lt;/strong&gt;: (default value) items are laid from left to right along the row of the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;row-reverse&lt;/em&gt;&lt;/strong&gt;: The items are laid horizontally, but the main-start and main-end lines are reversed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;column&lt;/em&gt;&lt;/strong&gt;: The items are laid in a column, from top to bottom.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;column-reverse&lt;/em&gt;&lt;/strong&gt;: The items are laid vertically, but the main-start and main-end lines are reversed. 
&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%2Fonl6a53ajdd21c4s0lys.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nc"&gt;.flex-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;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;row-reverse&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;column-reverse&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;ul&gt;
&lt;li&gt;The flex-direction is specified for the container which becomes the main-axis, and flex items are laid out accordingly. The cross-axis becomes perpendicular to the direction specified.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;flex-wrap&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By default, all flex items are displayed in a single line. This behavior can be changed using the flex-wrap property and items can be wrapped into multiple lines.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The property takes the following values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;nowrap&lt;/em&gt;&lt;/strong&gt;: (default) The items either shrink to fit the container or overflow if unable to shrink.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;wrap&lt;/em&gt;&lt;/strong&gt;: The items are wrapped onto multiple lines, from top to bottom, if the container is not large enough to fit all the items.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;wrap-reverse&lt;/em&gt;&lt;/strong&gt;: The items are wrapped onto multiple lines, but from bottom to top. The cross-start and cross-end lines are reversed.
&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%2Fra36j7zpv2vfdom5s2s0.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.flex-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;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;flex-wrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;nowrap&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;wrap-reverse&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;ul&gt;
&lt;li&gt;When the container is not large enough to fit all the items, &lt;strong&gt;&lt;em&gt;nowrap&lt;/em&gt;&lt;/strong&gt; value may cause the flex items to overflow out of the container if the items are unable to shrink or are very small to shrink. &lt;/li&gt;
&lt;li&gt;Making the container &lt;strong&gt;&lt;em&gt;wrap&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;wrap-reverse&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;avoids overflow&lt;/em&gt;, as flex items are wrapped onto multiple lines if they become larger than the flex container.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;flex-flow&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;This is a shorthand for flex-direction and flex-wrap properties and defines how items need to be displayed in both the main and cross axes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The default value for &lt;strong&gt;&lt;em&gt;flex-flow&lt;/em&gt;&lt;/strong&gt; is

&lt;code&gt;row nowrap;&lt;/code&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%2Ftsuv505oygbg1gdo0mb6.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt; &lt;span class="nc"&gt;.flex-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;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;flex-flow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;row-reverse&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;column-reverse&lt;/span&gt; &lt;span class="err"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;nowrap&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;wrap-reverse&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;/li&gt;
&lt;li&gt;&lt;p&gt;It takes all the values accepted for flex-direction and flex-wrap. It can take value for either one of the properties or both of them.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;justify-content&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This property distributes flex items along the main-axis. It allows to take up any extra space available along the main-axis and changes the way content is displayed.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The property takes the following values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-start&lt;/em&gt;&lt;/strong&gt;: (default) Items are aligned and packed to the start of the container along the main-axis (main-start line).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-end&lt;/em&gt;&lt;/strong&gt;: Items are packed to the end of the container along the main-axis (main-end line).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;center&lt;/em&gt;&lt;/strong&gt;: Items are packed to the center of the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;space-between&lt;/em&gt;&lt;/strong&gt;: Items are placed evenly with space between each other. The first item is placed on the main-start line, and the last item on the main-end line.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;space-around&lt;/em&gt;&lt;/strong&gt;: Similar to space-between but has space on either end of main-start and main-end lines as well. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;space-evenly&lt;/em&gt;&lt;/strong&gt;: Items are evenly distributed with equal spacing between each other and between the item and edges.
&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%2Fy8y8ipptuutsvlfbnjy2.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.flex-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;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;flex-wrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;justify-content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex-start&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-end&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-around&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-evenly&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;justify-content&lt;/em&gt;&lt;/strong&gt; property aligns items along the main-axis. In this case, since our main-axis is the &lt;strong&gt;row&lt;/strong&gt;, the property aligns items horizontally.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;align-items&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This property is used to align flex items along the cross-axis. &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The property takes the following values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;stretch&lt;/em&gt;&lt;/strong&gt;: (default) Items are stretched to fill the container across the cross-axis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-start&lt;/em&gt;&lt;/strong&gt;: Items are aligned at the start of the container's cross-axis (cross-start line).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-end&lt;/em&gt;&lt;/strong&gt;: Items are packed at the end of the container's cross-axis (cross-end line).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;center&lt;/em&gt;&lt;/strong&gt;: Items are placed at the center of the cross-axis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;baseline&lt;/em&gt;&lt;/strong&gt;: Items are placed such that their content's baselines align. This is particularly useful when all the flex items within a container have content of varying sizes. The baseline for the example is shown in the image below.
&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%2F7s556sltaor1aqg2kpwq.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.flex-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;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;flex-wrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
     &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;stretch&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-start&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-end&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;baseline&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;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;align-items&lt;/em&gt;&lt;/strong&gt; aligns flex items across the cross-axis. In this case, the property aligns flex items vertically since the main-axis is &lt;em&gt;row&lt;/em&gt;. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;align-content&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This property aligns the extra space between the flex items, called the &lt;strong&gt;&lt;em&gt;flex-lines&lt;/em&gt;&lt;/strong&gt;, along the cross-axis.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The property takes the following values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;stretch&lt;/em&gt;&lt;/strong&gt;: (default) Items are stretched to take up the extra space in the container across the cross-axis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-start&lt;/em&gt;&lt;/strong&gt;: Items are packed at the start of the container's cross-axis (cross-start line).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;flex-end&lt;/em&gt;&lt;/strong&gt;: Items are packed at the end of the container's cross-axis (cross-end line).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;center&lt;/em&gt;&lt;/strong&gt;: Items are packed and placed at the center of the cross-axis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;space-between&lt;/em&gt;&lt;/strong&gt;: Items are evenly placed across the cross-axis such that there is space between them. The first item is placed at the cross-start line and the last item at the cross-end line.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;space-around&lt;/em&gt;&lt;/strong&gt;: Items are distributed along the cross-axis such there is space around and between the items.
&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%2Fr1glb9j43vuuy7jnfaza.png" alt="Alt Text"&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.flex-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;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;flex-direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;flex-wrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wrap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;stretch&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-start&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;flex-end&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-between&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="n"&gt;space-around&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;ul&gt;
&lt;li&gt;Similar to how &lt;strong&gt;&lt;em&gt;justify-content&lt;/em&gt;&lt;/strong&gt; aligns items along the main-axis, &lt;strong&gt;&lt;em&gt;align-content&lt;/em&gt;&lt;/strong&gt; aligns flex items across the cross-axis. In this case, this property aligns flex items vertically.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;&lt;em&gt;align-content&lt;/em&gt;&lt;/strong&gt; property only takes effect when the flex items are wrapped onto multiple lines. &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;blockquote&gt;
&lt;p&gt;You can check out the entire demo here: &lt;a href="https://demo-flexbox.netlify.app/" rel="noopener noreferrer"&gt;https://demo-flexbox.netlify.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can check out the entire code here: &lt;a href="https://github.com/bharati-21/Flexbox-CSS" rel="noopener noreferrer"&gt;https://github.com/bharati-21/Flexbox-CSS&lt;/a&gt;&lt;/p&gt;


&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;In the next blog, I will focus on all the properties applied to the flex container's children (flex-items).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading the first blog on the CSS Flexbox Series!&lt;br&gt;
&lt;br&gt; &lt;br&gt;
If this blog helped you understand the flexbox concept better, do leave a thumbs up and follow if you enjoyed reading. &lt;/p&gt;

</description>
      <category>css</category>
      <category>flexbox</category>
      <category>layout</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
