<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Joy Shaheb - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Joy Shaheb - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 14 May 2026 20:24:18 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/JoyShaheb/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Learn React by Building a Mortgage Calculator ]]>
                </title>
                <description>
                    <![CDATA[ Today we will learn and practice ReactJs by building a mortgage calculator step by step. This is the project that we will build today 👇 Heres a live demo of the project And here's the Github Repo Link Objectives The topics we'll cover while build... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-mortgage-calculator-tutorial-for-beginners/</link>
                <guid isPermaLink="false">66b2096f4b36d956a7749c22</guid>
                
                    <category>
                        <![CDATA[ beginner ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Mon, 27 Mar 2023 22:56:14 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/thumbnail_EN--2-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we will learn and practice ReactJs by building a mortgage calculator step by step. This is the project that we will build today 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ryoc8jihbyprgp50ulhm.png" alt="Project Image" width="1532" height="697" loading="lazy"></p>
<ul>
<li><a target="_blank" href="https://mortgage-calculator-tutorial.vercel.app/">Heres a live demo of the project</a></li>
<li><a target="_blank" href="https://github.com/JoyShaheb/mortgage-calculator-tutorial">And here's the Github Repo Link</a></li>
</ul>
<h2 id="heading-objectives">Objectives</h2>
<p>The topics we'll cover while building this project are:</p>
<ul>
<li>React Functional Components</li>
<li>Material UI</li>
<li>User Inputs</li>
<li>Handling Props</li>
<li>props destructuring</li>
<li>useState Hook</li>
</ul>
<p>And much more! This course is excellent for beginners who want to learn ReactJs by building a real world project.</p>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like">You can watch this tutorial on YouTube as well if you like:</h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/uluphP4xXD8" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><a class="post-section-overview" href="#project-setup">Project Setup</a></li>
<li><a class="post-section-overview" href="#folder-structure">Folder Structure</a></li>
<li><a class="post-section-overview" href="#material-ui-theme">Material UI Theme</a></li>
<li><a class="post-section-overview" href="#how-to-build-the-navbar">How to Build the Navbar</a></li>
<li><a class="post-section-overview" href="#material-ui-grid-system">Material UI Grid System</a></li>
<li><a class="post-section-overview" href="#how-to-build-the-slider-component">How to Build the Slider Component</a></li>
<li><a class="post-section-overview" href="#take-a-break">Take a Break</a></li>
<li><a class="post-section-overview" href="#how-to-use-the-usestate-hook">How to Use the useState Hook</a></li>
<li><a class="post-section-overview" href="#how-to-build-the-sliderselect-component">How to Build the SliderSelect component</a></li>
<li><a class="post-section-overview" href="#how-to-build-the-tenureselect-component">How to Build the TenureSelect component</a></li>
<li><a class="post-section-overview" href="#how-to-build-the-result-component">How to Build the Result Component</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
<li><a class="post-section-overview" href="#my-social-media-links">My Social Media Links</a></li>
</ul>
<h2 id="heading-project-setup">Project Setup</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02wjpjpo4k6tjg78ynu4.png" alt="Project Setup" width="1280" height="720" loading="lazy"></p>
<p>In order to setup the project we need to install <code>react</code>, <code>material-ui</code>, and other necessary packages. </p>
<p>First create a folder named <code>mortgage-calculator</code>, open it on VS Code, and then run the following command in the terminal:</p>
<pre><code class="lang-bash">npx create-react-app .
npm install @mui/material @emotion/react @emotion/styled
npm install --save chart.js react-chartjs-2
</code></pre>
<h3 id="heading-appjs">App.js</h3>
<p>We will remove all the boilerplate code from <code>app.js</code> and keep up to this portion 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>Hello everyone<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>Then run this command in the terminal to start the server:</p>
<pre><code class="lang-bash">npm start
</code></pre>
<p>The project should look totally blank on the web browser now.</p>
<h3 id="heading-lets-start-coding">Let's start coding</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qew6q8zwn723s86md4rf.png" alt="Lets start coding" width="1280" height="720" loading="lazy"></p>
<p>Everything is setup and ready to go. Now we will start building the project :)</p>
<h2 id="heading-folder-structure">Folder Structure</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hujbmnjhri7470r2rxk.png" alt="Folder Structure" width="1280" height="720" loading="lazy"></p>
<p>Our folder structure should look like this so that we can easily manage and maintain our files &amp; folders:</p>
<pre><code class="lang-bash">mortgage-calculator/
├── src/
│   ├── Components/
│   │   ├── Common/
│   │   │   └── SliderComponent.js
│   │   ├── Navbar.js
│   │   ├── Result.js
│   │   ├── SliderSelect.js
│   │   ├── TenureSelect.js
│   ├── theme.js
│   ├── App.js
│   ├── index.js
├── .gitignore
├── package.json
└── package-lock.json
</code></pre>
<p>Here's an image of our project folder structure if you're feeling confused:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9q9ezw36rp0mcfo12qsw.png" alt="Folder Structure" width="826" height="681" loading="lazy"></p>
<h2 id="heading-material-ui-theme">Material UI Theme</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mxoseeckq7zgnidn4yol.png" alt="MUI Theme" width="1280" height="720" loading="lazy"></p>
<p>We will be using the dark theme of Material UI. For that we need to create a file named <code>theme.js</code> in the <code>src</code> folder and add the following code:</p>
<h3 id="heading-themejs">theme.js</h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { createTheme } <span class="hljs-keyword">from</span> <span class="hljs-string">'@mui/material/styles'</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> theme = createTheme({
  <span class="hljs-attr">palette</span>: {
    <span class="hljs-attr">mode</span>: <span class="hljs-string">'dark'</span>,
  },
})
</code></pre>
<h3 id="heading-indexjs">index.js</h3>
<p>Next up, we need to import the <code>theme</code> in the <code>index.js</code> file and wrap the app with the <code>ThemeProvider</code>. Follow along below: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { ThemeProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/styles"</span>;
<span class="hljs-keyword">import</span> CssBaseline <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/CssBaseline"</span>;
<span class="hljs-keyword">import</span> { theme } <span class="hljs-keyword">from</span> <span class="hljs-string">"./theme"</span>;

<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ThemeProvider</span> <span class="hljs-attr">theme</span>=<span class="hljs-string">{theme}</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">CssBaseline</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">ThemeProvider</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>
</code></pre>
<p><strong>Note:</strong> If you don't pass the <code>CssBaseline</code> component we will not be able to see the MUI dark theme.</p>
<p>This is the result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j117t2x35qebkx3qf21r.png" alt="Result so far" width="1617" height="607" loading="lazy"></p>
<p>The entire screen will be dark. This means that dark mode has been applied to our project from Material UI.</p>
<h2 id="heading-how-to-build-the-navbar">How to Build the Navbar</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tx7urj6lh5710anwtqa8.png" alt="Navbar Setup" width="1280" height="720" loading="lazy"></p>
<p>Next up, we will be creating a very simple Navbar to show the Logo. For that we need to create a file named <code>Navbar.js</code> in the <code>src/Components</code> folder and add the following code:</p>
<h3 id="heading-navbarjs">Navbar.js</h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> AppBar <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/AppBar"</span>;
<span class="hljs-keyword">import</span> Toolbar <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/Toolbar"</span>;
<span class="hljs-keyword">import</span> Typography <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/Typography"</span>;
<span class="hljs-keyword">import</span> { Container } <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/system"</span>;

<span class="hljs-keyword">const</span> Navbar = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AppBar</span> <span class="hljs-attr">position</span>=<span class="hljs-string">"static"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Container</span> <span class="hljs-attr">maxWidth</span>=<span class="hljs-string">'xl'</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Toolbar</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Typography</span> <span class="hljs-attr">variant</span>=<span class="hljs-string">"h5"</span>&gt;</span>
            Bank of React
          <span class="hljs-tag">&lt;/<span class="hljs-name">Typography</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Toolbar</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Container</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">AppBar</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Navbar;
</code></pre>
<p>Here's a quick explanation of the components used from Material UI:</p>
<ul>
<li><strong>AppBar :</strong> The Appbar component of Material UI is used for creating a top navigation bar in the user interface. <a target="_blank" href="https://mui.com/material-ui/react-app-bar/">Read more about it here</a></li>
<li><strong>Container :</strong> The Container component of Material UI is used for creating a container element that can be used to create a responsive layout, and center and contain other elements in a user interface. <a target="_blank" href="https://mui.com/material-ui/react-container/">Read more about it here</a></li>
<li><strong>ToolBar :</strong> The Toolbar component can contain elements such as buttons, text, and icons, and can also be used for creating a responsive layout that adapts to different screen sizes. <a target="_blank" href="https://mui.com/material-ui/api/toolbar/">Read more about it here</a></li>
<li><strong>Typography :</strong> Material UI's typography component is used for applying pre-defined typographic styles to text elements. It helps create a consistent and visually pleasing layout with customizable font family, size, weight and spacing. <a target="_blank" href="https://mui.com/material-ui/react-typography/">Read more about it here</a></li>
</ul>
<h3 id="heading-appjs-1">App.js</h3>
<p>Finally, import it to <code>App.js</code> and write the code like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> Navbar <span class="hljs-keyword">from</span> <span class="hljs-string">"./Components/Navbar"</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>This is the result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lv52resgdtg2wpgqq4xa.png" alt="Navbar result" width="1437" height="437" loading="lazy"></p>
<h2 id="heading-material-ui-grid-system">Material UI Grid System</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bd60xyrgs28g75eulshc.png" alt="MUI Grid System" width="1280" height="720" loading="lazy"></p>
<p>In the finalized project, we can see that our content is divided into 2 portions. On the left we have the slider components and on the right we have the pie chart. This is made possible using the <a target="_blank" href="https://mui.com/material-ui/react-grid/">Grid system of material UI</a>.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezdk2jrt1dg88wac6iyu.png" alt="Finalized project image" width="1399" height="690" loading="lazy"></p>
<p>Not only that, we can also see that the content is responsive on smaller screen sizes. This is also made possible using the Grid system of material UI.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a1e5356nbnr74y6r6ze.png" alt="Responsive content" width="507" height="1006" loading="lazy"></p>
<p>To replicate this, we need to write these things on our App.js file. You can follow along here. 👇</p>
<p>First of all, we need to import all of our required components from Material UI and our components folder:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { Grid } <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material"</span>;
<span class="hljs-keyword">import</span> { Container } <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/system"</span>;
<span class="hljs-keyword">import</span> Navbar <span class="hljs-keyword">from</span> <span class="hljs-string">"./Components/Navbar"</span>;
<span class="hljs-keyword">import</span> Result <span class="hljs-keyword">from</span> <span class="hljs-string">"./Components/Result"</span>;
<span class="hljs-keyword">import</span> SliderSelect <span class="hljs-keyword">from</span> <span class="hljs-string">"./Components/SliderSelect"</span>;
<span class="hljs-keyword">import</span> TenureSelect <span class="hljs-keyword">from</span> <span class="hljs-string">"./Components/TenureSelect"</span>;
</code></pre>
<p>Next up, we write this code inside the <code>return</code> statement like this: 👇</p>
<pre><code class="lang-js">&lt;div className=<span class="hljs-string">"App"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Container</span> <span class="hljs-attr">maxWidth</span>=<span class="hljs-string">"xl"</span> <span class="hljs-attr">sx</span>=<span class="hljs-string">{{marginTop:4}}</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">container</span> <span class="hljs-attr">spacing</span>=<span class="hljs-string">{5}</span> <span class="hljs-attr">alignItems</span>=<span class="hljs-string">"center"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">item</span> <span class="hljs-attr">xs</span>=<span class="hljs-string">{12}</span> <span class="hljs-attr">md</span>=<span class="hljs-string">{6}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">SliderSelect</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">TenureSelect</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">item</span> <span class="hljs-attr">xs</span>=<span class="hljs-string">{12}</span> <span class="hljs-attr">md</span>=<span class="hljs-string">{6}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Result</span>/&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Container</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<p>Explanation of the code:</p>
<ul>
<li><strong>Container:</strong> On the <code>Container</code> we wrote something called <code>sx={{marginTop:4}}</code>. This is the way to add inline-style to a component in Material UI.</li>
<li><strong>Grid:</strong> The Grid component is used to create a responsive layout that adapts to different screen sizes. <code>Grid container</code> represents the parent element and <code>Grid item</code> represents the child element.</li>
<li>On the <code>Grid</code> component we wrote something called <code>spacing={5}</code>. This is the way to add spacing between the grid items.</li>
<li>On the <code>Grid</code> component we also wrote <code>xs={12}</code> which means that the grid item will take up the entire width of the screen on extra small screens. Similarly, <code>md={6}</code> means that the grid item will take up half of the screen on medium and bigger screens. That's how we made our components responsive.</li>
</ul>
<p>This is the result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jr80ud2oawv7nj6xti75.png" alt="Result image of Grid system" width="1503" height="404" loading="lazy"></p>
<h2 id="heading-how-to-build-the-slider-component">How to Build the Slider Component</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1psq4l7lbwl1c2aizo6j.png" alt="Slider Component" width="1280" height="720" loading="lazy"></p>
<p>Next up, we will be creating a slider component to get the input amount from the user. It will look something like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oth4rtfgebeylr1kjktn.png" alt="Slider Component" width="757" height="135" loading="lazy"></p>
<p>For that we need to create a file named <code>SliderComponent.js</code> in the <code>src/Components/Common</code> folder. First let's list out all the props we need to pass to our re-usable slider component:</p>
<ul>
<li><strong>label</strong></li>
<li><strong>min</strong></li>
<li><strong>max</strong></li>
<li><strong>defaultValue</strong></li>
<li><strong>unit</strong></li>
<li><strong>value</strong></li>
<li><strong>steps</strong></li>
<li><strong>amount</strong></li>
<li><strong>onChange</strong></li>
</ul>
<h3 id="heading-slidercomponentjs">SliderComponent.js</h3>
<p>Let's get going. First of all, import the following components from MUI inside the <code>SliderComponent.js</code> file:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> Slider <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/Slider"</span>;
<span class="hljs-keyword">import</span> { Typography } <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material"</span>;
<span class="hljs-keyword">import</span> { Stack } <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/system"</span>;
</code></pre>
<p>We will be using the <a target="_blank" href="https://mui.com/material-ui/react-stack/">Stack component from MUI</a> to stack the components vertically. <code>my</code> is the shorthand for <code>marginY</code> [margin-top &amp; margin-bottom]. We will be using the <code>Typography</code> component from MUI to display the label, unit, and other data. We will be using the <code>Slider</code> component from MUI to display the slider.</p>
<p>Write this small amount of code first, with our props destructured:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> SliderComponent = <span class="hljs-function">(<span class="hljs-params">{
  defaultValue,
  min,
  max,
  label,
  unit,
  onChange,
  amount,
  value,
  steps
}</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Stack</span> <span class="hljs-attr">my</span>=<span class="hljs-string">{1.4}</span>&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">Stack</span>&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> SliderComponent
</code></pre>
<p>We will write this code to display the label, unit, and amount.</p>
<pre><code class="lang-jsx">&lt;Stack gap={<span class="hljs-number">1</span>}&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Typography</span> <span class="hljs-attr">variant</span>=<span class="hljs-string">"subtitle2"</span>&gt;</span>{label}<span class="hljs-tag">&lt;/<span class="hljs-name">Typography</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Typography</span> <span class="hljs-attr">variant</span>=<span class="hljs-string">"h5"</span>&gt;</span>
    {unit} {amount}
  <span class="hljs-tag">&lt;/<span class="hljs-name">Typography</span>&gt;</span></span>
&lt;/Stack&gt;
</code></pre>
<p>We will write this code to display the slider. We will be passing the props to the slider component like this: 👇</p>
<pre><code class="lang-jsx">&lt;Slider
  min={min}
  max={max}
  defaultValue={defaultValue}
  aria-label=<span class="hljs-string">"Default"</span>
  valueLabelDisplay=<span class="hljs-string">"auto"</span>
  onChange={onChange}
  value={value}
  marks
  step={steps}
/&gt;
</code></pre>
<p>We will write this code to display the min and max values of the slider. We will be using the <code>Stack</code> component from MUI to stack the components horizontally. <code>direction="row"</code> is the shorthand for <code>flex-direction: row</code>. <code>justifyContent="space-between"</code> is the shorthand for <code>justify-content: space-between</code>.</p>
<pre><code class="lang-js">&lt;Stack direction=<span class="hljs-string">"row"</span> justifyContent=<span class="hljs-string">"space-between"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Typography</span> <span class="hljs-attr">variant</span>=<span class="hljs-string">"caption"</span> <span class="hljs-attr">color</span>=<span class="hljs-string">"text.secondary"</span>&gt;</span>
    {unit} {min}
  <span class="hljs-tag">&lt;/<span class="hljs-name">Typography</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Typography</span> <span class="hljs-attr">variant</span>=<span class="hljs-string">"caption"</span> <span class="hljs-attr">color</span>=<span class="hljs-string">"text.secondary"</span>&gt;</span>
    {unit} {max}
  <span class="hljs-tag">&lt;/<span class="hljs-name">Typography</span>&gt;</span></span>
&lt;/Stack&gt;
</code></pre>
<p>Good Job so far!</p>
<h2 id="heading-take-a-break">Take a break</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9s9yorz1gwt380tbr7t.png" alt="Take a break" width="1280" height="720" loading="lazy"></p>
<p>Take a break – you deserve it! 🎉</p>
<h2 id="heading-how-to-use-the-usestate-hook">How to Use the useState Hook</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89cx3gxzdl5h7q0okf8f.png" alt="useState Hook" width="1280" height="720" loading="lazy"></p>
<p>We need to use the useState hook from React in our project. But before that, we need to understand what this hook is and why we need to use it.</p>
<p>The useState hook is a built-in React function that allows you to add state to functional components. It returns an <code>array</code> containing two elements: the current state value and a function to update that value. The general syntax of the useState hook is as follows:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> [state, setState] = useState(initialState);
</code></pre>
<p>Here's what's going on: 👇</p>
<ul>
<li><code>state</code>: The name of the constant or variable that will store the state.</li>
<li><code>setState</code>: A function to update the state.</li>
<li><code>initialState</code>: The initial value of the state.</li>
</ul>
<h3 id="heading-example-of-the-usestate-hook">Example of the useState hook</h3>
<p>We will create a toggle button that changes its text between "ON" and "OFF" when clicked.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">const</span> ToggleButton = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [isOn, setIsOn] = useState(<span class="hljs-literal">false</span>);

  <span class="hljs-keyword">const</span> toggle = <span class="hljs-function">() =&gt;</span> setIsOn(!isOn)

  <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{toggle}</span>&gt;</span>{isOn ? 'ON' : 'OFF'}<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ToggleButton;
</code></pre>
<p>Here, we initialize the <code>isOn</code> state with an initial value of <code>false</code>. The <code>toggle</code> function updates the <code>isOn</code> state to its opposite value when the user clicks the button. We use a <code>ternary operator</code> to render the text inside the button based on the current value of <code>isOn</code>.</p>
<h3 id="heading-appjs-2">App.js</h3>
<p>Now lets come back to our project. First, come over to the <code>App.js</code> file and import the <code>useState</code> hook from React.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
</code></pre>
<p>Next up, we will declare a state to store the value of the sliders using the <code>useState</code> hook. We will be passing the initial value of the state as <code>{}</code> inside the <code>useState</code> hook, because we are storing our data as an object.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [data, setData] = useState({})

  <span class="hljs-comment">// other codes are here</span>
}
</code></pre>
<p>We're using the useState hook to create a new state variable called <code>data</code> and a function called <code>setData</code> that we can use to update the state.</p>
<p>Next up, we will pass these values as default values for our slider component.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [data, setData] = useState({
    <span class="hljs-attr">homeValue</span>: <span class="hljs-number">3000</span>,
    <span class="hljs-attr">downPayment</span>: <span class="hljs-number">3000</span> * <span class="hljs-number">0.2</span>,
    <span class="hljs-attr">loanAmount</span>: <span class="hljs-number">3000</span> * <span class="hljs-number">0.8</span>,
    <span class="hljs-attr">loanTerm</span>: <span class="hljs-number">5</span>,
    <span class="hljs-attr">interestRate</span>: <span class="hljs-number">5</span>,
  })

  <span class="hljs-comment">// other codes are here</span>
}
</code></pre>
<p>Then, we will be passing the <code>data</code> and <code>setData</code> state as a prop to the <code>SliderSelect</code> component like this: 👇</p>
<pre><code class="lang-js">&lt;div className=<span class="hljs-string">"App"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Container</span> <span class="hljs-attr">maxWidth</span>=<span class="hljs-string">"xl"</span> <span class="hljs-attr">sx</span>=<span class="hljs-string">{{marginTop:4}}</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">container</span> <span class="hljs-attr">spacing</span>=<span class="hljs-string">{5}</span> <span class="hljs-attr">alignItems</span>=<span class="hljs-string">"center"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">item</span> <span class="hljs-attr">xs</span>=<span class="hljs-string">{12}</span> <span class="hljs-attr">md</span>=<span class="hljs-string">{6}</span>&gt;</span>

        {/* this is where we write the code  👇 */}
        <span class="hljs-tag">&lt;<span class="hljs-name">SliderSelect</span> <span class="hljs-attr">data</span>=<span class="hljs-string">{data}</span> <span class="hljs-attr">setData</span>=<span class="hljs-string">{setData}/</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">TenureSelect</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">item</span> <span class="hljs-attr">xs</span>=<span class="hljs-string">{12}</span> <span class="hljs-attr">md</span>=<span class="hljs-string">{6}</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Result</span>/&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Container</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<h2 id="heading-how-to-build-the-sliderselect-component">How to Build the SliderSelect Component</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/001ovrcapajjl5i480dn.png" alt="SliderSelect.js component" width="1280" height="720" loading="lazy"></p>
<p>So now that we have our re-usable <code>SliderComponent</code> ready, let's use it in our <code>SliderSelect.js</code> component. First of all, import the <code>SliderComponent</code> component from the <code>Common</code> folder.</p>
<h3 id="heading-sliderselectjs">SliderSelect.js</h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> SliderComponent <span class="hljs-keyword">from</span> <span class="hljs-string">"./Common/SliderComponent"</span>;
</code></pre>
<p>Next up, we will destructure our props that we are receiveing from <code>App.js</code>. And also, we'll create a variable called <code>bank_limit</code> and assign it a value of <code>10000</code>. This represents the maximum amount of money a person can borrow from our bank.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> SliderComponent <span class="hljs-keyword">from</span> <span class="hljs-string">"./Common/SliderComponent"</span>;

<span class="hljs-keyword">const</span> SliderSelect = <span class="hljs-function">(<span class="hljs-params">{ data, setData }</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> bank_limit = <span class="hljs-number">10000</span>;
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> SliderSelect;
</code></pre>
<p>Next up, we will use our <code>SliderComponent</code> to display the slider named <code>Home Value</code>. Here we will pass the props like this to the <code>SliderComponent</code> component.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> SliderSelect = <span class="hljs-function">(<span class="hljs-params">{ data, setData }</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> bank_limit = <span class="hljs-number">10000</span>;
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">SliderComponent</span>
        <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e,</span> <span class="hljs-attr">value</span>) =&gt;</span> {
          setData({
            ...data,
            homeValue: value.toFixed(0),
            downPayment: (0.2 * value).toFixed(0),
            loanAmount: (0.8 * value).toFixed(0),
          });
        }}
        defaultValue={data.homeValue}
        min={1000}
        max={bank_limit}
        steps={100}
        unit="$"
        amount={data.homeValue}
        label="Home Value"
        value={data.homeValue}
      /&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
</code></pre>
<p>Here's the result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tc8ymi79urkugw7kd4ci.png" alt="Home Value Slider" width="1005" height="374" loading="lazy"></p>
<p>In the same way, we will create the sliders for <code>Down Payment</code> and <code>Loan Amount</code> like this: 👇</p>
<pre><code class="lang-js">  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {/* other codes are here */}

      <span class="hljs-tag">&lt;<span class="hljs-name">SliderComponent</span>
        <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e,</span> <span class="hljs-attr">value</span>) =&gt;</span>
          setData({
            ...data,
            downPayment: value.toFixed(0),
            loanAmount: (data.homeValue - value).toFixed(0),
          })
        }
        defaultValue={data.downPayment}
        min={0}
        max={data.homeValue}
        steps={100}
        unit="$"
        amount={data.downPayment}
        label="Down Payment"
        value={data.downPayment}
      /&gt;

      <span class="hljs-tag">&lt;<span class="hljs-name">SliderComponent</span>
        <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e,</span> <span class="hljs-attr">value</span>) =&gt;</span>
          setData({
            ...data,
            loanAmount: value.toFixed(0),
            downPayment: (data.homeValue - value).toFixed(0),
          })
        }
        defaultValue={data.loanAmount}
        min={0}
        max={data.homeValue}
        steps={100}
        unit="$"
        amount={data.loanAmount}
        label="Loan Amount"
        value={data.loanAmount}
      /&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
</code></pre>
<p>Again, here's the result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nvhfgzpf1aq02p3kwdqz.png" alt="the result so far" width="1104" height="549" loading="lazy"></p>
<p>Finally we will create the slider for our <code>Interest Rate</code>. You can follow along here: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {/* other codes are here */}

      <span class="hljs-tag">&lt;<span class="hljs-name">SliderComponent</span>
        <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e,</span> <span class="hljs-attr">value</span>) =&gt;</span>
          setData({
            ...data,
            interestRate: value,
          })
        }
        defaultValue={data.interestRate}
        min={2}
        max={18}
        steps={0.5}
        unit="%"
        amount={data.interestRate}
        label="Interest Rate"
        value={data.interestRate}
      /&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
</code></pre>
<p>Here's the result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snlpyvu2qfqzt81ecbvo.png" alt="Interest Rate slider" width="1289" height="659" loading="lazy"></p>
<h2 id="heading-how-to-build-the-tenureselect-component">How to Build the TenureSelect Component</h2>
<p>Next up, we will build the <code>TenureSelect</code> component. This component will be used to select the tenure of the loan. It will look like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70arqood9dwqj9j46apk.png" alt="Image description" width="993" height="332" loading="lazy"></p>
<h3 id="heading-appjs-3">App.js</h3>
<p>First of all, pass the <code>data</code> and <code>setData</code> state as a prop to the <code>TenureSelect</code> component like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Container</span> <span class="hljs-attr">maxWidth</span>=<span class="hljs-string">"xl"</span> <span class="hljs-attr">sx</span>=<span class="hljs-string">{{marginTop:4}}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">container</span> <span class="hljs-attr">spacing</span>=<span class="hljs-string">{5}</span> <span class="hljs-attr">alignItems</span>=<span class="hljs-string">"center"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">item</span> <span class="hljs-attr">xs</span>=<span class="hljs-string">{12}</span> <span class="hljs-attr">md</span>=<span class="hljs-string">{6}</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">SliderSelect</span> <span class="hljs-attr">data</span>=<span class="hljs-string">{data}</span> <span class="hljs-attr">setData</span>=<span class="hljs-string">{setData}</span> /&gt;</span>

          {/* this is where we write the code  👇 */}
          <span class="hljs-tag">&lt;<span class="hljs-name">TenureSelect</span> <span class="hljs-attr">data</span>=<span class="hljs-string">{data}</span> <span class="hljs-attr">setData</span>=<span class="hljs-string">{setData}/</span>&gt;</span>

        <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">item</span> <span class="hljs-attr">xs</span>=<span class="hljs-string">{12}</span> <span class="hljs-attr">md</span>=<span class="hljs-string">{6}</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Result</span> <span class="hljs-attr">data</span>=<span class="hljs-string">{data}/</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Container</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);
</code></pre>
<h3 id="heading-tenureselectjs">TenureSelect.js</h3>
<p>Then, import these required components from the <code>MUI</code> library:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> InputLabel <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/InputLabel"</span>;
<span class="hljs-keyword">import</span> MenuItem <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/MenuItem"</span>;
<span class="hljs-keyword">import</span> FormControl <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/FormControl"</span>;
<span class="hljs-keyword">import</span> Select <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material/Select"</span>;
</code></pre>
<p>Then destructure the props that we are receiving from <code>App.js</code>. Also create a function named <code>handleChange</code> that will be used to set the <code>tenure</code> state, like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> TenureSelect = <span class="hljs-function">(<span class="hljs-params">{ data, setData }</span>) =&gt;</span> {

  <span class="hljs-keyword">const</span> handleChange = <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
    setData({...data, <span class="hljs-attr">loanTerm</span>: event.target.value});
  };

  <span class="hljs-keyword">return</span> ()
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> TenureSelect;
</code></pre>
<p>Next up, we will build the <code>TenureSelect</code> component. It will look like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">FormControl</span> <span class="hljs-attr">fullWidth</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">InputLabel</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"demo-simple-select-label"</span>&gt;</span>Tenure<span class="hljs-tag">&lt;/<span class="hljs-name">InputLabel</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Select</span>
      <span class="hljs-attr">labelId</span>=<span class="hljs-string">"demo-simple-select-label"</span>
      <span class="hljs-attr">id</span>=<span class="hljs-string">"demo-simple-select"</span>
      <span class="hljs-attr">value</span>=<span class="hljs-string">{data.loanTerm}</span>
      <span class="hljs-attr">label</span>=<span class="hljs-string">"Tenure"</span>
      <span class="hljs-attr">defaultValue</span>=<span class="hljs-string">{5}</span>
      <span class="hljs-attr">onChange</span>=<span class="hljs-string">{handleChange}</span>
    &gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{5}</span>&gt;</span>5 years<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{10}</span>&gt;</span>10 years<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{15}</span>&gt;</span>15 years<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{20}</span>&gt;</span>20 years<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">MenuItem</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{25}</span>&gt;</span>25 years<span class="hljs-tag">&lt;/<span class="hljs-name">MenuItem</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Select</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">FormControl</span>&gt;</span></span>
);
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fl0fsfk2lv9dnh588eyh.png" alt="The result so far" width="934" height="401" loading="lazy"></p>
<h2 id="heading-how-to-build-the-result-component">How to Build the Result Component</h2>
<p>Finally we will build the <code>Result</code> component. This component will be used to display the loan installment per month and the pie chart. It will look like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f5vgfcsk6aj6yseqvi1.png" alt="Result component" width="822" height="424" loading="lazy"></p>
<h3 id="heading-appjs-4">App.js</h3>
<p>First of all, pass the <code>data</code> state as a prop to the <code>Result</code> component like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Container</span> <span class="hljs-attr">maxWidth</span>=<span class="hljs-string">"xl"</span> <span class="hljs-attr">sx</span>=<span class="hljs-string">{{marginTop:4}}</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">container</span> <span class="hljs-attr">spacing</span>=<span class="hljs-string">{5}</span> <span class="hljs-attr">alignItems</span>=<span class="hljs-string">"center"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">item</span> <span class="hljs-attr">xs</span>=<span class="hljs-string">{12}</span> <span class="hljs-attr">md</span>=<span class="hljs-string">{6}</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">SliderSelect</span> <span class="hljs-attr">data</span>=<span class="hljs-string">{data}</span> <span class="hljs-attr">setData</span>=<span class="hljs-string">{setData}</span> /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">TenureSelect</span> <span class="hljs-attr">data</span>=<span class="hljs-string">{data}</span> <span class="hljs-attr">setData</span>=<span class="hljs-string">{setData}/</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Grid</span> <span class="hljs-attr">item</span> <span class="hljs-attr">xs</span>=<span class="hljs-string">{12}</span> <span class="hljs-attr">md</span>=<span class="hljs-string">{6}</span>&gt;</span>

          {/* this is where we write the code  👇 */}
          <span class="hljs-tag">&lt;<span class="hljs-name">Result</span> <span class="hljs-attr">data</span>=<span class="hljs-string">{data}/</span>&gt;</span>

        <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Grid</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Container</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);
</code></pre>
<h3 id="heading-resultjs">Result.js</h3>
<p>Next up, import the required components like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { Stack, Typography } <span class="hljs-keyword">from</span> <span class="hljs-string">"@mui/material"</span>;
<span class="hljs-keyword">import</span> { Chart <span class="hljs-keyword">as</span> ChartJS, ArcElement, Tooltip, Legend } <span class="hljs-keyword">from</span> <span class="hljs-string">"chart.js"</span>;
<span class="hljs-keyword">import</span> { Pie } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-chartjs-2"</span>;

ChartJS.register(ArcElement, Tooltip, Legend);
</code></pre>
<p>Then destruct the <code>data</code> state that we are receiving from <code>App.js</code> like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Result = <span class="hljs-function">(<span class="hljs-params">{ data }</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> { homeValue, loanAmount, loanTerm, interestRate } = data;
  <span class="hljs-keyword">return</span> ();
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Result;
</code></pre>
<p>Next up we will write all of these things that will help us do the calculation: 👇</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> totalLoanMonths = loanTerm * <span class="hljs-number">12</span>;
  <span class="hljs-keyword">const</span> interestPerMonth = interestRate / <span class="hljs-number">100</span> / <span class="hljs-number">12</span>;
  <span class="hljs-keyword">const</span> monthlyPayment =
    (loanAmount *
      interestPerMonth *
      (<span class="hljs-number">1</span> + interestPerMonth) ** totalLoanMonths) /
    ((<span class="hljs-number">1</span> + interestPerMonth) ** totalLoanMonths - <span class="hljs-number">1</span>);

  <span class="hljs-keyword">const</span> totalInterestGenerated = monthlyPayment * totalLoanMonths - loanAmount;
</code></pre>
<p>Then we need this variable to store all the data for our pie chart, like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> pieChartData = {
  <span class="hljs-attr">labels</span>: [<span class="hljs-string">"Principle"</span>, <span class="hljs-string">"Interest"</span>],
  <span class="hljs-attr">datasets</span>: [
    {
      <span class="hljs-attr">label</span>: <span class="hljs-string">"Ratio of Principle and Interest"</span>,
      <span class="hljs-attr">data</span>: [homeValue, totalInterestGenerated],
      <span class="hljs-attr">backgroundColor</span>: [<span class="hljs-string">"rgba(255, 99, 132, 0.2)"</span>, <span class="hljs-string">"rgba(54, 162, 235, 0.2)"</span>],
      <span class="hljs-attr">borderColor</span>: [<span class="hljs-string">"rgba(255, 99, 132, 1)"</span>, <span class="hljs-string">"rgba(54, 162, 235, 1)"</span>],
      <span class="hljs-attr">borderWidth</span>: <span class="hljs-number">1</span>,
    },
  ],
};
</code></pre>
<p>Finally, we will build the <code>Result</code> component. It will look like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Stack</span> <span class="hljs-attr">gap</span>=<span class="hljs-string">{3}</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Typography</span> <span class="hljs-attr">textAlign</span>=<span class="hljs-string">"center"</span> <span class="hljs-attr">variant</span>=<span class="hljs-string">"h5"</span>&gt;</span>
      Monthly Payment: $ {monthlyPayment.toFixed(2)}
    <span class="hljs-tag">&lt;/<span class="hljs-name">Typography</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">Stack</span> <span class="hljs-attr">direction</span>=<span class="hljs-string">"row"</span> <span class="hljs-attr">justifyContent</span>=<span class="hljs-string">"center"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Pie</span> <span class="hljs-attr">data</span>=<span class="hljs-string">{pieChartData}</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Stack</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Stack</span>&gt;</span></span>
);
</code></pre>
<p>Here's the final result: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqs2st1o5fhlpoqnpqol.png" alt="The result so far" width="738" height="414" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7w7p11dm81ggzxd6a1t.png" alt="Congratulations" width="1280" height="720" loading="lazy"></p>
<p>Congratulations for reading until the end! Now you can confidently and efficiently use React JS and Material UI to build cool projects.</p>
<p>You have also learned how to use React's useState hook and how to handle props. I hope you enjoyed this tutorial.</p>
<h2 id="heading-mentorship-program">Mentorship Program</h2>
<p>If you are interested in learning more about React JS and web development, I am offering a mentorship program. You can check out the details here 👉 <a target="_blank" href="https://www.mentorlabs.academy/">Mentor Labs Academy</a></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5oyscu7l16tr636ekqj.png" alt="Mentorship Program" width="1072" height="467" loading="lazy"></p>
<h2 id="heading-my-social-media-links">My Social Media Links</h2>
<ul>
<li><a target="_blank" href="https://www.linkedin.com/in/joyshaheb/">LinkedIn/ JoyShaheb</a></li>
<li><a target="_blank" href="https://www.youtube.com/c/joyshaheb">YouTube / JoyShaheb</a></li>
<li><a target="_blank" href="https://twitter.com/JoyShaheb">Twitter / JoyShaheb</a></li>
<li><a target="_blank" href="https://www.instagram.com/joyshaheb/">Instagram / JoyShaheb</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn CRUD Operations in JavaScript by Building TODO APP ]]>
                </title>
                <description>
                    <![CDATA[ Today we're gonna learn how to do CRUD Operations in JavaScript by making a Todo App. Let's get started 🔥 This is the app we're making today: Live preview GitHub Repository You can watch this tutorial on YouTube as well if you like 🎥 https://you... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-crud-operations-in-javascript-by-building-todo-app/</link>
                <guid isPermaLink="false">66b2095df31aa965000e588c</guid>
                
                    <category>
                        <![CDATA[ crud ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Wed, 13 Apr 2022 17:57:32 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/04/Frame-12--2-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're gonna learn how to do CRUD Operations in JavaScript by making a Todo App. Let's get started 🔥</p>
<p>This is the app we're making today:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ut6o5hbsrzb5eccm72v.png" alt="App that we're making today" width="611" height="848" loading="lazy"></p>
<ul>
<li><a target="_blank" href="https://crud-application-al9am9v2v-joyshaheb.vercel.app/">Live preview</a></li>
<li><a target="_blank" href="https://github.com/JoyShaheb/CRUD-Application">GitHub Repository</a></li>
</ul>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like">You can watch this tutorial on YouTube as well if you like 🎥</h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/fL9cts8ykbU" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-table-of-contents">Table of Contents</h1>
<ul>
<li>What is CRUD?</li>
<li>Understanding CRUD Principles</li>
<li>How to Make a To-Do App using CRUD Operations</li>
</ul>
<h2 id="heading-what-is-crud">What is CRUD?</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agloiqjwk11qr7mu39y4.png" alt="Image description" width="1280" height="613" loading="lazy"></p>
<p>CRUD stands for -</p>
<ul>
<li>C: Create</li>
<li>R: Read</li>
<li>U: Update</li>
<li>D: Delete</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3md2xtw76u0y1fr8polm.png" alt="CRUD Fullform" width="1280" height="720" loading="lazy"></p>
<p>CRUD is a type of mechanism that allows you to create data, read data, edit it, and delete those data. In our case, we're gonna make a Todo app, so we will have 4 options to create tasks, read tasks, update tasks, or delete tasks.</p>
<h2 id="heading-understanding-crud-principles">Understanding CRUD Principles</h2>
<p>Before starting the tutorial, first, let's understand the CRUD principles. For that, let's create a very very simple Social Media Application.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8cr1ueopcx4bfgz7j8ov.gif" alt="Social Media App Project" width="1036" height="720" loading="lazy"></p>
<h2 id="heading-setup">Setup</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9o3h86jd7md2bo54n4cd.png" alt="Project Setup" width="1280" height="613" loading="lazy"></p>
<p>For this project, we will be following these steps below:</p>
<ul>
<li>Create 3 files named index.html, style.css, and main.js</li>
<li>Link the JavaScript and CSS file to index.html</li>
<li>Start your live server</li>
</ul>
<h3 id="heading-html">HTML</h3>
<p>Inside the body tag, create a div with a class name <code>.container</code>. There, we will have 2 sections, <code>.left</code> and <code>.right</code> 👇</p>
<pre><code class="lang-HTML"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Social Media App<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"left"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"right"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre>
<p>On the left side, we will create our posts. On the right side, we can see, update, and delete our posts. Now, create a form inside the .left div tag 👇</p>
<pre><code class="lang-HTML"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"left"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"form"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"post"</span>&gt;</span> Write your post here<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"post"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">cols</span>=<span class="hljs-string">"30"</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">"10"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"msg"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Post<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Write this code inside the HTML so that we can display our post on the right side 👇</p>
<pre><code class="lang-HTML"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"right"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Your posts here<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"posts"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Next, we'll insert the font-awesome CDN inside the head tag to use its fonts in our project 👇</p>
<pre><code class="lang-HTML"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"</span> /&gt;</span>
</code></pre>
<p>Now, we're gonna make some sample posts with delete and edit icons. Write this code inside the div with the id #posts: 👇</p>
<pre><code class="lang-HTML"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"posts"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello world post 1<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"options"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-edit"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-trash-alt"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> &gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Hello world post 2<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"options"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-edit"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-trash-alt"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>The result so far looks like this:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ra3w4flp2et2wsf94m2.png" alt="HTML Markup result" width="465" height="681" loading="lazy"></p>
<h3 id="heading-css">CSS</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djlwav0yj8w5vld3ux5z.png" alt="Adding CSS for project 1" width="1280" height="613" loading="lazy"></p>
<p>Let's keep it simple. Write these styles inside your stylesheet: 👇</p>
<pre><code class="lang-CSS"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: sans-serif;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> <span class="hljs-number">50px</span>;
}

<span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">50px</span>;
}

<span class="hljs-selector-id">#posts</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">400px</span>;
}

<span class="hljs-selector-tag">i</span> {
  <span class="hljs-attribute">cursor</span>: pointer;
}
</code></pre>
<p>Now, write these styles for the post div and option icons: 👇</p>
<pre><code class="lang-CSS"><span class="hljs-selector-id">#posts</span> <span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">justify-content</span>: space-between;
}

<span class="hljs-selector-class">.options</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">25px</span>;
}

<span class="hljs-selector-id">#msg</span> {
  <span class="hljs-attribute">color</span>: red;
}
</code></pre>
<p>The results so far look like this:👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dttu77ecfpd235byt4pi.png" alt="The result after adding the css part project 1" width="730" height="435" loading="lazy"></p>
<h3 id="heading-javascript-part">JavaScript Part</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nl1o5q5kcymkfyrsme8f.png" alt="Starting the javascript part" width="1280" height="613" loading="lazy"></p>
<p>According to this flow chart, we will go forward with the project. Don't worry, I'll explain everything along the way. 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlsw0253j7st1ictip03.png" alt="flow chart" width="847" height="908" loading="lazy"></p>
<h4 id="heading-form-validation">Form Validation</h4>
<p>First, let's target all the ID selectors from the HTML in JavaScript. Like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> form = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"form"</span>);
<span class="hljs-keyword">let</span> input = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"input"</span>);
<span class="hljs-keyword">let</span> msg = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"msg"</span>);
<span class="hljs-keyword">let</span> posts = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"posts"</span>);
</code></pre>
<p>Then, build a submit event listener for the form so that it can prevent the default behaviour of our App. At the same time, we will create a function named <code>formValidation</code>. 👇</p>
<pre><code class="lang-js">form.addEventListener(<span class="hljs-string">"submit"</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  e.preventDefault();
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"button clicked"</span>);

  formValidation();
});

<span class="hljs-keyword">let</span> formValidation = <span class="hljs-function">() =&gt;</span> {};
</code></pre>
<p>Now, we're gonna make an if else statement inside our <code>formValidation</code> function. This will help us prevent users from submitting blank input fields. 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> formValidation = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">if</span> (input.value === <span class="hljs-string">""</span>) {
    msg.innerHTML = <span class="hljs-string">"Post cannot be blank"</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"failure"</span>);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"successs"</span>);
    msg.innerHTML = <span class="hljs-string">""</span>;
  }
};
</code></pre>
<p>Here's the result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sb8faq21j5dzy9vlswj.gif" alt="7sb8faq21j5dzy9vlswj" width="1036" height="720" loading="lazy"></p>
<p>As you can see, a message will also show up if the user tries to submit the form blank.</p>
<h4 id="heading-how-to-accept-data-from-input-fields">How to accept data from input fields</h4>
<p>Whatever data we get from the input fields, we will store them in an object. Let's create an object named <code>data</code>. And, create a function named <code>acceptData</code>: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> data = {};

<span class="hljs-keyword">let</span> acceptData = <span class="hljs-function">() =&gt;</span> {};
</code></pre>
<p>The main idea is that, using the function, we collect data from the inputs and store them in our object named <code>data</code>. Now let's finish building our <code>acceptData</code> function.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> acceptData = <span class="hljs-function">() =&gt;</span> {
  data[<span class="hljs-string">"text"</span>] = input.value;
  <span class="hljs-built_in">console</span>.log(data);
};
</code></pre>
<p>Also, we need the <code>acceptData</code> function to work when the user clicks the submit button. For that, we will fire this function in the else statement of our <code>formValidation</code> function. 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> formValidation = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">if</span> (input.value === <span class="hljs-string">""</span>) {
    <span class="hljs-comment">// Other codes are here</span>
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// Other codes are here</span>
    acceptData();
  }
};
</code></pre>
<p>When we input data and submit the form, on the console we can see an object holding our user's input values. Like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jje41w8b70hpdgyonpqj.png" alt="result so far on the console" width="1134" height="262" loading="lazy"></p>
<h4 id="heading-how-to-create-posts-using-javascript-template-literals">How to create posts using JavaScript template literals</h4>
<p>In order to post our input data on the right side, we need to create a div element and append it to the posts div. First, let's create a function and write these lines: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> createPost = <span class="hljs-function">() =&gt;</span> {
  posts.innerHTML += <span class="hljs-string">``</span>;
};
</code></pre>
<p>The backticks are template literals. It will work as a template for us. Here, we need 3 things: a parent div, the input itself, and the options div which carries the edit and delete icons. Let's finish our function 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> createPost = <span class="hljs-function">() =&gt;</span> {
  posts.innerHTML += <span class="hljs-string">`
  &lt;div&gt;
    &lt;p&gt;<span class="hljs-subst">${data.text}</span>&lt;/p&gt;
    &lt;span class="options"&gt;
      &lt;i onClick="editPost(this)" class="fas fa-edit"&gt;&lt;/i&gt;
      &lt;i onClick="deletePost(this)" class="fas fa-trash-alt"&gt;&lt;/i&gt;
    &lt;/span&gt;
  &lt;/div&gt;
  `</span>;
  input.value = <span class="hljs-string">""</span>;
};
</code></pre>
<p>In our <code>acceptdata</code> function, we will fire our <code>createPost</code> function. Like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> acceptData = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-comment">// Other codes are here</span>

  createPost();
};
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8cr1ueopcx4bfgz7j8ov.gif" alt="Result so far" width="1036" height="720" loading="lazy"></p>
<p>So far so good guys, we're almost done with project 1.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyrijoc70cy0sebiu1a5.png" alt="so far so good " width="1280" height="613" loading="lazy"></p>
<h4 id="heading-how-to-delete-a-post">How to delete a post</h4>
<p>In order to delete a post, first of all, let's create a function inside our javascript file:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> deletePost = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {};
</code></pre>
<p>Next up, we fire this <code>deletePost</code> function inside all of our delete icons using an onClick attribute. You'll write these lines in HTML and on the template literal. 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">"deletePost(this)"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-trash-alt"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
</code></pre>
<p>The <code>this</code> keyword will refer to the element that fired the event. in our case, the <code>this</code> refers to the delete button.</p>
<p>Look carefully, the parent of the delete button is the span with class name options. The parent of the span is the div. So, we write <code>parentElement</code> twice so that we can jump from the delete icon to the div and target it directly to remove it. </p>
<p>Let's finish our function. 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> deletePost = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  e.parentElement.parentElement.remove();
};
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9upccmzs4zszg1nfrdf.gif" alt="deleting a post result" width="1120" height="420" loading="lazy"></p>
<h4 id="heading-how-to-edit-a-post">How to edit a post</h4>
<p>In order to edit a post, first of all, let's create a function inside our JavaScript file:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> editPost = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {};
</code></pre>
<p>Next up, we fire this <code>editPost</code> function inside all of our edit icons using an onClick attribute. You'll write these lines in HTML and on the template literal. 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">"editPost(this)"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-edit"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
</code></pre>
<p>The <code>this</code> keyword will refer to the element that fired the event. In our case, the <code>this</code> refers to the edit button.</p>
<p>Look carefully, the parent of the edit button is the span with class name options. The parent of the span is the div. So, we write <code>parentElement</code> twice so that we can jump from the edit icon to the div and target it directly to remove it.</p>
<p>Then, whatever data is inside the post, we bring it back on the input field to edit it.</p>
<p>Let's finish our function. 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> editPost = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  input.value = e.parentElement.previousElementSibling.innerHTML;
  e.parentElement.parentElement.remove();
};
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uqymvra7ejzi59ekpscd.gif" alt="Editing a post result" width="1120" height="420" loading="lazy"></p>
<h2 id="heading-take-a-break">Take a Break!</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rywq5vr0yz4e0hoosfn9.png" alt="Take a Break" width="1280" height="613" loading="lazy"></p>
<p>Congratulations everyone for completing project 1. Now, take a small break!</p>
<h1 id="heading-how-to-make-a-to-do-app-using-crud-operations">How to Make a To-Do App using CRUD Operations</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbjnnnfvbqmmm8x3mdfw.png" alt="Let's make a todo app" width="1280" height="613" loading="lazy"></p>
<p>Let's start making project 2, which is a To-Do App.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pg9yjnp9s7176xomxlnj.png" alt="Project setup" width="1280" height="546" loading="lazy"></p>
<p>For this project, we will be following these steps below:</p>
<ul>
<li>Create 3 files named index.html, style.css, and main.js</li>
<li>Link the JavaScript and CSS files to index.html</li>
<li>Start our live server</li>
</ul>
<h3 id="heading-html-1">HTML</h3>
<p>Write this starter code inside the HTML file: 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"app"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h4</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"mb-3"</span>&gt;</span>TODO App<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"addNew"</span> <span class="hljs-attr">data-bs-toggle</span>=<span class="hljs-string">"modal"</span> <span class="hljs-attr">data-bs-target</span>=<span class="hljs-string">"#form"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Add New Task<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-plus"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>The div with an id <code>addNew</code> is the button that will open the modal. The span will be displayed on the button. The <code>i</code> is the icon from font-awesome.</p>
<p>We're going to use bootstrap to make our modal. We'll use the modal to add new tasks. For that, add the bootstrap CDN link inside the head tag. 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span>
  <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"</span>
  <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span>
  <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"</span>
  <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>
/&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span>
  <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"</span>
  <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"</span>
  <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>
&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>To see the created tasks, we'll use a div with an id tasks, inside the div with the classname app. 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-center my-3"</span>&gt;</span>Tasks<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"tasks"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Insert the font-awesome CDN inside the head tag to use fonts in our project 👇</p>
<pre><code class="lang-HTML"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"</span> /&gt;</span>
</code></pre>
<p>Copy and paste the code below which are from the bootstrap modal. It carries a form with 3 input fields and a submit button. If you want then you can search Bootstrap's website by writing 'modal' in the search bar.</p>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!-- Modal --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">form</span>
  <span class="hljs-attr">class</span>=<span class="hljs-string">"modal fade"</span>
  <span class="hljs-attr">id</span>=<span class="hljs-string">"form"</span>
  <span class="hljs-attr">tabindex</span>=<span class="hljs-string">"-1"</span>
  <span class="hljs-attr">aria-labelledby</span>=<span class="hljs-string">"exampleModalLabel"</span>
  <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>
&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"modal-dialog"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"modal-content"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"modal-header"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"modal-title"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"exampleModalLabel"</span>&gt;</span>Add New Task<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
          <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span>
          <span class="hljs-attr">class</span>=<span class="hljs-string">"btn-close"</span>
          <span class="hljs-attr">data-bs-dismiss</span>=<span class="hljs-string">"modal"</span>
          <span class="hljs-attr">aria-label</span>=<span class="hljs-string">"Close"</span>
        &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"modal-body"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Task Title<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">""</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"textInput"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"msg"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Due Date<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"date"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">""</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"dateInput"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Description<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span>
          <span class="hljs-attr">name</span>=<span class="hljs-string">""</span>
          <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span>
          <span class="hljs-attr">id</span>=<span class="hljs-string">"textarea"</span>
          <span class="hljs-attr">cols</span>=<span class="hljs-string">"30"</span>
          <span class="hljs-attr">rows</span>=<span class="hljs-string">"5"</span>
        &gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"modal-footer"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-secondary"</span> <span class="hljs-attr">data-bs-dismiss</span>=<span class="hljs-string">"modal"</span>&gt;</span>
          Close
        <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"add"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary"</span>&gt;</span>Add<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eazu2i62bilebhgbrclr.png" alt="Html file setup" width="1058" height="190" loading="lazy"></p>
<p>We're done with the HTML file setup. Let's start the CSS.</p>
<h3 id="heading-css-1">CSS</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqldzsv7vcq7ed4sbptj.png" alt="Adding the css part" width="1280" height="613" loading="lazy"></p>
<p>Add these styles in the body so that we can keep our app at the exact center of the screen.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: sans-serif;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#e5e5e5</span>;
  <span class="hljs-attribute">overflow</span>: hidden;
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>Let's style the div with the classname app. 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.app</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#fff</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">300px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">500px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">5px</span> solid <span class="hljs-number">#abcea1</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">15px</span>;
}
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuunx9tkgvw5uxqq05ab.png" alt="App styles" width="502" height="724" loading="lazy"></p>
<p>Now, let's style the button with the id <code>addNew</code>. 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-id">#addNew</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: space-between;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-built_in">rgba</span>(<span class="hljs-number">171</span>, <span class="hljs-number">206</span>, <span class="hljs-number">161</span>, <span class="hljs-number">0.35</span>);
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">5px</span> <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
  <span class="hljs-attribute">cursor</span>: pointer;
}
<span class="hljs-selector-class">.fa-plus</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#abcea1</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">3px</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">3px</span>;
}
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mt353dpjsczqtf8msb71.png" alt="Add new task Button" width="455" height="715" loading="lazy"></p>
<p>If you click on the button, the modal pops up like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfmc7fyzdef7nnmxf7ap.gif" alt="Modal poping" width="792" height="780" loading="lazy"></p>
<h3 id="heading-add-the-js">Add the JS</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xv4u6j0gafmqxwoh6rpm.png" alt="Adding the JavaScript" width="1280" height="555" loading="lazy"></p>
<p>In the JavaScript file, first of all, select all the selectors from the HTML that we need to use. 👇</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> form = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"form"</span>);
<span class="hljs-keyword">let</span> textInput = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"textInput"</span>);
<span class="hljs-keyword">let</span> dateInput = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"dateInput"</span>);
<span class="hljs-keyword">let</span> textarea = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"textarea"</span>);
<span class="hljs-keyword">let</span> msg = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"msg"</span>);
<span class="hljs-keyword">let</span> tasks = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"tasks"</span>);
<span class="hljs-keyword">let</span> add = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"add"</span>);
</code></pre>
<h4 id="heading-form-validations">Form Validations</h4>
<p>We cannot let a user submit blank input fields. So, we need to validate the input fields. 👇</p>
<pre><code class="lang-javascript">form.addEventListener(<span class="hljs-string">"submit"</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  e.preventDefault();
  formValidation();
});

<span class="hljs-keyword">let</span> formValidation = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">if</span> (textInput.value === <span class="hljs-string">""</span>) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"failure"</span>);
    msg.innerHTML = <span class="hljs-string">"Task cannot be blank"</span>;
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"success"</span>);
    msg.innerHTML = <span class="hljs-string">""</span>;
  }
};
</code></pre>
<p>Also, add this line inside the CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-id">#msg</span> {
  <span class="hljs-attribute">color</span>: red;
}
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j0b0aidh5hbc2hxkp378.gif" alt="Image description" width="792" height="780" loading="lazy"></p>
<p>As you can see, the validation is working. The JavaScript code doesn't let the user submit blank input fields, otherwise you're gonna see an error message.</p>
<h4 id="heading-how-to-collect-data-and-use-local-storage">How to collect data and use local storage</h4>
<p>Whatever inputs the user writes, we need to collect them and store them in local storage. </p>
<p>First, we collect the data from the input fields, using the function named <code>acceptData</code> and an array named <code>data</code>. Then we push them inside the local storage like this: 👇</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> data = [];

<span class="hljs-keyword">let</span> acceptData = <span class="hljs-function">() =&gt;</span> {
  data.push({
    <span class="hljs-attr">text</span>: textInput.value,
    <span class="hljs-attr">date</span>: dateInput.value,
    <span class="hljs-attr">description</span>: textarea.value,
  });

  <span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">"data"</span>, <span class="hljs-built_in">JSON</span>.stringify(data));

  <span class="hljs-built_in">console</span>.log(data);
};
</code></pre>
<p>Also note that this will never work unless you invoke the function <code>acceptData</code> inside the else statement of the form validation. Follow along here: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> formValidation = <span class="hljs-function">() =&gt;</span> {

  <span class="hljs-comment">// Other codes are here</span>
   <span class="hljs-keyword">else</span> {

    <span class="hljs-comment">// Other codes are here</span>

    acceptData();
  }
};
</code></pre>
<p>You may have noticed that the modal doesn't close automatically. To solve this, write this small function inside the else statement of the form validation: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> formValidation = <span class="hljs-function">() =&gt;</span> {

  <span class="hljs-comment">// Other codes are here</span>
   <span class="hljs-keyword">else</span> {

    <span class="hljs-comment">// Other codes are here</span>

    acceptData();
    add.setAttribute(<span class="hljs-string">"data-bs-dismiss"</span>, <span class="hljs-string">"modal"</span>);
    add.click();

    (<span class="hljs-function">() =&gt;</span> {
      add.setAttribute(<span class="hljs-string">"data-bs-dismiss"</span>, <span class="hljs-string">""</span>);
    })();
  }
};
</code></pre>
<p>If you open Chrome dev tools, go to the application and open the local storage. You can see this result: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7mku6j28tknry3xqrwg3.png" alt="Local Storage Result" width="1198" height="432" loading="lazy"></p>
<h4 id="heading-how-to-create-new-tasks">How to create new tasks</h4>
<p>In order to create a new task, we need to create a function, use template literals to create the HTML elements, and use a map to push the data collected from the user inside the template. Follow along here: 👇</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> createTasks = <span class="hljs-function">() =&gt;</span> {
  tasks.innerHTML = <span class="hljs-string">""</span>;
  data.map(<span class="hljs-function">(<span class="hljs-params">x, y</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> (tasks.innerHTML += <span class="hljs-string">`
    &lt;div id=<span class="hljs-subst">${y}</span>&gt;
          &lt;span class="fw-bold"&gt;<span class="hljs-subst">${x.text}</span>&lt;/span&gt;
          &lt;span class="small text-secondary"&gt;<span class="hljs-subst">${x.date}</span>&lt;/span&gt;
          &lt;p&gt;<span class="hljs-subst">${x.description}</span>&lt;/p&gt;

          &lt;span class="options"&gt;
            &lt;i onClick= "editTask(this)" data-bs-toggle="modal" data-bs-target="#form" class="fas fa-edit"&gt;&lt;/i&gt;
            &lt;i onClick ="deleteTask(this);createTasks()" class="fas fa-trash-alt"&gt;&lt;/i&gt;
          &lt;/span&gt;
        &lt;/div&gt;
    `</span>);
  });

  resetForm();
};
</code></pre>
<p>Also note that the function will never run unless you invoke it inside the <code>acceptData</code> function, like this: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> acceptData = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-comment">// Other codes are here</span>

  createTasks();
};
</code></pre>
<p>Once we're done collecting and accepting data from the user, we need to clear the input fields. For that we create a function called <code>resetForm</code>. Follow along: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> resetForm = <span class="hljs-function">() =&gt;</span> {
  textInput.value = <span class="hljs-string">""</span>;
  dateInput.value = <span class="hljs-string">""</span>;
  textarea.value = <span class="hljs-string">""</span>;
};
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8li973gq11jlrarl0nln.png" alt="Adding task cards" width="486" height="414" loading="lazy"></p>
<p>As you can see, there's no styles with the card. Let's add some styles: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-id">#tasks</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">1</span>fr;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">14px</span>;
}

<span class="hljs-selector-id">#tasks</span> <span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">border</span>: <span class="hljs-number">3px</span> solid <span class="hljs-number">#abcea1</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#e2eede</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">6px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">5px</span>;
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">4px</span>;
}
</code></pre>
<p>Style the edit and delete buttons with this code: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-id">#tasks</span> <span class="hljs-selector-tag">div</span> <span class="hljs-selector-class">.options</span> {
  <span class="hljs-attribute">justify-self</span>: center;
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">20px</span>;
}

<span class="hljs-selector-id">#tasks</span> <span class="hljs-selector-tag">div</span> <span class="hljs-selector-class">.options</span> <span class="hljs-selector-tag">i</span> {
  <span class="hljs-attribute">cursor</span>: pointer;
}
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axe47wf6jutn330b5scn.png" alt="Styles card templates" width="578" height="452" loading="lazy"></p>
<h4 id="heading-function-to-delete-a-task">Function to delete a task</h4>
<p>Look here carefully, I added 3 lines of code inside the function.</p>
<ul>
<li>The first line will delete the HTML element from the screen,</li>
<li>the second line will remove the targetted Task from the data array,</li>
<li>and the third line will update the local storage with the new data.</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> deleteTask = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  e.parentElement.parentElement.remove();

  data.splice(e.parentElement.parentElement.id, <span class="hljs-number">1</span>);

  <span class="hljs-built_in">localStorage</span>.setItem(<span class="hljs-string">"data"</span>, <span class="hljs-built_in">JSON</span>.stringify(data));

  <span class="hljs-built_in">console</span>.log(data);
};
</code></pre>
<p>Now create a dummy task and try to delete it. The result so far looks like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qv50ozqjp239ldg0g21.gif" alt="Image description" width="792" height="780" loading="lazy"></p>
<h4 id="heading-function-to-edit-tasks">Function to edit tasks</h4>
<p>Look here carefully, I added 5 lines of code inside the function.</p>
<ul>
<li>Line 1 is targetting the task that we selected to edit</li>
<li>Lines 2, 3, and 4, are targetting the values [task, date, description] of the task that we selected to edit</li>
<li>line 5 is running the delete function to remove the selected data both from the local storage, HTML element, and data array.</li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> editTask = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> selectedTask = e.parentElement.parentElement;

  textInput.value = selectedTask.children[<span class="hljs-number">0</span>].innerHTML;
  dateInput.value = selectedTask.children[<span class="hljs-number">1</span>].innerHTML;
  textarea.value = selectedTask.children[<span class="hljs-number">2</span>].innerHTML;

  deleteTask(e);
};
</code></pre>
<p>Now, try to create a dummy task and edit it. The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbxn7c7rs33chuafjca9.gif" alt="Editing a Task" width="792" height="780" loading="lazy"></p>
<h4 id="heading-how-to-get-data-from-local-storage">How to get data from local storage</h4>
<p>If you refresh the page, you'll note that all of your data is gone. In order to solve that issue, we run a IIFE (Immediately invoked function expression) to retrieve the data from local storage. Follow along: 👇</p>
<pre><code class="lang-js">(<span class="hljs-function">() =&gt;</span> {
  data = <span class="hljs-built_in">JSON</span>.parse(<span class="hljs-built_in">localStorage</span>.getItem(<span class="hljs-string">"data"</span>)) || [];
  <span class="hljs-built_in">console</span>.log(data);
  createTasks();
})();
</code></pre>
<p>Now the data will show up even if you refresh the page.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tenojs64uefxutl7ysn.png" alt="Congratulations" width="1280" height="555" loading="lazy"></p>
<p>Congratulations for successfully completing this tutorial. You've learned how to create a todo list application using CRUD operations. Now, you can create your own CRUD application using this tutorial.</p>
<p>Here's your medal for reading until the end. ❤️</p>
<h2 id="heading-suggestions-amp-criticisms-are-highly-appreciated">Suggestions &amp; Criticisms Are Highly Appreciated ❤️</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="1000" height="245" loading="lazy"></p>
<ul>
<li><a target="_blank" href="https://www.linkedin.com/in/joyshaheb/">LinkedIn/ JoyShaheb</a></li>
<li><a target="_blank" href="https://www.youtube.com/c/joyshaheb">YouTube / JoyShaheb</a></li>
<li><a target="_blank" href="https://twitter.com/JoyShaheb">Twitter / JoyShaheb</a></li>
<li><a target="_blank" href="https://www.instagram.com/joyshaheb/">Instagram / JoyShaheb</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ ReactJS Project – Build a Rick and Morty Character Wiki ]]>
                </title>
                <description>
                    <![CDATA[ Today we're gonna practice our React JS skills by building a Rick and Morty Character Wiki. Here's what we'll build today: Here's a [live demo of the project] 👇 https://react-projects-psi.vercel.app/. And here's the GitHub Repository. The topics we... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-js-project-build-a-rick-and-morty-character-wiki/</link>
                <guid isPermaLink="false">66b2096c27569435a9255b03</guid>
                
                    <category>
                        <![CDATA[ projects ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Mon, 22 Nov 2021 19:13:34 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/11/FCC.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're gonna practice our <strong>React JS</strong> skills by building a Rick and Morty Character Wiki.</p>
<p>Here's what we'll build today:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cufjzu9x8i7iuxmttzyf.png" alt="Image description" width="1535" height="921" loading="lazy"></p>
<p>Here's a [live demo of the project] 👇
https://react-projects-psi.vercel.app/.</p>
<p>And <a target="_blank" href="https://github.com/JoyShaheb/React-Projects/tree/main/Level-1/rick-morty-wiki">here's the GitHub Repository</a>.</p>
<p>The topics we'll cover while building this project are:</p>
<ul>
<li>React Hooks (useState, useEffect)</li>
<li>React Components</li>
<li>Fetch API</li>
<li>Bootstrap - for styling</li>
<li>Pagination</li>
<li>Search bar</li>
<li>Data Filtering</li>
<li>Dynamic Routing</li>
</ul>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like">You can watch this tutorial on YouTube as well if you like:</h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/35QCQnohLg8" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-features-of-the-project">Features of the project</h2>
<p>Let's look at a demo of all the features that we will be building during the entire course of this article. </p>
<p>This project is filled with cool features so that you can get the most out of this tutorial and become really good at writing ReactJS code.</p>
<h3 id="heading-search-bar">Search bar</h3>
<p>We will be building this cool search bar so that we can search for our favourite characters.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oamlwwlpdi12mxyo5fyo.gif" alt="Image description" width="1484" height="928" loading="lazy"></p>
<h3 id="heading-pagination">Pagination</h3>
<p>In total there are 800+ characters. In order to display and manage all of these characters, we will be implementing a pagination system where each page will display 20 characters.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1xv509vqui8s326dr4u.gif" alt="Image description" width="1112" height="868" loading="lazy"></p>
<h3 id="heading-filters">Filters</h3>
<p>There are lots of labels present in the API. Using them, we can filter our data and search for exactly what we need. Here's the demo:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxcx8oqqmgw6nqns0gdz.gif" alt="Image description" width="1112" height="796" loading="lazy"></p>
<h3 id="heading-routing">Routing</h3>
<p>We will implement this component to change our pages and create a navbar. We will use the library called <code>react-router-dom</code> to do this.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5in52wka4jccwdrol9l.gif" alt="Image description" width="1112" height="796" loading="lazy"></p>
<h3 id="heading-dynamic-routing">Dynamic Routing</h3>
<p>Using <code>react-router-dom</code>, we will also add dynamic routing so that we can learn more about a character when we click on it.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wx9c3gld1hvnn7nz3sda.gif" alt="Image description" width="1308" height="896" loading="lazy"></p>
<h1 id="heading-table-of-contents">Table of Contents</h1>
<ul>
<li>Setup</li>
<li>Folder structure</li>
<li>Data Fetching</li>
<li>Character Cards</li>
<li>Search bar</li>
<li>React-paginate</li>
<li>Filters</li>
<li>React Router</li>
<li>Episodes</li>
<li>Location</li>
<li>Dynamic Pages</li>
</ul>
<h1 id="heading-project-setup">Project Setup</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvqlz2dbwxw8hfw05s2w.png" alt="Image description" width="800" height="400" loading="lazy"></p>
<p>Before starting the project, follow these steps to get it set up:</p>
<ul>
<li>Create a folder named 'react-wiki'</li>
<li>open that folder in VS code</li>
<li>open your terminal and run these commands one by one: 👇</li>
</ul>
<pre><code class="lang-JS">npx create-react-app .

npm install bootstrap

npm install @popperjs/core --save

npm install sass

npm install react-router-dom

npm install react-paginate --save

npm start
</code></pre>
<p>To make your dev experience easier, download these VS code extensions:</p>
<ul>
<li>ES7 React/Redux/GraphQL/React-Native snippets</li>
<li>ESLint</li>
</ul>
<h1 id="heading-folder-structure">Folder structure</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4m7qo4j58o8fge72dgx.png" alt="Image description" width="800" height="400" loading="lazy"></p>
<p>We will divide our entire project into 5 components:</p>
<ul>
<li>Card</li>
<li>Pagination</li>
<li>Search</li>
<li>Filter</li>
<li>Navbar</li>
</ul>
<p>Create a folder named 'components' inside your 'src' folder and make 5 folders like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m58kt2r2cu7u5nfe7h9t.png" alt="Image description" width="812" height="420" loading="lazy"></p>
<p>And then, create these files according to the names of our components. 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gziuy17t2zlub3m30nro.png" alt="Image description" width="732" height="525" loading="lazy"></p>
<h3 id="heading-appjs">App.js</h3>
<p>Some other changes you need to do:</p>
<ul>
<li>Remove everything from the <code>App.css</code> file</li>
<li>import React hooks and Bootstrap at the top in <code>App.js</code></li>
</ul>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> <span class="hljs-string">"bootstrap/dist/css/bootstrap.min.css"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">"bootstrap/dist/js/bootstrap"</span>;
<span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
</code></pre>
<p>Next, import all your modules from components:</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> Search <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/Search/Search"</span>;
<span class="hljs-keyword">import</span> Card <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/Card/Card"</span>;
<span class="hljs-keyword">import</span> Pagination <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/Pagination/Pagination"</span>;
<span class="hljs-keyword">import</span> Filter <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/Filter/Filter"</span>;
<span class="hljs-keyword">import</span> Navbar <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/Navbar/Navbar"</span>;
</code></pre>
<p>Inside the <code>return statement</code>, remove everything and add this code: 👇</p>
<pre><code class="lang-JS">&lt;div className=<span class="hljs-string">"App"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-center mb-3"</span>&gt;</span>Characters<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"row"</span>&gt;</span>
    Filter component will be placed here
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"col-lg-8 col-12"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"row"</span>&gt;</span>
        Card component will be placed here
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<h3 id="heading-indexcss">index.css</h3>
<p>Add these default styles: 👇</p>
<pre><code class="lang-CSS"><span class="hljs-keyword">@import</span> url(<span class="hljs-string">'https://fonts.googleapis.com/css2?family=Ubuntu:wght@300;400;500;700&amp;display=swap'</span>);

<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">-webkit-font-smoothing</span>: antialiased;
  <span class="hljs-attribute">-moz-osx-font-smoothing</span>: grayscale;
}

<span class="hljs-selector-class">.ubuntu</span> {
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"Ubuntu"</span> <span class="hljs-meta">!important</span>;
}

<span class="hljs-selector-tag">code</span> {
  <span class="hljs-attribute">font-family</span>: source-code-pro, Menlo, Monaco, Consolas, <span class="hljs-string">"Courier New"</span>,
    monospace;
}
</code></pre>
<p>This is the result so far:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hiwhca06v6q4f6nvy6fs.png" alt="Image description" width="1491" height="232" loading="lazy"></p>
<p>Congratulations! We're done with the setup process. So now let's start coding.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zaff3h3an5botfzfdjxa.png" alt="Image description" width="800" height="400" loading="lazy"></p>
<h1 id="heading-how-to-fetch-data-from-the-api">How to Fetch Data from the API</h1>
<p>To fetch data from our API, we're gonna be using <a target="_blank" href="https://rickandmortyapi.com/">the rick and morty characters API</a>. We'll need to add a few things.</p>
<h3 id="heading-appjs-1">App.js 👇</h3>
<pre><code class="lang-JS"> <span class="hljs-keyword">let</span> api = <span class="hljs-string">`https://rickandmortyapi.com/api/character/?page=1`</span>
</code></pre>
<p><strong>Note :</strong> don't use quotes or double quotes around the URL, use backticks (<code>like this</code>) instead. ☝</p>
<p>To fetch data from this API, we're gonna use our <code>useEffects</code> hook like this: 👇</p>
<pre><code class="lang-JS">  useEffect(<span class="hljs-function">() =&gt;</span> { }, [api]);
</code></pre>
<p>We're writing the <code>useEffect</code> hook and putting the watch on <code>api</code>. This means that, in case the <code>api</code> variable changes, we want to load fresh new data. Let's continue. 👇</p>
<pre><code class="lang-JS">useEffect(<span class="hljs-function">() =&gt;</span> {
  (<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> data = <span class="hljs-keyword">await</span> fetch(api).then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.json());
    <span class="hljs-built_in">console</span>.log(data);
  })();
}, [api]);
</code></pre>
<p>We're using an async function to fetch our raw data and then we convert that to JSON format. Let's check the console to see what we have so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbtaztnmfa3e9t9hi8js.png" alt="Image description" width="1072" height="655" loading="lazy"></p>
<p>Wanna test it yourself? Change the page number to 2 inside the API, and you'll find new data in your console: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">let</span> api = <span class="hljs-string">`https://rickandmortyapi.com/api/character/?page=1`</span>
</code></pre>
<p>Instead of storing the data in the console, let's use the <code>useState</code> Hook. This will store the data in a variable, and we will have a function key to change the variable data whenever the useEffect hook fetches new data. 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">let</span> [fetchedData, updateFetchedData] = useState([]);
<span class="hljs-keyword">let</span> { info, results } = fetchedData;
</code></pre>
<p>Also, we are destructuring the <code>info and results</code> from the <code>fetchedData</code> variable to make our lives easier. 👆</p>
<p>The <code>fetchedData</code> variable will store the data we got from the api. We'll use the <code>updateFetchedData</code> function to change the data whenever we want.</p>
<p>Let's change our useEffect hook: 👇</p>
<pre><code class="lang-JS">useEffect(<span class="hljs-function">() =&gt;</span> {
  (<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> data = <span class="hljs-keyword">await</span> fetch(api).then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.json());
    updateFetchedData(data);
  })();
}, [api]);
</code></pre>
<h1 id="heading-how-to-make-the-character-cards">How to Make the Character Cards</h1>
<p>Let's start to build our character cards. 👇</p>
<p>First of all, import the card component by replacing the text where its written <code>Card component will be placed here</code>. Then, pass the fetched data from our <code>App.js</code> component to our <code>Card component</code> like this: 👇</p>
<pre><code class="lang-JS">&lt;Card results={results} /&gt;
</code></pre>
<h3 id="heading-cardjs">Card.js</h3>
<p>Now, first destructure the data we got from our <code>App.js</code> component. 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">const</span> Card = <span class="hljs-function">(<span class="hljs-params">{ results }</span>) =&gt;</span> {}
</code></pre>
<p>Then create a variable named <code>display</code>. This will hold all of our cards. Along with this, we will create an <code>if</code> <code>else</code> statement to check if the data we got from our API is empty or not. 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">const</span> Card = <span class="hljs-function">(<span class="hljs-params">{ results }</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> display;

  <span class="hljs-keyword">if</span> (results) {}
  <span class="hljs-keyword">else</span>{
    display = <span class="hljs-string">"No Characters Found :/"</span>;
  }

  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;&gt;</span>{display}<span class="hljs-tag">&lt;/&gt;</span></span>;
}
</code></pre>
<p>Now, we're gonna map our <code>results</code> to our cards component in a way that it'll create cards for us automatically. But first, we need to destructure the data we got from our API. 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">if</span> (results) {
  display = results.map(<span class="hljs-function">(<span class="hljs-params">x</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> { id, image, name, status, location } = x;

    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>
        <span class="hljs-attr">key</span>=<span class="hljs-string">{id}</span>
        <span class="hljs-attr">className</span>=<span class="hljs-string">"col-lg-4 col-md-6 col-sm-6 col-12 mb-4 position-relative text-dark"</span>
      &gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
});
}
</code></pre>
<p>Create a file named <code>Card.module.scss</code> and add this code: 👇</p>
<pre><code class="lang-SCSS"><span class="hljs-variable">$radius</span>: <span class="hljs-number">10px</span>;
<span class="hljs-selector-class">.card</span> {
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#0b5ed7</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-variable">$radius</span>;
}
<span class="hljs-selector-class">.content</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
}
<span class="hljs-selector-class">.img</span> {
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-variable">$radius</span> <span class="hljs-variable">$radius</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>;
}
<span class="hljs-selector-class">.badge</span> {
  <span class="hljs-attribute">top</span>: <span class="hljs-number">10px</span>; <span class="hljs-attribute">right</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">17px</span>;
}
</code></pre>
<p>Also, import it inside the <code>Card.js</code> component: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> <span class="hljs-string">"./Card.module.scss"</span>;
</code></pre>
<p>Now, its time to create our card template and put the data in their respective places. 👇</p>
<pre><code class="lang-JS">&lt;div
  className={<span class="hljs-string">`<span class="hljs-subst">${styles.card}</span> d-flex flex-column justify-content-center`</span>}
&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">styles.img</span>} <span class="hljs-attr">img-fluid</span>`} <span class="hljs-attr">src</span>=<span class="hljs-string">{image}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">styles.content</span>}`}&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"fs-5 fw-bold mb-4"</span>&gt;</span>{name}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"fs-6 fw-normal"</span>&gt;</span>Last Location<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"fs-5"</span>&gt;</span>{location.name}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<p>The results so far look like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50oqyl113kg096fcj0wl.png" alt="Image description" width="1582" height="912" loading="lazy"></p>
<p>At the very last, we're gonna use this code 👇 to let the users know whether the character is dead, alive, or unknown:</p>
<pre><code class="lang-JS">{
(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">if</span> (status === <span class="hljs-string">"Dead"</span>) {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">styles.badge</span>} <span class="hljs-attr">position-absolute</span> <span class="hljs-attr">badge</span> <span class="hljs-attr">bg-danger</span>`}&gt;</span>
        {status}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (status === <span class="hljs-string">"Alive"</span>) {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">styles.badge</span>} <span class="hljs-attr">position-absolute</span> <span class="hljs-attr">badge</span> <span class="hljs-attr">bg-success</span>`}&gt;</span>
        {status}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>
        <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">styles.badge</span>} <span class="hljs-attr">position-absolute</span> <span class="hljs-attr">badge</span> <span class="hljs-attr">bg-secondary</span>`}
      &gt;</span>
        {status}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
    );
  }
})()}
</code></pre>
<p>The reults so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hyl5uugz5h35dsve7wv.png" alt="Image description" width="1510" height="926" loading="lazy"></p>
<h1 id="heading-how-to-build-the-search-bar">How to Build the Search Bar</h1>
<p>Here's a demo video of our search bar component: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44tw3yyly0d3hiikhrpo.gif" alt="Image description" width="1112" height="888" loading="lazy"></p>
<p>Now, let's built our character search bar. But first, we need to create two <code>useState</code> hooks to hold our <code>search keywords</code> and <code>current page number</code>. 👇</p>
<h3 id="heading-appjs-2">App.js</h3>
<pre><code class="lang-JS"><span class="hljs-keyword">let</span> [pageNumber, updatePageNumber] = useState(<span class="hljs-number">1</span>);
<span class="hljs-keyword">let</span> [search, setSearch] = useState(<span class="hljs-string">""</span>);
</code></pre>
<p>Then, we need to update our API with variables. This means that whenever our API changes, our useEffect hook will fetch new data from our database. 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">let</span> api = <span class="hljs-string">`https://rickandmortyapi.com/api/character/?page=<span class="hljs-subst">${pageNumber}</span>&amp;name=<span class="hljs-subst">${search}</span>`</span>;
</code></pre>
<p>Now, we're gonna import our search bar component inside the return statement. And along with that, we're gonna pass our newly created state variables inside that component. 👇</p>
<pre><code class="lang-JS">  &lt;h1 className=<span class="hljs-string">"text-center mb-3"</span>&gt;Characters&lt;/h1&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Search</span> <span class="hljs-attr">setSearch</span>=<span class="hljs-string">{setSearch}</span> <span class="hljs-attr">updatePageNumber</span>=<span class="hljs-string">{updatePageNumber}</span> /&gt;</span></span>
</code></pre>
<p>Create a file named <code>Search.module.scss</code> to hold the styles of this specific module. Then, make these adjustments: 👇</p>
<h3 id="heading-searchmodulescss">Search.module.scss</h3>
<pre><code class="lang-SCSS"><span class="hljs-selector-class">.input</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>; <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#0b5ed7</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">1px</span> <span class="hljs-number">3px</span> <span class="hljs-number">9px</span> rgba(<span class="hljs-variable">$color</span>: <span class="hljs-number">#000000</span>, <span class="hljs-variable">$alpha</span>: <span class="hljs-number">0.25</span>);
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">15px</span>;
  &amp;<span class="hljs-selector-pseudo">:focus</span> { <span class="hljs-attribute">outline</span>: none; }
}
<span class="hljs-selector-class">.btn</span> {
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">1px</span> <span class="hljs-number">3px</span> <span class="hljs-number">9px</span> rgba(<span class="hljs-variable">$color</span>: <span class="hljs-number">#000000</span>, <span class="hljs-variable">$alpha</span>: <span class="hljs-number">0.25</span>);
}
<span class="hljs-keyword">@media</span> (max-width: <span class="hljs-number">576px</span>) {
  <span class="hljs-selector-class">.input</span> { <span class="hljs-attribute">width</span>: <span class="hljs-number">80%</span>; }
}
</code></pre>
<h3 id="heading-searchjs">Search.js</h3>
<p>First of all, we need to destructure our props. Then, we'll create a function named <code>searchBtn</code> to prevent the default behaviour of our app, like this: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> styles <span class="hljs-keyword">from</span> <span class="hljs-string">"./Search.module.scss"</span>;

<span class="hljs-keyword">const</span> Search = <span class="hljs-function">(<span class="hljs-params">{ setSearch, updatePageNumber }</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> searchBtn = <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
    e.preventDefault();
  };
};
</code></pre>
<p>Then, let's write inside our return statement. First of all, let's write the form tag to hold our input and button tags. 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">form</span>
    <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">styles.search</span>} <span class="hljs-attr">d-flex</span> <span class="hljs-attr">flex-sm-row</span> <span class="hljs-attr">flex-column</span> <span class="hljs-attr">align-items-center</span> <span class="hljs-attr">justify-content-center</span> <span class="hljs-attr">gap-4</span> <span class="hljs-attr">mb-5</span>`}
  &gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span></span>
);
</code></pre>
<p>Then, we create the button and input tags inside our form tag. 👇</p>
<pre><code class="lang-JS">&lt;input
  onChange={<span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
    updatePageNumber(<span class="hljs-number">1</span>);
    setSearch(e.target.value);
  }}
  placeholder=<span class="hljs-string">"Search for characters"</span>
  className={styles.input}
  type=<span class="hljs-string">"text"</span>
/&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span>
  <span class="hljs-attr">onClick</span>=<span class="hljs-string">{searchBtn}</span>
  <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">styles.btn</span>} <span class="hljs-attr">btn</span> <span class="hljs-attr">btn-primary</span> <span class="hljs-attr">fs-5</span>`}
&gt;</span>
  Search
<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
</code></pre>
<p>The results so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44tw3yyly0d3hiikhrpo.gif" alt="Image description" width="1112" height="888" loading="lazy"></p>
<h1 id="heading-how-to-set-up-pagination-with-react-paginate">How to Set Up Pagination with React-paginate</h1>
<p>Here's a demo of our React-paginate component: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1xv509vqui8s326dr4u.gif" alt="Image description" width="1112" height="868" loading="lazy"></p>
<p>We're gonna use <a target="_blank" href="https://www.npmjs.com/package/react-paginate">this package to paginate our data</a>. So, let's import it at the very bottom: 👇</p>
<h3 id="heading-appjs-3">App.js</h3>
<pre><code class="lang-JS">&lt;Pagination
  info={info}
  pageNumber={pageNumber}
  updatePageNumber={updatePageNumber}
/&gt;
</code></pre>
<h3 id="heading-paginationjs">Pagination.js</h3>
<p>Here, we're gonna destructure our props, and then write in some JSX styles: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> ReactPaginate <span class="hljs-keyword">from</span> <span class="hljs-string">"react-paginate"</span>;

<span class="hljs-keyword">const</span> Pagination = <span class="hljs-function">(<span class="hljs-params">{ pageNumber, info, updatePageNumber }</span>) =&gt;</span> {}
</code></pre>
<p>Inside the return statement, we write the styles in JSX like this: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">jsx</span>&gt;</span>
{`
  a {
    color: white; text-decoration: none;
  }
  @media (max-width: 768px) {
    .pagination {font-size: 12px}

    .next,
    .prev {display: none}
  }
  @media (max-width: 768px) {
    .pagination {font-size: 14px}
  }
`}
<span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/&gt;</span></span>
);
</code></pre>
<p>Now, create this function to handle the page change function: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">let</span> pageChange = <span class="hljs-function">(<span class="hljs-params">data</span>) =&gt;</span> {
  updatePageNumber(data.selected + <span class="hljs-number">1</span>);
};
</code></pre>
<p>In order to make our pagination component responsive, we need to write this little component:</p>
<pre><code class="lang-JS"><span class="hljs-keyword">const</span> [width, setWidth] = useState(<span class="hljs-built_in">window</span>.innerWidth);
<span class="hljs-keyword">const</span> updateDimensions = <span class="hljs-function">() =&gt;</span> {
  setWidth(<span class="hljs-built_in">window</span>.innerWidth);
};
useEffect(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">"resize"</span>, updateDimensions);
  <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">window</span>.removeEventListener(<span class="hljs-string">"resize"</span>, updateDimensions);
}, []);
</code></pre>
<p>Alright everyone, excellent! Now we're gonna be using the react-paginate package. </p>
<p>First, let's style everything using the built-in props of react-paginate to style basic elements: 👇</p>
<pre><code class="lang-JS">&lt;ReactPaginate
  className=<span class="hljs-string">"pagination justify-content-center my-4 gap-4"</span>
  nextLabel=<span class="hljs-string">"Next"</span>
  previousLabel=<span class="hljs-string">"Prev"</span>
  previousClassName=<span class="hljs-string">"btn btn-primary fs-5 prev"</span>
  nextClassName=<span class="hljs-string">"btn btn-primary fs-5 next"</span>
  activeClassName=<span class="hljs-string">"active"</span>
  pageClassName=<span class="hljs-string">"page-item"</span>
  pageLinkClassName=<span class="hljs-string">"page-link"</span>
/&gt;
</code></pre>
<p>Here's the main sauce: we're gonna be adding the functionalities to our component so that it works properly. 👇</p>
<pre><code class="lang-JS">&lt;ReactPaginate
  forcePage={pageNumber === <span class="hljs-number">1</span> ? <span class="hljs-number">0</span> : pageNumber - <span class="hljs-number">1</span>}
  marginPagesDisplayed={width &lt; <span class="hljs-number">576</span> ? <span class="hljs-number">1</span> : <span class="hljs-number">2</span>}
  pageRangeDisplayed={width &lt; <span class="hljs-number">576</span> ? <span class="hljs-number">1</span> : <span class="hljs-number">2</span>}
  pageCount={info?.pages}
  onPageChange={pageChange}
  <span class="hljs-comment">//.... other props here</span>
/&gt;
</code></pre>
<p>The results so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1xv509vqui8s326dr4u.gif" alt="Image description" width="1112" height="868" loading="lazy"></p>
<h1 id="heading-how-to-make-the-filters-component">How to Make the Filters Component</h1>
<p>Here's a demo of our filters component: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxcx8oqqmgw6nqns0gdz.gif" alt="Image description" width="1112" height="796" loading="lazy"></p>
<p>The very first thing we need to do is create a folder structure to hold all the little components we're gonna use. Create these components inside the <code>Filter</code> folder: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1f1mna2c00acmoer45ce.png" alt="Image description" width="697" height="341" loading="lazy"></p>
<h3 id="heading-appjs-4">App.js</h3>
<p>Now, create these useState hooks to store our status, gender, and species.</p>
<pre><code class="lang-JS"><span class="hljs-keyword">let</span> [status, updateStatus] = useState(<span class="hljs-string">""</span>);
<span class="hljs-keyword">let</span> [gender, updateGender] = useState(<span class="hljs-string">""</span>);
<span class="hljs-keyword">let</span> [species, updateSpecies] = useState(<span class="hljs-string">""</span>);
</code></pre>
<p>Also, we need to modify our <code>api</code> variable according to our useState hook variables. 👇</p>
<pre><code class="lang-JS">  <span class="hljs-keyword">let</span> api = <span class="hljs-string">`https://rickandmortyapi.com/api/character/?page=<span class="hljs-subst">${pageNumber}</span>&amp;name=<span class="hljs-subst">${search}</span>&amp;status=<span class="hljs-subst">${status}</span>&amp;gender=<span class="hljs-subst">${gender}</span>&amp;species=<span class="hljs-subst">${species}</span>`</span>;
</code></pre>
<p>Now, we'll import our <code>filter</code> component inside our App where its written <code>Filter component will be placed here</code>. Also, pass all of these required props: 👇</p>
<pre><code class="lang-JS">&lt;Filter
  pageNumber={pageNumber}
  status={status}
  updateStatus={updateStatus}
  updateGender={updateGender}
  updateSpecies={updateSpecies}
  updatePageNumber={updatePageNumber}
/&gt;
</code></pre>
<h3 id="heading-filterjs">Filter.js</h3>
<p>Let's make these changes in our Filter component so that we can achieve the desired results. First, import all of our category components like this: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> Gender <span class="hljs-keyword">from</span> <span class="hljs-string">"./category/Gender"</span>;
<span class="hljs-keyword">import</span> Species <span class="hljs-keyword">from</span> <span class="hljs-string">"./category/Species"</span>;
<span class="hljs-keyword">import</span> Status <span class="hljs-keyword">from</span> <span class="hljs-string">"./category/Status"</span>;
</code></pre>
<p>Then, destructure the passed props and place an <code>accordion</code> including a <code>clear button</code>:</p>
<pre><code class="lang-JS"><span class="hljs-keyword">const</span> Filter = <span class="hljs-function">(<span class="hljs-params">{
  pageNumber, updatePageNumber,
  updateStatus, updateGender,
  updateSpecies,
}</span>) =&gt;</span> {

<span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"col-lg-3 col-12 mb-5"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-center fw-bold fs-4 mb-2"</span>&gt;</span>Filters<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>
    <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">cursor:</span> "<span class="hljs-attr">pointer</span>" }} <span class="hljs-attr">onClick</span>=<span class="hljs-string">{clear}</span>
    <span class="hljs-attr">className</span>=<span class="hljs-string">"text-primary text-decoration-underline text-center mb-3"</span>
  &gt;</span> Clear Filters <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"accordionExample"</span>&gt;</span>
    {/* Category components will be placed here */}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);
};
</code></pre>
<p>Make this function so that we can clear our filters and refresh the page: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">let</span> clear = <span class="hljs-function">() =&gt;</span> {
  updateStatus(<span class="hljs-string">""</span>);
  updateGender(<span class="hljs-string">""</span>);
  updateSpecies(<span class="hljs-string">""</span>);
  updatePageNumber(<span class="hljs-number">1</span>);
  <span class="hljs-built_in">window</span>.location.reload(<span class="hljs-literal">false</span>);
};
</code></pre>
<p>The results so far look like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcnncgfpnjqkaow9201j.png" alt="Image description" width="460" height="282" loading="lazy"></p>
<h3 id="heading-filterbtnjs">FilterBTN.js</h3>
<p>First, let's create these filter buttons. We will also destructure the required props. 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmas54wdnue9d7rfcreg.png" alt="Image description" width="542" height="506" loading="lazy"></p>
<pre><code class="lang-JS"><span class="hljs-keyword">const</span> FilterBTN = <span class="hljs-function">(<span class="hljs-params">{ input, task, updatePageNumber, index, name }</span>) =&gt;</span> {
<span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">jsx</span>&gt;</span>
    {`
      .x:checked + label {
        background-color: #0b5ed7;
        color: white }
      input[type="radio"] { display: none; }
    `}
  <span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);
};
</code></pre>
<p>Now, we place the main input component with labels below the <code>style jsx</code>: 👇</p>
<pre><code class="lang-JS">&lt;div className=<span class="hljs-string">"form-check"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">input</span>
    <span class="hljs-attr">className</span>=<span class="hljs-string">"form-check-input x"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"radio"</span>
    <span class="hljs-attr">name</span>=<span class="hljs-string">{name}</span> <span class="hljs-attr">id</span>=<span class="hljs-string">{</span>`${<span class="hljs-attr">name</span>}<span class="hljs-attr">-</span>${<span class="hljs-attr">index</span>}`}
  /&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">label</span>
    <span class="hljs-attr">onClick</span>=<span class="hljs-string">{(x)</span> =&gt;</span> {
      task(input); updatePageNumber(1);
    }}
    className="btn btn-outline-primary"
    for={`${name}-${index}`}
  &gt; {input} <span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<h3 id="heading-statusjs">Status.js</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmas54wdnue9d7rfcreg.png" alt="Image description" width="542" height="506" loading="lazy"></p>
<p>Write this starter code inside Status.js:</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> FilterBTN <span class="hljs-keyword">from</span> <span class="hljs-string">"../FilterBTN"</span>;

<span class="hljs-keyword">const</span> Status = <span class="hljs-function">(<span class="hljs-params">{ updateStatus, updatePageNumber }</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> status = [<span class="hljs-string">"Alive"</span>, <span class="hljs-string">"Dead"</span>, <span class="hljs-string">"Unknown"</span>];
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-item"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-header"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"headingOne"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
          <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-button"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span>
          <span class="hljs-attr">data-bs-toggle</span>=<span class="hljs-string">"collapse"</span> <span class="hljs-attr">data-bs-target</span>=<span class="hljs-string">"#collapseOne"</span>
          <span class="hljs-attr">aria-expanded</span>=<span class="hljs-string">"true"</span> <span class="hljs-attr">aria-controls</span>=<span class="hljs-string">"collapseOne"</span>
        &gt;</span> Status <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
};
</code></pre>
<p>Then let's create the status buttons by mapping our array of data. 👇</p>
<p>Below the ending <code>h2</code> tag, put these inside 👇 which will help us automatically map data and make our Status buttons:</p>
<pre><code class="lang-JS">&lt;div
  id=<span class="hljs-string">"collapseOne"</span> className=<span class="hljs-string">"accordion-collapse collapse show"</span>
  aria-labelledby=<span class="hljs-string">"headingOne"</span> data-bs-parent=<span class="hljs-string">"#accordionExample"</span>
&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-body d-flex flex-wrap gap-3"</span>&gt;</span>
  {status.map((item, index) =&gt; (
    <span class="hljs-tag">&lt;<span class="hljs-name">FilterBTN</span>
      <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span>
      <span class="hljs-attr">index</span>=<span class="hljs-string">{index}</span>
      <span class="hljs-attr">name</span>=<span class="hljs-string">"status"</span>
      <span class="hljs-attr">task</span>=<span class="hljs-string">{updateStatus}</span>
      <span class="hljs-attr">updatePageNumber</span>=<span class="hljs-string">{updatePageNumber}</span>
      <span class="hljs-attr">input</span>=<span class="hljs-string">{item}</span>
    /&gt;</span>
  ))}
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<h4 id="heading-time-to-test-in-filterjs">time to test in Filter.js</h4>
<p>Write these lines inside Filter.js to check whether the component works or not: 👇</p>
<pre><code class="lang-JS">&lt;Status
  updatePageNumber={updatePageNumber}
  updateStatus={updateStatus}
/&gt;
</code></pre>
<p>The results so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxcq2s8q8ntfe6dh36vc.gif" alt="Image description" width="1112" height="796" loading="lazy"></p>
<h3 id="heading-speciesjs">Species.js</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmvw4v4tqh6gx2lupoju.png" alt="Image description" width="682" height="667" loading="lazy"></p>
<p>Write thes starter codes inside Species.js:</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> FilterBTN <span class="hljs-keyword">from</span> <span class="hljs-string">"../FilterBTN"</span>;

<span class="hljs-keyword">const</span> Species = <span class="hljs-function">(<span class="hljs-params">{ updateSpecies, updatePageNumber }</span>) =&gt;</span> {
<span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-item "</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-header"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"headingTwo"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
      <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-button collapsed"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span>
      <span class="hljs-attr">data-bs-toggle</span>=<span class="hljs-string">"collapse"</span> <span class="hljs-attr">data-bs-target</span>=<span class="hljs-string">"#collapseTwo"</span>
      <span class="hljs-attr">aria-expanded</span>=<span class="hljs-string">"false"</span> <span class="hljs-attr">aria-controls</span>=<span class="hljs-string">"collapseTwo"</span>
    &gt;</span> Species <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
)}
</code></pre>
<p>Now, create an array to hold all of our possible species data: 👇</p>
<pre><code class="lang-JS">  <span class="hljs-keyword">let</span> species = [
    <span class="hljs-string">"Human"</span>, <span class="hljs-string">"Alien"</span>, <span class="hljs-string">"Humanoid"</span>,
    <span class="hljs-string">"Poopybutthole"</span>, <span class="hljs-string">"Mythological"</span>, <span class="hljs-string">"Unknown"</span>,
    <span class="hljs-string">"Animal"</span>, <span class="hljs-string">"Disease"</span>,<span class="hljs-string">"Robot"</span>,<span class="hljs-string">"Cronenberg"</span>,<span class="hljs-string">"Planet"</span>,
  ];
</code></pre>
<p>And then, let's create the Species buttons by mapping our array data like this: 👇</p>
<pre><code class="lang-JS">&lt;div
    id=<span class="hljs-string">"collapseTwo"</span> className=<span class="hljs-string">"accordion-collapse collapse"</span>
    aria-labelledby=<span class="hljs-string">"headingTwo"</span>
    data-bs-parent=<span class="hljs-string">"#accordionExample"</span>
  &gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-body d-flex flex-wrap gap-3"</span>&gt;</span>
    {species.map((item, index) =&gt; {
      return (
        <span class="hljs-tag">&lt;<span class="hljs-name">FilterBTN</span>
          <span class="hljs-attr">name</span>=<span class="hljs-string">"species"</span> <span class="hljs-attr">index</span>=<span class="hljs-string">{index}</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span>
          <span class="hljs-attr">updatePageNumber</span>=<span class="hljs-string">{updatePageNumber}</span>
          <span class="hljs-attr">task</span>=<span class="hljs-string">{updateSpecies}</span> <span class="hljs-attr">input</span>=<span class="hljs-string">{item}</span>
        /&gt;</span>
      );
    })}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<h4 id="heading-time-to-test-in-filterjs-1">time to test in Filter.js</h4>
<p>Write these lines inside Filter.js to check whether the component works or not: 👇</p>
<pre><code class="lang-JS">&lt;Species
  updatePageNumber={updatePageNumber}
  updateSpecies={updateSpecies}
/&gt;
</code></pre>
<p>The results so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aav8duudtdkwxznfayn0.gif" alt="Image description" width="1112" height="796" loading="lazy"></p>
<h3 id="heading-genderjs">Gender.js</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45yw5m81hr3utyydwfms.png" alt="Image description" width="618" height="532" loading="lazy"></p>
<p>Write this starter code: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> FilterBTN <span class="hljs-keyword">from</span> <span class="hljs-string">"../FilterBTN"</span>;

<span class="hljs-keyword">const</span> Gender = <span class="hljs-function">(<span class="hljs-params">{ updateGender, updatePageNumber }</span>) =&gt;</span> {
<span class="hljs-keyword">let</span> genders = [<span class="hljs-string">"female"</span>, <span class="hljs-string">"male"</span>, <span class="hljs-string">"genderless"</span>, <span class="hljs-string">"unknown"</span>];
<span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-item"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-header"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"headingThree"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span>
        <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-button collapsed"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"button"</span>
        <span class="hljs-attr">data-bs-toggle</span>=<span class="hljs-string">"collapse"</span> <span class="hljs-attr">data-bs-target</span>=<span class="hljs-string">"#collapseThree"</span>
        <span class="hljs-attr">aria-expanded</span>=<span class="hljs-string">"false"</span> <span class="hljs-attr">aria-controls</span>=<span class="hljs-string">"collapseThree"</span>
      &gt;</span> Gender <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);
};
</code></pre>
<p>Below the final <code>h2</code> tag, put this code inside 👇 which will help us automatically map data and make our gender buttons:</p>
<pre><code class="lang-JS">&lt;div id=<span class="hljs-string">"collapseThree"</span> className=<span class="hljs-string">"accordion-collapse collapse"</span>
  aria-labelledby=<span class="hljs-string">"headingThree"</span> data-bs-parent=<span class="hljs-string">"#accordionExample"</span>
&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"accordion-body d-flex flex-wrap gap-3"</span>&gt;</span>
  {genders.map((items, index) =&gt; {
    return (
      <span class="hljs-tag">&lt;<span class="hljs-name">FilterBTN</span>
        <span class="hljs-attr">name</span>=<span class="hljs-string">"gender"</span> <span class="hljs-attr">index</span>=<span class="hljs-string">{index}</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span>
        <span class="hljs-attr">updatePageNumber</span>=<span class="hljs-string">{updatePageNumber}</span>
        <span class="hljs-attr">task</span>=<span class="hljs-string">{updateGender}</span> <span class="hljs-attr">input</span>=<span class="hljs-string">{items}</span>
      /&gt;</span>
      );
    })}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<h4 id="heading-time-to-test-in-filterjs-2">time to test in Filter.js</h4>
<p>Write these lines inside Filter.js to check whether the component works or not: 👇</p>
<pre><code class="lang-JS">&lt;Gender
  updatePageNumber={updatePageNumber}
  updateGender={updateGender}
/&gt;
</code></pre>
<p>The results so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9aklsikf2brx5h7vxxsm.gif" alt="Image description" width="1112" height="796" loading="lazy"></p>
<h1 id="heading-how-to-set-up-react-router">How to Set Up React Router</h1>
<p>Here's a demo of our Navigation component: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5in52wka4jccwdrol9l.gif" alt="Image description" width="1112" height="796" loading="lazy"></p>
<p>Let's start coding!</p>
<p>First, create a folder named <code>Pages</code> inside the <code>src</code> folder. It will hold 2 files - <code>Episodes.js</code> and <code>Location.js</code>. Something like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fd0k8mt8s1sq842axwsd.png" alt="Image description" width="771" height="295" loading="lazy"></p>
<h3 id="heading-appjs-5">App.js</h3>
<p>Import your newly created pages component in <code>App.js</code>: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> Episodes <span class="hljs-keyword">from</span> <span class="hljs-string">"./Pages/Episodes"</span>;
<span class="hljs-keyword">import</span> Location <span class="hljs-keyword">from</span> <span class="hljs-string">"./Pages/Location"</span>;
</code></pre>
<p>In order to declare the Router and to define all sorts of file paths, we need to import <code>react-router-dom</code> in <code>App.js</code> including its core components. 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> { BrowserRouter <span class="hljs-keyword">as</span> Router, Routes, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
</code></pre>
<p>Now, create a new functional component called <code>Home</code> inside the App.js file. Then, cut everything from the <code>App</code> component and put it inside the <code>Home</code> component: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">const</span> Home = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-comment">// Everything you've written so far</span>
}
</code></pre>
<p>Inside your <code>App</code> component function, create a new <code>Router</code> component and put it inside the <code>Navbar</code> component. 👇</p>
<pre><code class="lang-JS"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Router</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Navbar</span> /&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">Router</span>&gt;</span></span>
  );
}
</code></pre>
<p>Now, we need to define all our routes. Remember, <code>Routes</code> is a collection of <code>Route</code>s. <code>Route</code> is the actual file path.</p>
<p>Each route requires 2 things: The <code>path</code> where the app will actually lead to and the <code>element</code> which will get loaded. 👇</p>
<pre><code class="lang-JS">&lt;Routes&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Home</span> /&gt;</span>} /&gt;</span>

  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/episodes"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Episodes</span> /&gt;</span>} /&gt;</span>

  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/location"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">Location</span> /&gt;</span>} /&gt;</span>
&lt;/Routes&gt;
</code></pre>
<h3 id="heading-navbarjs">Navbar.js</h3>
<p>So far so good! Now, let's make the navbar component. First, import 2 components from <code>react-router-dom</code>. Then, write this bootstrap parent class including the brand name to hold our navbar pages component: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> { NavLink, Link } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> Navbar = <span class="hljs-function">() =&gt;</span> {
<span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar navbar-expand-lg navbar-light bg-light mb-4"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-brand fs-3 ubuntu"</span>&gt;</span>
        Rick &amp; Morty <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-primary"</span>&gt;</span>WiKi<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">jsx</span>&gt;</span>{`
        button[aria-expanded="false"] &gt; .close {
          display: none;
        }
        button[aria-expanded="true"] &gt; .open {
          display: none;
        }
      `}<span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
);
};
</code></pre>
<p>Write this code to generate our responsive hamburger menu: 👇</p>
<pre><code class="lang-JS">&lt;button
  className=<span class="hljs-string">"navbar-toggler border-0"</span>
  type=<span class="hljs-string">"button"</span>
  data-bs-toggle=<span class="hljs-string">"collapse"</span>
  data-bs-target=<span class="hljs-string">"#navbarNavAltMarkup"</span>
  aria-controls=<span class="hljs-string">"navbarNavAltMarkup"</span>
  aria-expanded=<span class="hljs-string">"false"</span>
  aria-label=<span class="hljs-string">"Toggle navigation"</span>
&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-bars open text-dark"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-times close text-dark"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>
&lt;/button&gt;
</code></pre>
<p>Write this code to generate our clickable navbar links. Notice that we are using the  component from 'react-router-dom' to link to our page's component URL: 👇</p>
<pre><code class="lang-JS">&lt;div
  className=<span class="hljs-string">"collapse navbar-collapse justify-content-end"</span>
  id=<span class="hljs-string">"navbarNavAltMarkup"</span>
&gt; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"navbar-nav fs-5"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-link"</span>&gt;</span>
      Characters
    <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">to</span>=<span class="hljs-string">"/episodes"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-link"</span>&gt;</span>
      Episode
    <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span>
      <span class="hljs-attr">activeClassName</span>=<span class="hljs-string">"active"</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"nav-link"</span>
      <span class="hljs-attr">to</span>=<span class="hljs-string">"/location"</span> &gt;</span>Location<span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<h3 id="heading-appcss">App.css</h3>
<p>Also, write these styles if you want to make the navbar look nice and let your users know exactly which page they are on currently: 👇</p>
<pre><code class="lang-CSS"><span class="hljs-selector-class">.active</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#0b5ed7</span> <span class="hljs-meta">!important</span>;
  <span class="hljs-attribute">font-weight</span>: bold;
  <span class="hljs-attribute">border-bottom</span>: <span class="hljs-number">3px</span> solid <span class="hljs-number">#0b5ed7</span>;
}
</code></pre>
<p>Then, inside <code>Navbar.js</code>, import the styles like this: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> <span class="hljs-string">"../../App.css"</span>;
</code></pre>
<p>The result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i5in52wka4jccwdrol9l.gif" alt="Image description" width="1112" height="796" loading="lazy"></p>
<h1 id="heading-episodes">Episodes</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kby93lasrq6di8md58sg.png" alt="Image description" width="1293" height="808" loading="lazy"></p>
<p>For creating this page, we need 2 components: the first one is the <code>card component</code>, which is already built, and the second one is the <code>Input Group</code> component through which we can change our episode number.</p>
<h3 id="heading-inputgroupjs">InputGroup.js</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d53jsa8tz1e2aab1dk9s.gif" alt="Image description" width="868" height="772" loading="lazy"></p>
<p>Our input groups will only be available in the <code>Episodes</code> &amp; <code>Location</code> component so that we can change the episode number and location to search for characters. Let's start! 👇</p>
<p>Inside the <code>category</code> folder of <code>Filter</code> folder, create a new file named <code>InputGroup.js</code> and write this starter code including the props destructuring system: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">const</span> InputGroup = <span class="hljs-function">(<span class="hljs-params">{ name, changeID, total }</span>) =&gt;</span> {
<span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"input-group mb-3"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">select</span>
  <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e)</span> =&gt;</span> changeID(e.target.value)}
  className="form-select"
  id={name}
  &gt;<span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
};
</code></pre>
<p>Then, let's create our input group option. Write this code inside your <code>select</code> tag: 👇</p>
<pre><code class="lang-JS">&lt;option value=<span class="hljs-string">"1"</span>&gt;Choose...&lt;/option&gt;
{[...Array(total).keys()].map(<span class="hljs-function">(<span class="hljs-params">x, index</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{x</span> + <span class="hljs-attr">1</span>}&gt;</span>
      {name} - {x + 1}
    <span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span></span>
  );
})}
</code></pre>
<h3 id="heading-episodesjs">Episodes.js</h3>
<p>Inside this file, import these components: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> React, { useEffect, useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> Card <span class="hljs-keyword">from</span> <span class="hljs-string">"../components/Card/Card"</span>;
<span class="hljs-keyword">import</span> InputGroup <span class="hljs-keyword">from</span> <span class="hljs-string">"../components/Filter/category/InputGroup"</span>;
</code></pre>
<p>Now, create these states so that we can hold and change crucial information to fetch data from our <code>api</code>: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">const</span> Episodes = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">let</span> [results, setResults] = React.useState([]);
  <span class="hljs-keyword">let</span> [info, setInfo] = useState([]);
  <span class="hljs-keyword">let</span> { air_date, episode, name } = info;
  <span class="hljs-keyword">let</span> [id, setID] = useState(<span class="hljs-number">1</span>);

<span class="hljs-keyword">let</span> api = <span class="hljs-string">`https://rickandmortyapi.com/api/episode/<span class="hljs-subst">${id}</span>`</span>;
}
</code></pre>
<p>Write this code to fetch data from our API: 👇</p>
<pre><code class="lang-JS">useEffect(<span class="hljs-function">() =&gt;</span> {
  (<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> data = <span class="hljs-keyword">await</span> fetch(api).then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.json());
    setInfo(data);

    <span class="hljs-keyword">let</span> a = <span class="hljs-keyword">await</span> <span class="hljs-built_in">Promise</span>.all(
      data.characters.map(<span class="hljs-function">(<span class="hljs-params">x</span>) =&gt;</span> {
        <span class="hljs-keyword">return</span> fetch(x).then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.json());
      })
    );
    setResults(a);
  })();
}, [api]);
</code></pre>
<p>Now, let's write the code to render the results on our screen. First, let's display the Episode name and air date like this: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"row mb-3"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-center mb-3"</span>&gt;</span>
      Episode name :{" "}
      <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-primary"</span>&gt;</span>{name === "" ? "Unknown" : name}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-center"</span>&gt;</span>
      Air Date: {air_date === "" ? "Unknown" : air_date}
    <span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
);
</code></pre>
<p>The results so far look like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rq6yd767in8qi5eu3mt.png" alt="Image description" width="1465" height="280" loading="lazy"></p>
<p>Now, let's display the character cards and input groups: 👇</p>
<pre><code class="lang-JS">&lt;div className=<span class="hljs-string">"row"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"col-lg-3 col-12 mb-4"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h4</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-center mb-4"</span>&gt;</span>Pick Episode<span class="hljs-tag">&lt;/<span class="hljs-name">h4</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">InputGroup</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"Episode"</span> <span class="hljs-attr">changeID</span>=<span class="hljs-string">{setID}</span> <span class="hljs-attr">total</span>=<span class="hljs-string">{51}</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"col-lg-8 col-12"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"row"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Card</span> <span class="hljs-attr">results</span>=<span class="hljs-string">{results}</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<p>The results so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kby93lasrq6di8md58sg.png" alt="Image description" width="1293" height="808" loading="lazy"></p>
<h1 id="heading-location">Location</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yq78xas7xjcgzgnkp74.gif" alt="Image description" width="1480" height="896" loading="lazy"></p>
<p>Making this component is similar to making <code>episodes</code> page. First, copy everything from the <code>episodes</code> page and make these few changes: 👇</p>
<pre><code class="lang-JS">  <span class="hljs-keyword">let</span> [results, setResults] = useState([]);
  <span class="hljs-keyword">let</span> [info, setInfo] = useState([]);
  <span class="hljs-keyword">let</span> { dimension, type, name } = info;
  <span class="hljs-keyword">let</span> [number, setNumber] = useState(<span class="hljs-number">1</span>);

  <span class="hljs-keyword">let</span> api = <span class="hljs-string">`https://rickandmortyapi.com/api/location/<span class="hljs-subst">${number}</span>`</span>;
</code></pre>
<p>Now change only one keyword in the <code>useEffect</code> hook, from <code>characters</code> to <code>residents</code>, like this: 👇</p>
<pre><code class="lang-JS">useEffect(<span class="hljs-function">() =&gt;</span> {
      <span class="hljs-comment">// Other codes are here</span>
      data.residents.map(<span class="hljs-function">(<span class="hljs-params">x</span>) =&gt;</span> {
      })
    <span class="hljs-comment">// Other codes are here</span>
}, [api]);
</code></pre>
<p>In the return statement, make these changes: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-center mb-3"</span>&gt;</span>
  Location :
  <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-primary"</span>&gt;</span>
    {name === "" ? "Unknown" : name}
  <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span></span>
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h5</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-center"</span>&gt;</span>
  Dimension: {dimension === "" ? "Unknown" : dimension}
<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span></span>
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h6</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-center"</span>&gt;</span>Type: {type === "" ? "Unknown" : type}<span class="hljs-tag">&lt;/<span class="hljs-name">h6</span>&gt;</span></span>
);
</code></pre>
<p>And finally, make these changes for our <code>InputGroup</code> component: 👇</p>
<pre><code class="lang-JS">&lt;InputGroup name=<span class="hljs-string">"Location"</span> changeID={setNumber} total={<span class="hljs-number">126</span>} /&gt;
</code></pre>
<p>The results so far 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yq78xas7xjcgzgnkp74.gif" alt="Image description" width="1480" height="896" loading="lazy"></p>
<h1 id="heading-dynamic-pages">Dynamic Pages</h1>
<p>This is the last step of our project. Our main gioal is to know more about a specific character when we click on the card. We're gonna be using the dynamic module system of <code>react-router-dom</code> – something like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wx9c3gld1hvnn7nz3sda.gif" alt="Image description" width="1308" height="896" loading="lazy"></p>
<h3 id="heading-carddetailsjs">CardDetails.js</h3>
<p>To accomplish our goals, we need to create a seperate component to display more details about the character. We will create a new file named <code>CardDetails.js</code> inside the <code>Card</code> folder.abs.</p>
<p>First, let's import the essential components:</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { useParams } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;
</code></pre>
<p>Then, we need to store our <code>api</code> and use some <code>useState</code> hooks. We will use the <code>useParams</code> hook to get the id from the URL: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">const</span> CardDetails = <span class="hljs-function">() =&gt;</span> {
<span class="hljs-keyword">let</span> { id } = useParams();
<span class="hljs-keyword">let</span> [fetchedData, updateFetchedData] = useState([]);
<span class="hljs-keyword">let</span> { name, location, origin, gender, image, status, species } = fetchedData;

<span class="hljs-keyword">let</span> api = <span class="hljs-string">`https://rickandmortyapi.com/api/character/<span class="hljs-subst">${id}</span>`</span>;
}
</code></pre>
<p>We'll use this useEffect hook to fetch data from our API: 👇</p>
<pre><code class="lang-JS">useEffect(<span class="hljs-function">() =&gt;</span> {
  (<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> data = <span class="hljs-keyword">await</span> fetch(api).then(<span class="hljs-function">(<span class="hljs-params">res</span>) =&gt;</span> res.json());
    updateFetchedData(data);
  })();
}, [api]);
</code></pre>
<p>Now, let's write this code which will output the name and image of our character: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"container d-flex justify-content-center mb-5"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"d-flex flex-column gap-3"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"text-center"</span>&gt;</span>{name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"img-fluid"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{image}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
)
</code></pre>
<p>Now, write this code in case you want to display the current status of every character: 👇</p>
<pre><code class="lang-JS">{(<span class="hljs-function">() =&gt;</span> {
<span class="hljs-keyword">if</span> (status === <span class="hljs-string">"Dead"</span>) {
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"badge bg-danger fs-5"</span>&gt;</span>{status}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (status === <span class="hljs-string">"Alive"</span>) {
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">" badge bg-success fs-5"</span>&gt;</span>{status}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
} <span class="hljs-keyword">else</span> {
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"badge bg-secondary fs-5"</span>&gt;</span>{status}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
}
})()}
</code></pre>
<p>Next up, write this code to display all the information about the character: 👇</p>
<pre><code class="lang-JS">&lt;div className=<span class="hljs-string">"content"</span>&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"fw-bold"</span>&gt;</span>Gender : <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    {gender}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"fw-bold"</span>&gt;</span>Location: <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    {location?.name}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"fw-bold"</span>&gt;</span>Origin: <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    {origin?.name}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"fw-bold"</span>&gt;</span>Species: <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    {species}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
&lt;/div&gt;
</code></pre>
<h3 id="heading-appjs-6">App.js</h3>
<p>Next up, we need to define our paths so that our dynamic router component works properly. First, import and then add this code: 👇</p>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> CardDetails <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/Card/CardDetails"</span>;
<span class="hljs-comment">// other codes are here</span>

<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Routes</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/:id"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">CardDetails</span> /&gt;</span>} /&gt;
  <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/episodes/:id"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">CardDetails</span> /&gt;</span>} /&gt;
  <span class="hljs-tag">&lt;<span class="hljs-name">Route</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"/location/:id"</span> <span class="hljs-attr">element</span>=<span class="hljs-string">{</span>&lt;<span class="hljs-attr">CardDetails</span> /&gt;</span>} /&gt;
<span class="hljs-tag">&lt;/<span class="hljs-name">Routes</span>&gt;</span></span>
</code></pre>
<p>Now, scroll down inside your App.js and make this small modification 👇 so that it refers to the home page:</p>
<pre><code class="lang-JS">&lt;Card page=<span class="hljs-string">"/"</span> results={results} /&gt;
</code></pre>
<h3 id="heading-cardjs-1">Card.js</h3>
<p>Go to your card component and make these changes: 👇</p>
<ul>
<li>First, destructure the new props and import <code>Link</code> from <code>react-router-dom</code></li>
</ul>
<pre><code class="lang-JS"><span class="hljs-keyword">import</span> { Link } <span class="hljs-keyword">from</span> <span class="hljs-string">"react-router-dom"</span>;

<span class="hljs-keyword">const</span> Card = <span class="hljs-function">(<span class="hljs-params">{ page, results }</span>) =&gt;</span> {}
</code></pre>
<ul>
<li>Next up, wrap everything inside the return statement inside a Link tag:</li>
</ul>
<pre><code class="lang-JS">&lt;Link
  style={{ <span class="hljs-attr">textDecoration</span>: <span class="hljs-string">"none"</span> }}
  to={<span class="hljs-string">`<span class="hljs-subst">${page}</span><span class="hljs-subst">${id}</span>`</span>}
  key={id}
  className=<span class="hljs-string">"col-lg-4 col-md-6 col-sm-6 col-12 mb-4 position-relative text-dark"</span>
&gt;
{<span class="hljs-comment">/* Other codes are here */</span>}
&lt;/Link&gt;
</code></pre>
<h3 id="heading-episodesjs-1">Episodes.js</h3>
<p>In this file, just adjust this small line: 👇</p>
<pre><code class="lang-JS">&lt;Card page=<span class="hljs-string">"/episodes/"</span> results={results} /&gt;
</code></pre>
<h3 id="heading-locationjs">Location.js</h3>
<p>Just like in Episodes.js, just adjust this small line: 👇</p>
<pre><code class="lang-JS">&lt;Card page=<span class="hljs-string">"/location/"</span> results={results} /&gt;
</code></pre>
<p>The results: ✨✨✨</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wx9c3gld1hvnn7nz3sda.gif" alt="Image description" width="1308" height="896" loading="lazy"></p>
<h1 id="heading-conclusion">Conclusion</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6596mzy60z7yb2366ffa.png" alt="Image description" width="715" height="400" loading="lazy"></p>
<p>Congratulations for reading until the end! Now you can easily and efficiently use React JS and Bootstrap to handle projects. </p>
<p>You have also learned how to fetch data from an API and map the results. Not only that, you also have a project to show your local recruiter.</p>
<p>Here's your medal for reading until the end ❤️</p>
<h2 id="heading-suggestions-and-criticisms-are-highly-appreciated">Suggestions and criticisms are highly appreciated ❤️</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="1000" height="245" loading="lazy"></p>
<ul>
<li><a target="_blank" href="https://www.linkedin.com/in/joyshaheb/">LinkedIn/ JoyShaheb</a></li>
<li><a target="_blank" href="https://www.youtube.com/c/joyshaheb">YouTube / JoyShaheb</a></li>
<li><a target="_blank" href="https://twitter.com/JoyShaheb">Twitter / JoyShaheb</a></li>
<li><a target="_blank" href="https://www.instagram.com/joyshaheb/">Instagram / JoyShaheb</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn JavaScript Form Validation – Build a JS Project for Beginners ✨ ]]>
                </title>
                <description>
                    <![CDATA[ Today we're going to learn how to do form validation in JavaScript. We'l also add images and media queries to build out the entire project and keep it as a portfolio.   Here's the project demo that we're gonna build 👇 Desktop design Here's a small ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-javascript-form-validation-by-making-a-form/</link>
                <guid isPermaLink="false">66b209694b36d956a7749c20</guid>
                
                    <category>
                        <![CDATA[ Form validations ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Wed, 22 Sep 2021 18:33:54 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/Frame-31.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're going to learn how to do <strong>form validation</strong> in JavaScript. We'l also add images and media queries to build out the entire project and keep it as a <strong>portfolio</strong>.  </p>
<p>Here's the project demo that we're gonna build 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-30--1-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Desktop design</strong></em></p>
<p>Here's a small sample of how the form will work 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/dvdfvdf-1.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>project sample</strong></em></p>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like"><strong>You can watch this tutorial on YouTube as well if you like:</strong></h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/VufN46OyFng" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-source-code">Source code</h2>
<p>You can get the source code, including the images, from here:</p>
<ul>
<li><a target="_blank" href="https://codepen.io/joyshaheb/pen/XWgdOyY">CodePen</a></li>
<li><a target="_blank" href="https://github.com/JoyShaheb/Project-image-repo/tree/main/Form-Validation">GitHub</a></li>
</ul>
<h1 id="heading-how-to-setup-the-project">How to Setup the Project</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-1--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Follow these steps to set up our project: 👇</p>
<ul>
<li>Create a new folder named "Project" and open VS Code</li>
<li>Create index.html, style.css, and main.js files</li>
<li>Link the files inside the HTML </li>
<li>Download the <a target="_blank" href="https://github.com/JoyShaheb/Project-image-repo/tree/main/Form-Validation">images from my GitHub repository</a></li>
<li>Paste this font-awesome link inside the head tag. Then, we can access Font Awesome icons 👇👇</li>
</ul>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span>
      <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span>
      <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"</span>
      <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span>
    /&gt;</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-2--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-heres-what-well-cover">Here's what we'll cover:</h1>
<ul>
<li>Writing the HTML</li>
<li>Adding the CSS</li>
<li>Writing the JavaScript</li>
<li>Adding a social media button</li>
<li>Adding the images</li>
<li>Media queries for the mobile version (responsive)</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-20--2-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Table of contents</strong></em></p>
<h1 id="heading-how-to-write-the-html">How to Write the HTML</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Create a class named <code>.container</code> inside the body tag and host the form tag which will have an id of form 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"form"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>And inside the form tag, create 4 <code>div</code>s, like this 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"form"</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"title"</span>&gt;</span>Get Started<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<p>Inside those 3 empty div tags, let's create 3 inputs [Username, Email, and Password] along with the icons and labels.</p>
<p><strong>Note</strong>: we are creating an <code>.error</code> class name. We will inject the error message here using JavaScript. </p>
<h4 id="heading-username-input">Username Input</h4>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!-- User Name input --&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"username"</span>&gt;</span>User Name<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-user"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
        <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span>
        <span class="hljs-attr">name</span>=<span class="hljs-string">"username"</span>
        <span class="hljs-attr">id</span>=<span class="hljs-string">"username"</span>
        <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Joy Shaheb"</span>
     /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-exclamation-circle failure-icon"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"far fa-check-circle success-icon"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"error"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h4 id="heading-email-input">Email Input</h4>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!-- Email input --&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"email"</span>&gt;</span>Email<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"far fa-envelope"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
        <span class="hljs-attr">type</span>=<span class="hljs-string">"email"</span>
        <span class="hljs-attr">name</span>=<span class="hljs-string">"email"</span>
        <span class="hljs-attr">id</span>=<span class="hljs-string">"email"</span>
        <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"abc@gmail.com"</span>
     /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-exclamation-circle failure-icon"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"far fa-check-circle success-icon"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"error"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h4 id="heading-password-input">Password Input</h4>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!--   Password input --&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">"password"</span>&gt;</span>Password<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-lock"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">input</span>
        <span class="hljs-attr">type</span>=<span class="hljs-string">"password"</span>
        <span class="hljs-attr">name</span>=<span class="hljs-string">"password"</span>
        <span class="hljs-attr">id</span>=<span class="hljs-string">"password"</span>
        <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Password here"</span>
     /&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fas fa-exclamation-circle failure-icon"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"far fa-check-circle success-icon"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"error"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h4 id="heading-how-to-make-the-button">How to make the button</h4>
<p>Finally, add the button before the form closing tag like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- other codes are here --&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"btn"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<p>Here's the result so far 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/fdgdfgdfdffcvb.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result So far</strong></em></p>
<p>Congrats on completing the HTML part! 🍾🎉🥂</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-7.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-how-to-add-the-css">How to Add the CSS</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Let's add the CSS to style our form. First, let's remove the default styles of our browser including the font-family👇</p>
<pre><code class="lang-css"><span class="hljs-comment">/**
* ! changing default styles of brower
**/</span>

* {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;
}

<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: sans-serif;
}
</code></pre>
<p>Now, apply these styles for the form tag:</p>
<pre><code class="lang-css"><span class="hljs-comment">/**
* ! style rules for form section
**/</span>

<span class="hljs-selector-tag">form</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: column;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">400px</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">2px</span> <span class="hljs-number">2px</span> <span class="hljs-number">4px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.1</span>);
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">50px</span>;
}
</code></pre>
<p>Next, make these changes for our title text: 👇👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
  <span class="hljs-attribute">font-weight</span>: bold;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">20px</span>;
}
</code></pre>
<p>Your result so far 👇👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/fsdfsdsfxvxcvxd.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result so far</strong></em></p>
<p>Now, add a margin to the bottom of our label text like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">label</span> {
  <span class="hljs-attribute">display</span>: block;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">5px</span>;
}
</code></pre>
<p>And add these styles to change the look and feel of our input tags 👇👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">form</span> <span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">input</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">40px</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">outline</span>: none;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#c4c4c4</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span> <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">2px</span> <span class="hljs-number">2px</span> <span class="hljs-number">4px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.1</span>);
}
</code></pre>
<p>Add this code to add some space and color changing effects:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">form</span> <span class="hljs-selector-tag">div</span> {
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">15px</span>;
}

<span class="hljs-selector-tag">input</span><span class="hljs-selector-pseudo">:focus</span> {
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#f2796e</span>;
}
</code></pre>
<p>The result so far 👇👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/fdfdfdfdfvdfv.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result so far</strong></em></p>
<h2 id="heading-how-to-style-the-icons">How to Style the Icons</h2>
<p>Now we're gonna style the icons which we imported from font-awesome. Follow along with the code: ✨✨</p>
<pre><code class="lang-css"><span class="hljs-comment">/**
* ! style rules for form icons
**/</span>

<span class="hljs-selector-tag">form</span> <span class="hljs-selector-tag">div</span> <span class="hljs-selector-tag">i</span> {
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
}
</code></pre>
<p>Here's the result of adding those two lines 👇👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/fddfvdfvdfvgfbh.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result so far</strong></em></p>
<p>Now, add these styles to style the error class, along with the success and failure icons 👇👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.failure-icon</span>,
<span class="hljs-selector-class">.error</span> {
  <span class="hljs-attribute">color</span>: red;
}

<span class="hljs-selector-class">.success-icon</span> {
  <span class="hljs-attribute">color</span>: green;
}

<span class="hljs-selector-class">.error</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">14.5px</span>;
  <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">5px</span>;
}
</code></pre>
<p>Here's the result so far 👇👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ddsfsddsdscsfvv.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result so far</strong></em></p>
<p>Look, the success and failure icons are overlapping each other. Don't worry, we will manipulate those in JavaScript. For now, you can hide them like this👇👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.success-icon</span>,
<span class="hljs-selector-class">.failure-icon</span> {
  <span class="hljs-attribute">right</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0</span>;
}
</code></pre>
<p>Now, let's style our submit button, like this 👇</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Style rules for submit btn */</span>

<span class="hljs-selector-tag">button</span> {
  <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">15px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">45px</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#f2796e</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#f2796e</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#fff</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">cursor</span>: pointer;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">2px</span> <span class="hljs-number">2px</span> <span class="hljs-number">4px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.1</span>);
  <span class="hljs-attribute">transition</span>: all <span class="hljs-number">0.1s</span> ease;
}
</code></pre>
<p>If you want to add a hover effect, then sure, add these styles 👇👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">button</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">opacity</span>: <span class="hljs-number">0.8</span>;
}
</code></pre>
<h1 id="heading-take-a-break">Take a Break!</h1>
<p>So far so good. Take a break – you deserve it.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-33.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-how-to-add-the-javascript">How to Add the JavaScript</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-6.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>First, we need to target all our classes and id from the HTML inside the JavaScript. To do this task efficiently, make these two functions 👇👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">let</span> <span class="hljs-selector-tag">id</span> = (<span class="hljs-selector-tag">id</span>) =&gt; <span class="hljs-selector-tag">document</span><span class="hljs-selector-class">.getElementById</span>(<span class="hljs-selector-tag">id</span>);

<span class="hljs-selector-tag">let</span> <span class="hljs-selector-tag">classes</span> = (<span class="hljs-selector-tag">classes</span>) =&gt; <span class="hljs-selector-tag">document</span><span class="hljs-selector-class">.getElementsByClassName</span>(<span class="hljs-selector-tag">classes</span>);
</code></pre>
<p>Then, store the classes and id inside these variables 👇</p>
<p><strong>Note:</strong> Try not to make spelling mistakes. Otherwise, your JavaScript will not work.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> username = id(<span class="hljs-string">"username"</span>),
  email = id(<span class="hljs-string">"email"</span>),
  password = id(<span class="hljs-string">"password"</span>),
  form = id(<span class="hljs-string">"form"</span>),

  errorMsg = classes(<span class="hljs-string">"error"</span>),
  successIcon = classes(<span class="hljs-string">"success-icon"</span>),
  failureIcon = classes(<span class="hljs-string">"failure-icon"</span>);
</code></pre>
<p>Now, we're gonna target our form and add the <strong>submit</strong> event listener 👇 </p>
<pre><code class="lang-css">
<span class="hljs-selector-tag">form</span><span class="hljs-selector-class">.addEventListener</span>("<span class="hljs-selector-tag">submit</span>", (<span class="hljs-selector-tag">e</span>) =&gt; {
  e.preventDefault();
});
</code></pre>
<p>Now, we will create a function named engine which will do all sorts of form validation work for us. It will have three arguments – follow along here: 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> engine = <span class="hljs-function">(<span class="hljs-params">id, serial, message</span>) =&gt;</span> {}
</code></pre>
<p>The arguments represent the following:</p>
<ul>
<li><code>id</code> will target our id</li>
<li><code>serial</code> will target our classes [error class, success and failure icons]</li>
<li><code>message</code> will print a message inside our .error class </li>
</ul>
<p>Now create an <code>if, else</code> statement like this 👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> engine = <span class="hljs-function">(<span class="hljs-params">id, serial, message</span>) =&gt;</span> {

  <span class="hljs-keyword">if</span> (id.value.trim() === <span class="hljs-string">""</span>) {
  } 

  <span class="hljs-keyword">else</span> {
  }
}
</code></pre>
<p><strong>Note:</strong> the <strong><code>id.value.trim()</code></strong> will remove all the extra white spaces from the value which the user inputs. You can get an idea of how it works by looking at this illustration 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-19-1.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>trim() used to remove extra spaces</strong></em></p>
<p>Now, look at our objectives 👇</p>
<ul>
<li>We want the JavaScript to print a message inside the <strong>error</strong> class whenever the user <strong>submits a blank</strong> <strong>form</strong>. At the same time, we want the <strong>failure</strong> icons to highlight as well. </li>
<li>But, if the user <strong>fills in all the inputs</strong> and submits it, we want the <strong>success</strong> icon to be visible.</li>
</ul>
<p>To accomplish this, write this logic 👇 to print the message:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> engine = <span class="hljs-function">(<span class="hljs-params">id, serial, message</span>) =&gt;</span> {

  <span class="hljs-keyword">if</span> (id.value.trim() === <span class="hljs-string">""</span>) {
    errorMsg[serial].innerHTML = message;
  } 

  <span class="hljs-keyword">else</span> {
    errorMsg[serial].innerHTML = <span class="hljs-string">""</span>;
  }
}
</code></pre>
<p>For the icons to work properly, add this code: 👇👇</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> engine = <span class="hljs-function">(<span class="hljs-params">id, serial, message</span>) =&gt;</span> {

  <span class="hljs-keyword">if</span> (id.value.trim() === <span class="hljs-string">""</span>) {
    errorMsg[serial].innerHTML = message;
    id.style.border = <span class="hljs-string">"2px solid red"</span>;

    <span class="hljs-comment">// icons</span>
    failureIcon[serial].style.opacity = <span class="hljs-string">"1"</span>;
    successIcon[serial].style.opacity = <span class="hljs-string">"0"</span>;
  } 

  <span class="hljs-keyword">else</span> {
    errorMsg[serial].innerHTML = <span class="hljs-string">""</span>;
    id.style.border = <span class="hljs-string">"2px solid green"</span>;

    <span class="hljs-comment">// icons</span>
    failureIcon[serial].style.opacity = <span class="hljs-string">"0"</span>;
    successIcon[serial].style.opacity = <span class="hljs-string">"1"</span>;
  }
}
</code></pre>
<p>Time to implement our newly created function. Write these inside the place where we added the submit event listener 👇</p>
<pre><code class="lang-js">form.addEventListener(<span class="hljs-string">"submit"</span>, <span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
  e.preventDefault();

  engine(username, <span class="hljs-number">0</span>, <span class="hljs-string">"Username cannot be blank"</span>);
  engine(email, <span class="hljs-number">1</span>, <span class="hljs-string">"Email cannot be blank"</span>);
  engine(password, <span class="hljs-number">2</span>, <span class="hljs-string">"Password cannot be blank"</span>);
});
</code></pre>
<p>Here, we are passing the id names, serials of our class names, and passing the message which should be printed when we find an error when the user submits the form. </p>
<p>Here are the results so far 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/dvdfvdf.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>The Result so far</strong></em></p>
<h2 id="heading-how-to-add-the-social-media-buttons">How to Add the Social Media Buttons</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-10.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>So far so good, let's add social media sign up options. Follow along here. 👇 </p>
<p>Inside the form tag, create a new <code>div</code> with the class name <code>social</code>:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"form"</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"social"</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"title"</span>&gt;</span>Get Started<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"question"</span>&gt;</span>
        Already Have an Account? <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Sign In<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"or"</span>&gt;</span>Or<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- other codes are here--&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<p>Inside the <code>.btn</code> class, we create two more divs with class names <code>.btn-1</code> and <code>.btn-2</code> with the images and text as well</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn-1"</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://img.icons8.com/color/30/000000/google-logo.png"</span> /&gt;</span>
     Sign Up
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn-2"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://img.icons8.com/ios-filled/30/ffffff/facebook-new.png"</span> /&gt;</span>
     Sign Up
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Here are the results so far 👇👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/dfvgdfdsfdsf.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>The Result so far</strong></em></p>
<p>Now, let's style the <code>.btn-1</code> and <code>.btn-2</code> first. We'll change the alignment of the buttons to row from column 👇</p>
<pre><code class="lang-css"><span class="hljs-comment">/**
* ! style rules for social section
**/</span>

<span class="hljs-selector-class">.btn</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;
  <span class="hljs-attribute">justify-content</span>: space-between;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">15px</span>;
}
</code></pre>
<p>Here's what it looks like now:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/dfdfdfdbgf.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>The Result so far</strong></em></p>
<p>Now, add styles for the button like this: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.btn-1</span>,
<span class="hljs-selector-class">.btn-2</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">5px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">15px</span>;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#c4c4c4</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">2px</span> <span class="hljs-number">2px</span> <span class="hljs-number">4px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.1</span>);
  <span class="hljs-attribute">cursor</span>: pointer;
}
</code></pre>
<p>Change the icon color and text color of <code>.btn-2</code> like this: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.btn-2</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#4f70b5</span>;
  <span class="hljs-attribute">color</span>: white;
}
</code></pre>
<p>Then add these small changes to make the component look better:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.or</span> {
  <span class="hljs-attribute">text-align</span>: center;
}

<span class="hljs-selector-class">.question</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">15px</span>;
}

<span class="hljs-selector-tag">span</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#f2796e</span>;
  <span class="hljs-attribute">cursor</span>: pointer;
}
</code></pre>
<p>The result so far:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/fdfhgnmhg.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result so far</strong></em></p>
<h2 id="heading-how-to-add-the-images">How to Add the Images</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-9.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now, let's add images to our project. First, let's write the HTML 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"content"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"logo"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://svgshare.com/i/_go.svg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"image"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text"</span>&gt;</span>
          Start for free &amp; get <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
          attractive offers today !
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>  
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"form"</span>&gt;</span>
   <span class="hljs-comment">&lt;!--other codes are here --&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>The result so far 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/dfghgjgjgytfh.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result so far</strong></em></p>
<p>Now we need to change the orientation of our content from column to row. Follow along 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;
}
</code></pre>
<p>Add these style rules for the content section:</p>
<pre><code class="lang-css"><span class="hljs-comment">/**
* ! style rules for content section
**/</span>

<span class="hljs-selector-class">.content</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: column;
  <span class="hljs-attribute">justify-content</span>: space-around;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#f2796e</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">55%</span>;
  <span class="hljs-attribute">min-height</span>: <span class="hljs-number">100vh</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">20px</span>;
}

<span class="hljs-selector-tag">form</span> {
   <span class="hljs-attribute">width</span>: <span class="hljs-number">45%</span>;
   <span class="hljs-attribute">max-width</span>: none;
}
</code></pre>
<p>The result so far 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/dsffgythjy.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result so far</strong></em></p>
<p>Add the main illustration in CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.image</span> {
  <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">"https://svgshare.com/i/_gZ.svg"</span>);
  <span class="hljs-attribute">background-repeat</span>: no-repeat;
  <span class="hljs-attribute">background-size</span>: contain;
  <span class="hljs-attribute">background-position</span>: center;
  <span class="hljs-comment">/* border: 2px solid black; */</span>
  <span class="hljs-attribute">height</span>: <span class="hljs-number">65%</span>;
}
</code></pre>
<p>And add these styles for the <code>.text</code> class:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">text-align</span>: center;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">18px</span>;
}

<span class="hljs-selector-tag">form</span> {
   <span class="hljs-attribute">width</span>: <span class="hljs-number">45%</span>;
   <span class="hljs-attribute">max-width</span>: none;
}
</code></pre>
<p>The result so far 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/wewrwerew.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result so far</strong></em></p>
<h2 id="heading-how-to-add-media-queries-for-the-mobile-version">How to Add Media Queries for the Mobile Version</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-8.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We want to make this responsive. So we'll add media queries to help us with that.</p>
<p>For screens with a width from 900px, we will add these styles. Follow along 👇👇</p>
<pre><code class="lang-css"><span class="hljs-keyword">@media</span> (<span class="hljs-attribute">max-width:</span> <span class="hljs-number">900px</span>) {
  <span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">flex-direction</span>: column;
  }

  <span class="hljs-selector-tag">form</span>,
  <span class="hljs-selector-class">.content</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  }

  <span class="hljs-selector-class">.btn</span> {
    <span class="hljs-attribute">flex-direction</span>: column;
  }
  <span class="hljs-selector-class">.image</span> {
    <span class="hljs-attribute">height</span>: <span class="hljs-number">70vh</span>;
  }
}
</code></pre>
<p>For screens with a width from 425px, we will make these minor changes 👇</p>
<pre><code class="lang-css"><span class="hljs-keyword">@media</span> (<span class="hljs-attribute">max-width:</span> <span class="hljs-number">425px</span>) {
  <span class="hljs-selector-tag">form</span> {
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
  }
}
</code></pre>
<p>Here's the final result 👇👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/fgbgfnghnghnhgmjhgnmhgnhgnggfbgfgfb.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>The final result</strong></em></p>
<h1 id="heading-conclusion">Conclusion</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/Frame-5.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Congratulations !</strong></em></p>
<p>Congratulations for reading until the end. Now you can easily and efficiently use JavaScript to handle form validation. Not only that, <strong>you also have a project to show your local recruiter!</strong></p>
<p>Here's your medal for reading till the end ❤️</p>
<h3 id="heading-suggestions-amp-criticisms-are-highly-appreciated">Suggestions &amp; Criticisms Are Highly Appreciated ❤️</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="1000" height="245" loading="lazy"></p>
<ul>
<li><a target="_blank" href="https://www.linkedin.com/in/joyshaheb/"><strong>LinkedIn/ JoyShaheb</strong></a></li>
<li><strong><a target="_blank" href="https://www.youtube.com/c/joyshaheb">YouTube / JoyShaheb</a></strong></li>
<li><strong><a target="_blank" href="https://twitter.com/JoyShaheb">Twitter / JoyShaheb</a></strong></li>
<li><strong><a target="_blank" href="https://www.instagram.com/joyshaheb/">Instagram/ JoyShaheb</a></strong></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn CSS Units – Em, Rem, VH, and VW with Code Examples ✨✨ ]]>
                </title>
                <description>
                    <![CDATA[ Today we're gonna learn how to use the CSS units EM, REM, VW and VH by working through some practical examples. We'll also see how to make responsive websites with these units.  Let's start. 💖 Table of Contents Why learn relative units? What are RE... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-css-units-em-rem-vh-vw-with-code-examples/</link>
                <guid isPermaLink="false">66b2096420f547d3557757a2</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ responsive design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Wed, 25 Aug 2021 15:34:28 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/08/FCC-Thumbnail.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're gonna learn how to use the CSS units EM, REM, VW and VH by working through some practical examples. We'll also see how to make responsive websites with these units. </p>
<p>Let's start. 💖</p>
<h1 id="heading-table-of-contents">Table of Contents</h1>
<ul>
<li><a class="post-section-overview" href="#heading-why-learn-css-relative-units">Why</a> learn relative units?</li>
<li><a class="post-section-overview" href="#heading-what-are-rem-units">What are REM Units?</a></li>
<li><a class="post-section-overview" href="#heading-how-to-make-responsive-websites-with-rem-units">How to make responsive websites with REM units</a></li>
<li><a class="post-section-overview" href="#heading-what-are-em-units">What are EM Units?</a></li>
<li><a class="post-section-overview" href="#heading-what-are-vw-units">What are VW</a> units?</li>
<li><a class="post-section-overview" href="#heading-what-are-vh-units">What are VH units?</a></li>
<li><a class="post-section-overview" href="#heading-additional-resources">Additional Resources</a></li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-25.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Topics covered</strong></em></p>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like"><strong>You can watch this tutorial on YouTube as well if you like:</strong></h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/6uJPTM0AaFc" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-why-learn-css-relative-units">Why Learn CSS Relative Units?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/YT-Thumbnail-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>If you want to make <strong>responsive websites</strong> very easily, quickly, and efficiently, then you should definitely learn the relative units of CSS. </p>
<p><strong>REM, EM, VW, VH are relative units</strong>. If you Combine these with media queries, then you can make perfectly scalable layouts. Look at this GIF 👇 The text is responsive on desktop, tablet, and mobile screens! </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/final-1.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Font using the REM unit</strong></em></p>
<p>Keep in mind that <strong>pixels are absolute units.</strong> They won't change when you resize the window. Look at this GIF 👇 Notice that the font size of <strong>50px doesn't change</strong> when we resize the window.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/aaaaaaaaaaa.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Font using the Pixel unit</strong></em></p>
<p>Tip: Before starting the tutorial, I suggest that you don't think of EM and REM as units. Think them as multipliers of a base number. </p>
<h1 id="heading-project-setup">Project Setup</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>First, <a target="_blank" href="https://codepen.io/joyshaheb/pen/XWMqEdV">copy the code from this Code Pen link</a> and paste it into VS Code or your code editor of choice. Then follow these steps:👇</p>
<ul>
<li>create a folder named <strong>project-1</strong></li>
<li>create HTML, CSS, JS files and link them together</li>
<li>install the plugins we'll need – <strong>px to rem</strong> and <strong>Live server</strong></li>
<li>Run live server</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/textthat.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Testing starter files</strong></em></p>
<p>As you can see in the gif above, 👆 the JavaScript is doing all the calculations, so we just need to focus on the tutorial. We will just change the CSS and experiment with different values. </p>
<p>Let's start coding!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-what-are-rem-units">What are REM Units?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/YT-Thumbnail--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The REM unit depends on the <strong>root element</strong> [the <strong>HTML</strong> element]. Here's an image to show you how it works:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/YT-Thumbnail--5-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Default font size of root element</strong></em></p>
<p>The default font-size of the root element [in HTML] is 16px. So, 1 REM = 16px. </p>
<p>If we write 3 rem, it will show us <strong>[ 3*16px = 48px ]</strong>. So as you can see, it works like a multiplier.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/YT-Thumbnail--7-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>experimenting with 3 rem</strong></em></p>
<p>But, if we change the root element font size, the REM unit changes – like this: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/YT-Thumbnail--6-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>changed font size of root element</strong></em></p>
<p>We're setting the HTML font-size to 50px. </p>
<p>Now, if we write 3 rem, it will show us <strong>[ 3*50px = 150px ]</strong> like this: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/YT-Thumbnail--8-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>experimenting with 3 rem</strong></em></p>
<p>Let's recreate the results with code and see their use cases in practice. 👇</p>
<p>First, let's experiment with the default font-size of every website, which is 16 pixels. And we'll set the <code>.text</code> class font-size to 1 rem.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">html</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
}

<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1rem</span>;
}

<span class="hljs-comment">/** Calculations 
 1 rem * 16px = 16px
**/</span>
</code></pre>
<p>Here's what the result looks like:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--6-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Font-size is 1rem, root is 16px</strong></em></p>
<p>Now, let's increase the <code>.text</code> font-size to 2 rem:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">html</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
}

<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
}

<span class="hljs-comment">/** Calculations
 2 rem * 16px = 32px
**/</span>
</code></pre>
<p>And here's the result: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--5-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Font-size is 2 rem, root is 16px</strong></em></p>
<p>As you can see, the font size gets bigger but the width stays the same (1536px).</p>
<h3 id="heading-how-to-change-the-root-font-size">How to change the root font-size</h3>
<p>Now, let's experiment by changing the root font-size, which is inside the html. First write this code to get the default result: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">html</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
}

<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1rem</span>;
}

<span class="hljs-comment">/** Calculations
 1 rem * 16px = 16px
**/</span>
</code></pre>
<p>Here's what that looks like:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--6--1.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Default setting</strong></em></p>
<p>Now, change the root font-size to 40px like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">html</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
}

<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1rem</span>;
}

<span class="hljs-comment">/** Calculations
 1 rem * 40px = 40px
**/</span>
</code></pre>
<p>Here's the result:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--4-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>root element is 40px</strong></em></p>
<p>Now, change the <code>.text</code> font size to 2 rem: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">html</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
}

<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
}

<span class="hljs-comment">/** Calculations
 2 rem * 40px = 80px
**/</span>
</code></pre>
<p>And you can see the result: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--17-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>The result</strong></em></p>
<p>Since we changed the root font size to 40px, when we change the .text font size to 2 rem, we get 2*40 = 80px. </p>
<h1 id="heading-how-to-make-responsive-websites-with-rem-units">How to Make Responsive Websites with REM Units</h1>
<p>Making responsive websites with the REM unit is very easy. Just write your styles in <strong>rem units</strong> instead of the pixels and change the root elements at different breakpoints using media queries.</p>
<p>Here's a demo that shows you how it's done👇 and how to add the media queries:</p>
<pre><code class="lang-css">// <span class="hljs-selector-tag">large</span> <span class="hljs-selector-tag">screen</span> 

<span class="hljs-keyword">@media</span> (<span class="hljs-attribute">max-width:</span> <span class="hljs-number">1400px</span>) {
  <span class="hljs-selector-tag">html</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
  }
}

// <span class="hljs-selector-tag">Tablet</span> <span class="hljs-selector-tag">screen</span> 

<span class="hljs-keyword">@media</span> (<span class="hljs-attribute">max-width:</span> <span class="hljs-number">768px</span>) {
  <span class="hljs-selector-tag">html</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">18px</span>;
  }
}

// <span class="hljs-selector-tag">Mobile</span> <span class="hljs-selector-tag">screen</span> 

<span class="hljs-keyword">@media</span> (<span class="hljs-attribute">max-width:</span> <span class="hljs-number">450px</span>) {
  <span class="hljs-selector-tag">html</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">12px</span>;
  }
}
</code></pre>
<p>Now, set the <strong>.text</strong> class to 3 rem units, like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.text</span>{
    <span class="hljs-attribute">font-size </span>: <span class="hljs-number">3rem</span>;
}
</code></pre>
<p>And here's the result: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/final-1.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h4 id="heading-here-are-the-calculations">Here are the calculations:</h4>
<ul>
<li>For the large screen  -&gt; 3 rem * 25px = 75px</li>
<li>For tablet screen        -&gt; 3 rem * 18px = 54px</li>
<li>For mobile screen      -&gt; 3 rem  * 12px = 36px</li>
<li>Default setting            -&gt; 3rem * 16px = 48px</li>
</ul>
<h1 id="heading-what-are-em-units">What are EM Units?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/YT-Thumbnail--2-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The EM unit is the same as the REM unit but it depends on the <strong>parent font size</strong>. Here's a demo. 👇</p>
<p><strong>Note</strong>: make sure you remove all the media queries.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">html</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
}

<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">3em</span>;
}

<span class="hljs-comment">/** Calculations
  font-size should be 
  3 em * 16px = 48px
**/</span>
</code></pre>
<p>Here's the result: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--8-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now, let's try adding <strong>3em padding</strong> to the .text class.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">html</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
}

<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">3em</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">3em</span>;
}

<span class="hljs-comment">/** Calculations
text    =&gt; 3em * 16px = 48px
padding =&gt; 3em * 3em * 16px = 144px
**/</span>
</code></pre>
<p>Instead of being 48px of padding, <strong>we are getting 144px padding</strong>. As you can see, it is getting <strong>multiplied</strong> by the previous number. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--7-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>result of 3em padding</strong></em></p>
<p>Here's the computed part from the developer console: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/ss.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>3em padding to our text</strong></em></p>
<h3 id="heading-dont-use-the-em-unit">Don't use the EM unit 😵❌</h3>
<p>Using the EM unit is <strong>not worth the effort</strong> because:</p>
<ul>
<li>you have a high chance of making a calculation error</li>
<li>you have to write a lot of code in media queries while trying to make the website responsive on all screen sizes</li>
<li>it's too time-consuming.</li>
</ul>
<h1 id="heading-what-are-vw-units">What are VW Units?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--9--1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The full form of VW is <strong>viewport width</strong>. It works like the <strong>percentage unit.</strong> Specifying <strong>10vw</strong> is equivalent to occupying 10% of entire visible screen width.</p>
<p>To experiment with the results, make these changes in your CSS👇</p>
<p><strong>Note:</strong> comment the last line on the .box class.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">display</span>: none;
}

<span class="hljs-selector-class">.box</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">50vw</span>;

  <span class="hljs-attribute">height</span>: <span class="hljs-number">300px</span>;
  <span class="hljs-comment">/* display: none; */</span>
}
</code></pre>
<p>If you look carefully, you can see that <strong>50vw means 50%,</strong> which will cover half of the entire screen width.</p>
<p>In the JavaScript part, uncomment this line at the very end: 👇</p>
<pre><code class="lang-javascript">  <span class="hljs-comment">// Box Width &amp; height</span>

  box.innerHTML = <span class="hljs-string">"Width : "</span> + Box_width;

  <span class="hljs-comment">// box.innerHTML = "Height : " + Box_height;</span>
</code></pre>
<p>The result looks like this:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--11-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>50vw occupies 50% of screen width</strong></em></p>
<p>As you can see, that element will always cover that much space even if we resize the window</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/ttt.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>resizing box which is 50vw in size</strong></em></p>
<h1 id="heading-what-are-vh-units">What are VH Units?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--10-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The full form of VH is <strong>viewport height</strong>. It works like the <strong>percentage unit</strong> as well. Specifying <strong>10vh</strong> is equivalent to occupying 10% of entire visible screen height.</p>
<p>Look at this demo to see how it works:👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">display</span>: none;
}

<span class="hljs-selector-class">.box</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">300px</span>;

  <span class="hljs-attribute">height</span>: <span class="hljs-number">50vh</span>;
   <span class="hljs-comment">/* display: none; */</span>
}
</code></pre>
<p>If you look carefully, you can see that <strong>50vh means 50%,</strong> which will cover half of the entire screen height.</p>
<p>In the JavaScript part, uncomment this line at the very end: 👇</p>
<pre><code class="lang-javascript">  <span class="hljs-comment">// Box Width &amp; height</span>

  <span class="hljs-comment">// box.innerHTML = "Width : " + Box_width;</span>

  box.innerHTML = <span class="hljs-string">"Height : "</span> + Box_height;
</code></pre>
<p>Also, make these changes:👇</p>
<pre><code class="lang-javascript">  <span class="hljs-comment">// Screen Size (Width &amp; height)</span>

  <span class="hljs-comment">// size.innerHTML = "Width : " + Width + " px";</span>
  size.innerHTML = <span class="hljs-string">"Height : "</span> + Height + <span class="hljs-string">" px"</span>;
</code></pre>
<p>And here's the result: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/YT-Thumbnail--21-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>50vh occupies 50% of screen Height</strong></em></p>
<p>As you can see, it will always cover that much space even if we resize the window.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/gggg.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>resizing box which is 50vh in size</strong></em></p>
<p>That's it! </p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Congratulations! Now, can confidently use the REM, EM, VW, and VH units to make <strong>perfectly responsive websites.</strong></p>
<p>Here's your medal 🎖️ for successfully reading till the end. ❤️</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/yx020xpcqeh1wg30wc5c.png" alt="Alt Text" width="1000" height="245" loading="lazy"></p>
<h1 id="heading-additional-resources">Additional Resources</h1>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/learn-css-media-queries-by-building-projects/">Complete Media query tutorial</a></li>
</ul>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/HY8q4TD3KGM" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-credits">Credits</h1>
<ul>
<li>Images from <a target="_blank" href="https://www.freepik.com/user/collections/rem/2273142">Freepik</a> </li>
</ul>
<h3 id="heading-suggestions-amp-criticisms-are-highly-appreciated">Suggestions &amp; Criticisms are Highly Appreciated ❤️️</h3>
<ul>
<li><a target="_blank" href="https://www.youtube.com/c/joyshaheb"><strong>YouTube</strong> / JoyShaheb</a></li>
<li><a target="_blank" href="https://www.linkedin.com/in/joyshaheb/"><strong>LinkedIn</strong> / JoyShaheb</a></li>
<li><a target="_blank" href="https://twitter.com/JoyShaheb"><strong>Twitter</strong> / JoyShaheb</a></li>
<li><a target="_blank" href="https://www.instagram.com/joyshaheb/"><strong>Instagram</strong> / JoyShaheb</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ CSS Box Model Properties – Explained With Examples ✨ ]]>
                </title>
                <description>
                    <![CDATA[ Today we're gonna learn how to use the CSS box model with examples. This will help you make pixel perfect websites and will teach you to use the box-sizing, margin, padding, and border properties more accurately.  We're also going to see some practic... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/css-box-model-explained-with-examples/</link>
                <guid isPermaLink="false">66b20949f31aa965000e5883</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Thu, 22 Jul 2021 17:31:57 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/07/FCC-Thumbnnail--2--1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're gonna learn how to use the <strong>CSS box model</strong> with examples. This will help you make pixel perfect websites and will teach you to use the box-sizing, margin, padding, and border properties more accurately. </p>
<p>We're also going to see some practical use cases for these properties. Let's get started 💖</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><strong><a class="post-section-overview" href="#heading-why-learn-css-box-model">Why learn CSS Box Model?</a></strong></li>
<li><a class="post-section-overview" href="#heading-css-box-model-diagram">CSS Box-Model Diagram</a></li>
<li><a class="post-section-overview" href="#heading-the-padding-property">The Padding Property</a></li>
<li><a class="post-section-overview" href="#heading-the-border-property">The Border Property</a></li>
<li><a class="post-section-overview" href="#heading-the-margin-property">The Margin  Property</a></li>
<li><a class="post-section-overview" href="#heading-the-box-sizing-property">The <strong>box-sizing</strong> Property.</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-difference-between-content-box-and-border-box-in-css">Content-box <strong>VS</strong> Border-box</a></li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-7--2-.png" alt="Topics covered: box model diagram, padding, border, margin, box-sizing, and shorthands" width="600" height="400" loading="lazy">
<em><strong>Topics covered</strong></em></p>
<h3 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like">You can watch this tutorial on YouTube as well if you like:</h3>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/WJ8Yoi04XvQ" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-why-learn-css-box-model">Why learn CSS Box Model?</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/YT-Thumbnail.png" alt="Why learn CSS box model?" width="600" height="400" loading="lazy"></p>
<p>The CSS box model comprises the <strong>box-sizing, padding</strong> and <strong>margin</strong> properties. If you <strong>don't</strong> use them, your website will look like this 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Page-1-1.png" alt="A website with no margin or padding" width="600" height="400" loading="lazy">
<em><strong>A website with no margin or padding</strong></em></p>
<p>But if you use the box model properties correctly, your website will look like this 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Page-1--1-.png" alt="Same image of website with padding and good use of other box model properties" width="600" height="400" loading="lazy">
<em><strong>A website using box model properties</strong></em></p>
<p>Much more visually appealing, right? If you want to make your website with accurate calculations, like the one above 👆 then this topic is for you. Learning about the CSS box model is one of many ways that will help you make <strong>pixel perfect websites.</strong></p>
<p>This article will talk about how to use these properties:</p>
<ul>
<li>Padding</li>
<li>Margin</li>
<li>Border</li>
<li>box-sizing</li>
</ul>
<h2 id="heading-how-to-use-css-box-model-properties">How to Use CSS box-model Properties</h2>
<p>Let's look at some examples of where we can use the properties of the CSS box-model. We're gonna dissect the website shown above. 👆</p>
<p>Let's have a closer look at the <strong>navbar</strong>. You can notice the difference between the example that uses the padding property and the one that doesn't: </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-47.png" alt="Before and after of a navbar with and without padding" width="600" height="400" loading="lazy">
<em><strong>Navbar items using the padding property</strong></em></p>
<p>Now let's have a closer look to the <strong>content section along with the buttons</strong>. Again, you'll notice the difference – the right one is also using the <strong>padding</strong> property. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-48.png" alt="Before and after of content with and without padding" width="600" height="400" loading="lazy">
<em><strong>A content section using the padding property</strong></em></p>
<h2 id="heading-css-box-model-diagram">CSS Box-Model Diagram</h2>
<p>Think of the CSS box-model like an <strong>onion</strong>. It has <strong>4 Layers</strong>:</p>
<ul>
<li><strong>1st</strong> layer: Content</li>
<li><strong>2nd</strong> layer: Padding</li>
<li><strong>3rd</strong> layer: Border</li>
<li><strong>4th</strong> layer: Margin</li>
</ul>
<h3 id="heading-1st-box-model-layer-content">1st box-model layer: Content</h3>
<p>In HTML, <strong>everything behaves like a box</strong>. Let's insert some content with a kitty image. 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-1--1--1.png" alt="Cute cat image to demonstrate content within the box model" width="600" height="400" loading="lazy">
<em><strong>1st layer of the box model: content</strong></em></p>
<h3 id="heading-2nd-box-model-layer-padding">2nd box-model layer: Padding</h3>
<p>The next layer of the CSS box model is the <strong>padding</strong> layer. It wraps our content like this 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-2-2.png" alt="Same cute cat image above with padding around it" width="600" height="400" loading="lazy">
<em><strong>2nd layer of the box model: padding</strong></em></p>
<h3 id="heading-3rd-box-model-layer-border">3rd box-model layer: Border</h3>
<p>The next layer of the CSS box model is the <strong>border</strong> layer. It wraps our content + padding like this 👇 </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-3--1-.png" alt="A border around the cat image above" width="600" height="400" loading="lazy">
<em><strong>The black dashed line is the border</strong></em></p>
<h3 id="heading-4th-box-model-layer-margin">4th box-model layer: Margin</h3>
<p>The next and final layer of the CSS box model is the <strong>margin</strong> layer. It wraps our content + padding + border like this 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Margin.png" alt="Margin outside the cat image" width="600" height="400" loading="lazy">
<em><strong>Grey Region is The Margin</strong></em></p>
<p>Alright, let's see how these properties work in a project.</p>
<h2 id="heading-how-to-setup-the-project">How to Setup the Project</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-8.png" alt="Let's code together" width="600" height="400" loading="lazy"></p>
<p>This tutorial is <strong>good for everyone including beginners.</strong> If you want to code along, then follow these steps. </p>
<h3 id="heading-html">HTML</h3>
<p>Open VS Code or <a target="_blank" href="http://codepen.io/">Codepen.io</a> and write this code 👇 inside the <strong>body tag:</strong></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> Box-1 <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-css">CSS</h3>
<p>Clear the default styles of our browser 👇</p>
<pre><code class="lang-css">* {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">font-family</span>: sans-serif;
}
</code></pre>
<p>Now, let's style our box 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">300px</span>;
  <span class="hljs-attribute">background-color</span>: skyblue;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">50px</span>;
}
</code></pre>
<p>We're all set, let's start coding! ✨</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-9.png" alt="Dog drinking a bubble tea" width="600" height="400" loading="lazy"></p>
<h2 id="heading-the-padding-property">The Padding Property</h2>
<p>But first, let's discuss the <strong>practical uses</strong> of the padding property. Then, we'll see how to use this property.</p>
<p>Generally, I use padding to put some space between contents. Look at this <strong>navbar</strong> 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-47-1.png" alt="Navbar with padding" width="600" height="400" loading="lazy">
<em><strong>Navbar items using padding property</strong></em></p>
<p>Here's another example for you – look at the below content, with two buttons👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-48-1.png" alt="Content with padding" width="600" height="400" loading="lazy">
<em><strong>content section using padding property</strong></em></p>
<h3 id="heading-how-to-use-the-padding-property-in-css">How to use the padding property in CSS</h3>
<p>This is the <strong>shorthand</strong> of the four padding properties:</p>
<ul>
<li>padding-top</li>
<li>padding-right</li>
<li>padding-bottom</li>
<li>padding-left</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-10.png" alt="Padding shorthand" width="600" height="400" loading="lazy">
<em><strong>Shorthand of padding property</strong></em></p>
<p>And remember, padding is the space you add on top of your <strong>main content</strong>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Padding.png" alt="Cat image showing padding" width="600" height="400" loading="lazy">
<em><strong>2nd Layer of Box-Model : Padding</strong></em></p>
<p>Let's add some padding to our content. <strong>The red colored area is the padding 👇</strong></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/bmnmmmmm.gif" alt="The red colored area is padding" width="600" height="400" loading="lazy">
<em><strong>The red colored area is padding</strong></em></p>
<p>In order to recreate the results above, ☝ write this code in your CSS: 👇</p>
<pre><code class="lang-css">// <span class="hljs-selector-tag">Padding</span> <span class="hljs-selector-tag">added</span> <span class="hljs-selector-tag">on</span> <span class="hljs-selector-tag">top</span>, <span class="hljs-selector-tag">right</span>, <span class="hljs-selector-tag">left</span>, <span class="hljs-selector-tag">bottom</span> <span class="hljs-selector-tag">of</span> <span class="hljs-selector-class">.box-1</span>

<span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">padding </span>: <span class="hljs-number">100px</span>;
}
</code></pre>
<p>Let's open our developer console and <strong>go to the computed section</strong>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/a.png" alt="Dev console image of the box model and padding" width="600" height="400" loading="lazy">
<em><strong>Computed CSS Box model</strong></em></p>
<p>At the very middle is our <strong>content</strong> which is <strong>300px</strong> in width. Look around our content, we have added <strong>100px padding</strong> all around it. </p>
<p>Let's try adding padding to just 1 side of our content (only the right side):</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-11--1-.png" alt="Image showing padding-right" width="600" height="400" loading="lazy">
<em><strong>padding-right property</strong></em></p>
<p>In order to recreate the results above, ☝ write this code in your CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span> <span class="hljs-number">100px</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>;
}

// <span class="hljs-selector-tag">Or</span> <span class="hljs-selector-tag">you</span> <span class="hljs-selector-tag">can</span> <span class="hljs-selector-tag">use</span> 👇

<span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">padding-right</span>: <span class="hljs-number">100px</span>;
}
</code></pre>
<p>Now, open the computed section on your developer console 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/s.png" alt="Dev console image showing padding-right" width="600" height="400" loading="lazy">
<em><strong>Computed CSS Box model</strong></em></p>
<p>Look – the padding of 100px has only been added on the <strong>right</strong> side of our content as we specified. </p>
<h2 id="heading-the-border-property">The Border Property</h2>
<p>You'll commonly use the border property <strong>while making buttons</strong>. Here's a GIF demo 👇</p>
<p><img src="https://media.giphy.com/media/iUTNdCt5RVTXlD7ARq/giphy.gif" alt="Image showing showing hovering a mouse over buttons to demonstrate the border property" width="600" height="400" loading="lazy">
<em><strong>Buttons using the border property</strong></em></p>
<p>Notice how a <strong>white colored border</strong> shows around the button when I hover the mouse over the button.</p>
<h3 id="heading-how-to-use-the-border-property-in-css">How to use the border property in CSS</h3>
<p>And remember, the <strong>border</strong> is the space added on top of our <strong>main content + padding</strong>: <strong>👇</strong></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Border.png" alt="Cat image with black dashed line is the border" width="600" height="400" loading="lazy">
<em><strong>The black dashed line is the border</strong></em></p>
<p>There are three crucial <strong>inputs</strong> of the border property:</p>
<ul>
<li>border size</li>
<li>border style : <strong>solid / dotted/ dashed</strong></li>
<li>border color</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-23.png" alt="Border property syntax" width="600" height="400" loading="lazy">
<em><strong>Border property syntax</strong></em></p>
<p>There are three styles of border property as I listed above. In this example, we'll use the <strong>dashed</strong> style:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-22.png" alt="A box with content, padding, and a black dashed line as a border" width="600" height="400" loading="lazy"></p>
<p>To recreate the results above, write this code in your CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">300px</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">10px</span> dashed black;
}
</code></pre>
<p>Let's open our console and see the box model calculations:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/dxcxcvbxc-1.png" alt="Image of the computed box model in the dev console" width="600" height="400" loading="lazy">
<em><strong>Computed CSS box model</strong></em></p>
<p>Now look at the above image☝ – a 10px border is added all around our <strong>content + padding</strong>.</p>
<h2 id="heading-the-margin-property">The Margin Property</h2>
<p>Generally, I use the <strong>margin</strong> property to put some <strong>whitespace</strong> between my content and the main screen on the desktop layout (large screens). Look at this GIF: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/rea.gif" alt="Adding margin to a website" width="600" height="400" loading="lazy">
<em><strong>Adding margin to a website</strong></em></p>
<p>Notice that I'm adding the margin to the left and right edges of my website above 👆 </p>
<p>Here's another sample GIF of <strong>a use case</strong> of the margin property: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/reammmmm.gif" alt="Adding margin to a website" width="600" height="400" loading="lazy">
<em><strong>Adding margin to a website</strong></em></p>
<h3 id="heading-how-to-use-margin-property-in-css">How to use margin property in CSS</h3>
<p>This is the <strong>shorthand</strong> for the four properties of the margin property:</p>
<ul>
<li>margin-top</li>
<li>margin-right</li>
<li>margin-bottom</li>
<li>margin-left</li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-12.png" alt="Shorthand of the margin property" width="600" height="400" loading="lazy">
<em><strong>Shorthand of the margin property</strong></em></p>
<p>And remember, <strong>margin</strong> is the space added on top of our <strong>main content + padding + border</strong>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Margin-1.png" alt="Cat image with a grey margin" width="600" height="400" loading="lazy">
<em><strong>The grey region is the margin</strong></em></p>
<p>Let's add a margin to our content. <strong>The content is getting pushed due to the Margin</strong> in this GIF:<strong>👇</strong></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/agid.gif" alt="Content getting pushed due to margin" width="600" height="400" loading="lazy">
<em><strong>Content getting pushed due to margin</strong></em></p>
<p>To recreate the results above, write this code in your CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">10px</span> dashed black;

  <span class="hljs-attribute">margin</span>: <span class="hljs-number">50px</span>;
}
</code></pre>
<p>We can check the calculations again: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/klkjkj.png" alt="Dev console image showing a margin" width="600" height="400" loading="lazy">
<em><strong>Computed CSS box model</strong></em></p>
<p>Look, a 50px margin has been added all around our <strong>content + padding + border</strong>.</p>
<p>Let's try adding a <strong>margin</strong> to just 1 side of our content (only the left side):</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-22--2-.png" alt="The margin-left property" width="600" height="400" loading="lazy">
<em><strong>The margin-left property</strong></em></p>
<p>To recreate the results above, write this code in your CSS 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">10px</span> dashed black;

  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">50px</span>;
}
</code></pre>
<p>On the console, we can see that a <strong>50px margin</strong> got applied only on the <strong>left side</strong> 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/vbnvbnbnbv.png" alt="Dev console image showing the margin-left property" width="600" height="400" loading="lazy">
<em><strong>Computed CSS Box Model</strong></em></p>
<h2 id="heading-take-a-break">Take a Break!</h2>
<p>So far so good – take a break! You deserve it 💖</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-24--1-.png" alt="Dog drinking a bubble tea" width="600" height="400" loading="lazy"></p>
<h2 id="heading-the-box-sizing-property">The Box-sizing Property</h2>
<p>This property defines how our margin, padding, and borders will be calculated. There are three types of calculations (you can call them values):</p>
<ul>
<li>border-box</li>
<li>padding-box</li>
<li>content box</li>
</ul>
<h3 id="heading-note">Note:</h3>
<p>We're not gonna discus <strong>box-sizing: padding-box,</strong> as only Firefox supports it and it isn't used very often.</p>
<h2 id="heading-what-is-the-difference-between-content-box-and-border-box-in-css">What is the difference between content-box and border-box in CSS?</h2>
<p>Both border-box and content-box work in the same way. Look at these images:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-15.png" alt="Boxes using the border-box value" width="600" height="400" loading="lazy">
<em><strong>Boxes using the border-box value</strong></em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-17.png" alt="Boxes using the content-box value" width="600" height="400" loading="lazy">
<em><strong>Boxes using the content-box value</strong></em></p>
<p>So, what's the main difference here? The difference is noticeable when we are adding margin, border, or padding to our boxes. </p>
<p>When we are using the <strong>box-sizing: content-box</strong>, which is the default value, it will <strong>add</strong> a margin, padding, and borders <strong>outside the box</strong>, like this: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/abcdefg.gif" alt="Padding, getting applied outside the box" width="600" height="400" loading="lazy">
<em><strong>Padding getting applied outside the box</strong></em></p>
<p>You can see the calculations here as well: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/Frame-19.png" alt="Calculations with content-box" width="600" height="400" loading="lazy">
<em><strong>Calculations with content-box</strong></em></p>
<p>Which means that things can get out of control and you'll see unexpected calculations. This means that it's <strong>hard to make responsive websites.</strong> Always use the <strong>box-sizing: border-box</strong> property instead.</p>
<p>But when we are using the <strong>box-sizing: border-box</strong> property, it will <strong>add</strong> a margin, padding, and borders <strong>inside the box</strong>, like this:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/07/c-box.gif" alt="Padding getting applied inside the box" width="600" height="400" loading="lazy">
<em><strong>Padding getting applied inside the box</strong></em></p>
<p>The <strong>box-sizing: border-box</strong> property shows us the <strong>EXACT</strong> calculations of our HTML elements, which means that this value is <strong>ideal for making responsive websites.</strong></p>
<p>You can experiment with the values as well – just follow along with this code: 👇</p>
<pre><code class="lang-css">* {
  <span class="hljs-attribute">box-sizing</span>: border-box;
}

<span class="hljs-comment">/* Or, Write 👇 */</span>

* {
  <span class="hljs-attribute">box-sizing</span>: content-box;
}
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Congratulations! You can now make <strong>pixel perfect websites.</strong> Not only that, but when you're coding, you can figure out why your content is behaving strangely.</p>
<p>Here's your medal for reading till the end ❤️</p>
<h2 id="heading-suggestions-amp-criticisms-are-highly-appreciated">Suggestions &amp; Criticisms Are Highly Appreciated ❤️</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Clapping hands and a gold medal" width="600" height="400" loading="lazy"></p>
<p><strong>YouTube <a target="_blank" href="https://youtube.com/c/joyshaheb">/ Joy Shaheb</a></strong></p>
<p><strong>LinkedIn <a target="_blank" href="https://www.linkedin.com/in/joyshaheb/">/ JoyShaheb</a></strong></p>
<p><strong>Twitter <a target="_blank" href="https://twitter.com/JoyShaheb">/ JoyShaheb</a></strong></p>
<p><strong>Instagram <a target="_blank" href="https://www.instagram.com/joyshaheb/">/ JoyShaheb</a></strong></p>
<h2 id="heading-credits">Credits</h2>
<ul>
<li><a target="_blank" href="https://www.freepik.com/collection/css-box-model-vectors/2187534">Images from Freepik</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn the CSS Box-Shadow Property by Coding a Beautiful Button ✨ ]]>
                </title>
                <description>
                    <![CDATA[ Today we're gonna learn how to use CSS's box-shadow property to make beautiful website components. Along the way, we'll create a button and get hands-on experience using this property. Let's get started. 🎖️ Table of Contents Why you should use the ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/css-box-shadow-property-with-examples/</link>
                <guid isPermaLink="false">66b2094ca5be9a107f4341c5</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ css properties ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Wed, 30 Jun 2021 18:49:49 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/06/FCC-Thumbnail.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're gonna learn how to use CSS's <strong>box-shadow</strong> property to make beautiful website components. Along the way, we'll <strong>create a button</strong> and get hands-on experience using this property. Let's get started. 🎖️</p>
<h1 id="heading-table-of-contents">Table of Contents</h1>
<ul>
<li><a class="post-section-overview" href="#heading-why-should-you-use-the-css-box-shadow-property"><strong>Why</strong> you should use the CSS box-shadow property</a></li>
<li><a class="post-section-overview" href="#heading-the-syntax-of-the-box-shadow-property">The <strong>syntax</strong> of the box-shadow property</a></li>
<li><a class="post-section-overview" href="#heading-how-to-add-a-drop-shadow-to-a-button">How to make a <strong>button</strong> using the box-shadow property</a></li>
<li><a class="post-section-overview" href="#heading-additional-resources">Additional Resources</a> </li>
<li><a class="post-section-overview" href="#heading-what-is-inset-in-the-css-box-shadow-property">What is <strong>inset</strong> in</a> CSS <strong>Box shadow property</strong>?</li>
</ul>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like"><strong>You can watch this tutorial on YouTube as well if you like:</strong></h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/4Clc-Bb5sY4" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-why-should-you-use-the-css-box-shadow-property">Why Should You Use The CSS box-shadow Property?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><strong>Attention to small details</strong> separates a good website from an excellent looking website. If you want to add those small details to your website, you should definitely use this property along with many other properties.</p>
<p>Let's took at some examples. 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Page-1--1-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>A Website Design</strong></em></p>
<p>Pay close attention to the button components in the image above. You'll see that we have some drop shadows. ☝</p>
<p>Let's examine these buttons even further: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-27.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Button with no box-shadow property</strong></em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-28.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Button using the box-shadow property</strong></em></p>
<p>You can tell that the latter one looks more dynamic and interesting, as it has more <strong>attention to detail.</strong> This is called a <strong>drop shadow effect</strong>. Let's see how we can implement it in our code.</p>
<h1 id="heading-project-setup"><strong>Project Setup</strong></h1>
<h3 id="heading-html">HTML</h3>
<p>Write this code inside the body tag: 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> A Button <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-css">CSS</h3>
<p>Clear your default browser settings like this:</p>
<pre><code class="lang-css">*{
   <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span>;
   <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span>;
   <span class="hljs-attribute">box-sizing</span>: border-box;
   <span class="hljs-attribute">font-family</span>: sans-serif;
}
</code></pre>
<p>Now, let's create a button with the following code:👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">margin</span>: <span class="hljs-number">100px</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">100px</span>;
   <span class="hljs-attribute">height</span>: <span class="hljs-number">80px</span>;
   <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
   <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
   <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
   <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;

   <span class="hljs-attribute">display</span>: grid;
   <span class="hljs-attribute">place-content</span>: center;
}
</code></pre>
<p>We're all set, now let's start coding!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-the-syntax-of-the-box-shadow-property">The Syntax of the box-shadow Property</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-3-1.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>All the inputs of box-shadow property</strong></em></p>
<p>Here's the syntax for the box-shadow property: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">box-shadow</span>: <span class="hljs-selector-tag">offset-x</span> | <span class="hljs-selector-tag">offset-y</span> | <span class="hljs-selector-tag">blur-radius</span> | <span class="hljs-selector-tag">spread-radius</span> | <span class="hljs-selector-tag">color</span> ;
</code></pre>
<p>Let's look at each part in more detail.</p>
<h2 id="heading-how-to-use-offset-x-in-the-box-shadow-property">How to Use Offset-x in the box-shadow Property</h2>
<p>You'll use the offset-x property to move the shadow left and right along the X-Axis. Here's a demo to show you how that looks:👇</p>
<p><img src="https://media.giphy.com/media/Mzxh8CdUTaxgzzj9ml/giphy.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>We can move the shadow left &amp; right</strong></em></p>
<p>The recreate these results, write the following code in your CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-comment">/* offset-x | offset-y | color */</span>
<span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">box-shadow</span>: -<span class="hljs-number">50px</span> <span class="hljs-number">0px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0.5</span>);
}

<span class="hljs-comment">/*Or, you can write*/</span>

<span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">50px</span> <span class="hljs-number">0px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0.5</span>);
}
</code></pre>
<h2 id="heading-how-to-use-offset-y-in-the-box-shadow-property">How to Use Offset-y in the box-shadow Property</h2>
<p>You'll use the offset-y property to move the shadow up and down along the Y-Axis. Here's a demo of how that looks:👇</p>
<p><img src="https://media.giphy.com/media/Ss9Qnrq9PFBpAfVLk8/giphy.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>We can move the shadow top &amp; bottom</strong></em></p>
<p>To recreate these results write the following in your CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-comment">/* offset-x | offset-y | color */</span>
<span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0px</span> -<span class="hljs-number">50px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0.5</span>);
}

<span class="hljs-comment">/*Or, you can write*/</span>

<span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0px</span> -<span class="hljs-number">50px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0.5</span>);
}
</code></pre>
<h3 id="heading-how-to-combine-both-offset-x-and-offset-y">How to Combine Both offset-x and offset-y</h3>
<p>Write the following code in your CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">10px</span> <span class="hljs-number">10px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0.5</span>);
}
</code></pre>
<p>Here's the result with the box shadow showing on the right and bottom of the button: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-6-1.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Our button with box shadow</strong></em></p>
<h2 id="heading-how-to-use-blur-radius-in-the-box-shadow-property">How to Use blur-radius in the box-shadow Property</h2>
<p>The blur-radius property will blur <strong>the color</strong> around our button, like this:👇</p>
<p><img src="https://media.giphy.com/media/5fRA7jzOwtmXnT57Ne/giphy.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Experimenting w/ blur radius</strong></em></p>
<p>To duplicate the results, write the following in your CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-comment">/* offset-x | offset-y | blur-radius | color */</span>

<span class="hljs-selector-class">.box-1</span>{
<span class="hljs-comment">/* play around with 👇 this */</span>
   <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">50px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0.8</span>);
}
</code></pre>
<h2 id="heading-how-to-use-spread-radius-in-the-box-shadow-property">How to Use spread-radius in the box-shadow Property</h2>
<p>This value spreads our shadow around our button, like this: 👇</p>
<p><img src="https://media.giphy.com/media/FfVw2vxOonQAjkFc7B/giphy.gif" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Experimenting w/ spread radius</strong></em></p>
<p>Let's recreate the results with the following CSS code:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* offset-x | offset-y | blur-radius | spread-radius | color */</span>

<span class="hljs-selector-class">.box-1</span>{
<span class="hljs-comment">/*  play around with 👇 this */</span>
   <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">50px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0.5</span>);
}
</code></pre>
<h1 id="heading-how-to-add-a-drop-shadow-to-a-button">How to Add a Drop Shadow to a Button</h1>
<p>Let's put together what we've learned so far and add a drop shadow effect to our button: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">8px</span> <span class="hljs-number">10px</span> <span class="hljs-number">10px</span> <span class="hljs-number">1px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0.5</span>);
}
</code></pre>
<p>The result looks like this: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-6--1--1.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>The result</strong></em></p>
<h1 id="heading-additional-resources">Additional Resources</h1>
<ul>
<li>[<a target="_blank" href="https://getcssscan.com/css-box-shadow-examples">GetCssScan</a>] - To get readymade box shadows</li>
<li>[<a target="_blank" href="https://keyframes.app/animate/">keyframes.app</a>] - to test and practice these properties in real time</li>
<li><a target="_blank" href="https://flatuicolors.com/">flatuicolors</a> - Beautiful color palettes</li>
</ul>
<h2 id="heading-bonus-tip">✨ Bonus Tip ✨</h2>
<h1 id="heading-what-is-inset-in-the-css-box-shadow-property">What is Inset in the CSS box-shadow Property?</h1>
<p>There's a keyword named <code>inset</code> that you can use with the box-shadow property. This puts the shadow inside our button instead of spreading it around the outside. Write this CSS code to experiment with it:👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">box-shadow</span>: inset <span class="hljs-number">8px</span> <span class="hljs-number">10px</span> <span class="hljs-number">10px</span> <span class="hljs-number">1px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0.5</span>);
}
</code></pre>
<p>Here's the result: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-6--2--4.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Effect of the inset keyword</strong></em></p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Now you can confidently use the box-shadow property to add not only <strong>drop shadows</strong> but also to add more <strong>attention to detail</strong> to your projects.</p>
<p>Here's your medal for reading till the end. ❤️</p>
<h3 id="heading-suggestions-and-criticisms-are-highly-appreciated">Suggestions and criticisms are highly appreciated ❤️</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="usxsz1lstuwry3jlly4d" width="600" height="400" loading="lazy"></p>
<ul>
<li><strong>YouTube<a target="_blank" href="https://youtube.com/c/joyshaheb"> / Joy Shaheb</a></strong></li>
<li><strong>LinkedIn<a target="_blank" href="https://www.linkedin.com/in/joyshaheb/"> / JoyShaheb</a></strong></li>
<li><strong>Twitter<a target="_blank" href="https://twitter.com/JoyShaheb"> / JoyShaheb</a></strong></li>
<li><strong>Instagram<a target="_blank" href="https://www.instagram.com/joyshaheb/"> / JoyShaheb</a></strong></li>
</ul>
<h1 id="heading-credits">Credits</h1>
<ul>
<li><a target="_blank" href="https://www.freepik.com/free-vector/young-girl-thinking-face-wondering-cartoon-illustration_11652601.htm#page=1&amp;query=worried%20illustration&amp;position=31">Young Girl</a></li>
<li><a target="_blank" href="https://www.freepik.com/free-vector/cute-cat-playing-with-box-cartoon_13747509.htm?query=happy%20illustration">Cute Kat</a>, <a target="_blank" href="https://www.freepik.com/free-vector/kawaii-cat-unicorn-character-collection_5481560.htm">Unicorn cat</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How the CSS Position Property Works – Explained with Code Examples ]]>
                </title>
                <description>
                    <![CDATA[ Today we're gonna learn everything you need to know about the CSS position property along with examples. Let's get started 🎖️ Table of contents What is CSS Position Property? What is the Static Position in CSS? What are the Relative and Absolute Po... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/css-position-property-explained/</link>
                <guid isPermaLink="false">66b20954a5be9a107f4341cb</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Mon, 21 Jun 2021 16:36:14 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/06/FCC-Thumbnail--4-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're gonna learn everything you need to know about the CSS position property along with examples. Let's get started 🎖️</p>
<h1 id="heading-table-of-contents">Table of contents</h1>
<ul>
<li><a class="post-section-overview" href="#heading-what-is-the-position-property-in-css">What is CSS Position Property?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-static-position-in-css">What is the <strong>Static Position</strong> in CSS?</a></li>
<li><a class="post-section-overview" href="#heading-what-are-the-relative-and-absolute-positions-in-css">What are the R<strong>elative and Absolute</strong> Positions in CSS?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-fixed-position-in-css">What is the <strong>Fixed P</strong>osition in CSS?</a></li>
<li><a class="post-section-overview" href="#heading-what-is-the-sticky-position-in-css">What is the <strong>Sticky P</strong>osition in CSS?</a></li>
</ul>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like"><strong>You can watch this tutorial on YouTube as well if you like:</strong></h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/yFXEur3SCGI" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-what-is-the-position-property-in-css">What is the position property in CSS?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-3--6-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>If you want to make <strong>stunning websites that looks artistic, unique, and beautiful,</strong> then you should definitely learn how to use the CSS position property. Let's see how it works.</p>
<p>Using <strong>Flexbox or Grid</strong>, you can make a <strong>symmetrical website</strong> like this: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-35--2-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Website made w/ Flexbox</strong></em></p>
<p>With the <strong>position property</strong>, you can make an <strong>asymmetrical website</strong> like this:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/A-1-1--2-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Website made w/ Grid &amp; position properties</strong></em></p>
<p>You can't place your content anywhere you wish using Flexbox and Grid. You're limited around the <strong>X and Y Axis</strong>. Look at this drawing to see what I mean: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-1--6-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Symmetrical content layout showing elements placed respective to the x and y axis</strong></em></p>
<p>Your boxes will follow these exact measurements. 👆</p>
<p>But, using the <strong>position property</strong>, you can place your content anywhere you wish by <strong>detaching</strong> each element from the other elements.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-2--4-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Asymmetrical content layout showing elements placed irrespective of x and y axis.</strong></em></p>
<p>You can place your boxes <strong>anywhere</strong> you wish with this sort of layout. 👆 In other words, you will have <strong>free movement</strong> around your screen.</p>
<p>Here's another example of what you can make using the position property:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-3--8-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>An Asymmetrical Website</strong></em></p>
<p>You can place or move those little dots and waves patterns and donut image all around the page <strong>☝</strong> anywhere you wish using the position property.</p>
<h1 id="heading-project-setup">Project Setup</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-4--7-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>For this project, you can use any code editor that has the <strong>emmet plugin</strong> installed. I'm gonna use <a target="_blank" href="https://codepen.io">CodePen.io</a>.</p>
<h3 id="heading-html">HTML</h3>
<p>Inside the body tag, write this code: 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-css">CSS</h3>
<p>Clear your default browser settings and add this CSS:👇</p>
<pre><code class="lang-css">*{
   <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span>;
   <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span>;
   <span class="hljs-attribute">box-sizing</span>: border-box;
}
</code></pre>
<p>Style the box-1 class like this:👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">width</span>: <span class="hljs-number">120px</span>;
   <span class="hljs-attribute">height</span>: <span class="hljs-number">120px</span>;
   <span class="hljs-attribute">background-color</span>: skyblue;
   <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
}
</code></pre>
<p>Our position property has 5 values:</p>
<ol>
<li>relative</li>
<li>absolute</li>
<li>static</li>
<li>fixed</li>
<li>sticky</li>
</ol>
<p>To move our box, we'll use 4 properties:</p>
<ul>
<li><strong>Top, Bottom</strong></li>
<li><strong>Left, Right</strong></li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-12--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-what-is-the-static-position-in-css">What is the Static Position in CSS?</h1>
<p>This has <strong>no use cases</strong>. This is the <strong>default value</strong> of every element.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-10--3-.png" alt="Image" width="600" height="400" loading="lazy">
<em>Default position of every element</em></p>
<h1 id="heading-what-are-the-relative-and-absolute-positions-in-css">What are the Relative and Absolute Positions in CSS?</h1>
<p>Both the <strong>relative position</strong> and <strong>absolute position</strong> work in the same way except in one field. We use <code>relative</code> to identify the parent class. And we use <code>absolute</code> to identify the children classes. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-11--2-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Position VS relative position</strong></em></p>
<h3 id="heading-lets-look-at-2-examples">Let's look at 2 examples 👇</h3>
<p>First, let's experiment with the <strong><code>relative</code></strong> value. Try out this code:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
<span class="hljs-comment">/* Other codes are here*/</span>

   <span class="hljs-attribute">position</span>: relative;
   <span class="hljs-attribute">left</span>: <span class="hljs-number">100px</span>;
}
</code></pre>
<p>This is the result you'll get:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-13--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We can duplicate the same result using the <strong><code>absolute</code></strong> value like this: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
<span class="hljs-comment">/* Other codes are here*/</span>

   <span class="hljs-attribute">position</span>: absolute;
   <span class="hljs-attribute">left</span>: <span class="hljs-number">100px</span>;
}
</code></pre>
<p>Let's investigate the main difference between <strong>relative and absolute</strong> positions.</p>
<h3 id="heading-relative-vs-absolute-position-in-css">Relative vs Absolute Position in CSS</h3>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/BEM-1--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-html-1">HTML</h3>
<p>Write this code inside your HTML: 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span>

       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-2"</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>    

   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre>
<h3 id="heading-css-1">CSS</h3>
<p>Style the boxes with the following CSS:👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
    <span class="hljs-attribute">width</span>: <span class="hljs-number">300px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">300px</span>;
    <span class="hljs-attribute">background-color</span>: skyblue;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
    <span class="hljs-attribute">margin</span>: auto;
}

<span class="hljs-selector-class">.box-2</span>{
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;
    <span class="hljs-attribute">height</span>:<span class="hljs-number">100px</span>;
    <span class="hljs-attribute">background-color</span>: pink;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
}
</code></pre>
<p>It should look like this:👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/dd-2.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>The result is a blue box with a smaller pink box in the upper left</strong></em></p>
<p>Now, we'll select our classes like this: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span>{ }

<span class="hljs-selector-class">.box-1</span>{ }

<span class="hljs-selector-class">.box-2</span>{ }
</code></pre>
<p>Now, write this code in your CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span>{

}

<span class="hljs-selector-class">.box-1</span>{
<span class="hljs-comment">/* This is the  👇 parent */</span>
    <span class="hljs-attribute">position</span>: relative;
}
<span class="hljs-selector-class">.box-2</span>{
<span class="hljs-comment">/* This is the  👇 child */</span>
    <span class="hljs-attribute">position</span>: absolute;
    <span class="hljs-attribute">left</span>: <span class="hljs-number">100px</span>;
}
</code></pre>
<p>Here's the result: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-14.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>The result is that the pink box has moved right 100px</strong></em></p>
<p>Notice that .box-2 has moved <strong>100px</strong> from .box-1.</p>
<p>This is because .box-1 is the <strong>parent</strong> and .box-2 is the <strong>child</strong>.</p>
<p>Let's change it again. Write this code in your CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span>{
<span class="hljs-comment">/* This is the  👇 parent */</span>
   <span class="hljs-attribute">position</span>: relative;    
}

<span class="hljs-selector-class">.box-1</span>{

}
<span class="hljs-selector-class">.box-2</span>{
<span class="hljs-comment">/* This is the  👇 child */</span>
   <span class="hljs-attribute">position</span>: absolute;
    <span class="hljs-attribute">left</span>: <span class="hljs-number">100px</span>;
}
</code></pre>
<p>Now here's the result: 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-15.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>The result is that the pink box has moved 100px from the body</strong></em></p>
<p>Notice that .box-2 has moved <strong>100px</strong> from the <strong>body</strong> element.</p>
<p>This is because the <strong>body</strong> is the <strong>parent</strong> and .box-2 is the <strong>child</strong>.</p>
<h1 id="heading-what-is-the-fixed-position-in-css">What is the Fixed Position in CSS?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-16--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This value will <strong>fix the position</strong> of your element on the screen even when you <strong>scroll</strong> in the browser. Let's look at some examples to see how it works.</p>
<h3 id="heading-fixed-position-example">Fixed position example</h3>
<p>Write this code in your HTML. 👇 Once you write <code>lorem200</code>, make sure to hit the <strong>Tab</strong> key on your keyboard:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>lorem200<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> fixed <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>lorem200<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>        

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>And here's the CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">height</span>: <span class="hljs-number">3000px</span>;
}

<span class="hljs-selector-class">.box-1</span>{
    <span class="hljs-attribute">height</span>: <span class="hljs-number">120px</span>;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">120px</span>;
    <span class="hljs-attribute">background-color</span>: skyblue;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;

    <span class="hljs-attribute">display</span>: grid;
    <span class="hljs-attribute">place-content</span>: center;
}
</code></pre>
<p>Then add this CSS at the bottom:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{

    <span class="hljs-attribute">position</span>: fixed;
    <span class="hljs-attribute">top</span>: <span class="hljs-number">100px</span>;
    <span class="hljs-attribute">left</span>: <span class="hljs-number">200px</span>;
}
</code></pre>
<p>Here's the result:👇</p>
<p><img src="https://media.giphy.com/media/J6hbBulobEQz6HftRv/giphy.gif" alt="giphy" width="600" height="400" loading="lazy"></p>
<p>You can see that the element remains fixed even when we scroll our browser.</p>
<h1 id="heading-what-is-the-sticky-position-in-css">What is the Sticky Position in CSS?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-17.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>After scrolling to a certain point on our screen, this value will <strong>fix the position</strong> of our element on the screen so it doesn't move.</p>
<h3 id="heading-sticky-position-example">Sticky position example</h3>
<p>Don't change anything in your current HTML and CSS except this one value:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
<span class="hljs-comment">/*  Play with  👇 this value */</span>
   <span class="hljs-attribute">position</span>: sticky;
   <span class="hljs-attribute">top</span>: <span class="hljs-number">30px</span>;
   <span class="hljs-attribute">left</span>: <span class="hljs-number">200px</span>;
}
</code></pre>
<p>Here's the result: 👇</p>
<p><img src="https://media.giphy.com/media/175hkevbKC3yUfiLQc/giphy.gif" alt="giphy" width="600" height="400" loading="lazy"></p>
<p>You can see that after a <strong>certain scroll point</strong>, the element remains fixed at the exact top of our browser screen.</p>
<p>You can check out these websites to see how the sticky position works on actual websites.</p>
<ul>
<li><a target="_blank" href="https://awakeboards.com/">AwakeBoards</a></li>
<li><a target="_blank" href="https://ferme.shop/">Ferme</a></li>
<li><a target="_blank" href="https://www.ascensionlatorre.com/">LATORRE</a></li>
</ul>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Now, you can confidently make beautiful websites and solve simple layout problems using the position property. </p>
<p>Here's your medal for reading till the end. ❤️</p>
<h3 id="heading-suggestions-and-criticisms-are-highly-appreciated">Suggestions and Criticisms Are Highly Appreciated ❤️</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><strong>YouTube <a target="_blank" href="https://youtube.com/c/joyshaheb">/ Joy Shaheb</a></strong></p>
<p><strong>LinkedIn <a target="_blank" href="https://www.linkedin.com/in/joyshaheb/">/ JoyShaheb</a></strong></p>
<p><strong>Twitter <a target="_blank" href="https://twitter.com/JoyShaheb">/ JoyShaheb</a></strong></p>
<p><strong>Instagram <a target="_blank" href="https://www.instagram.com/joyshaheb/">/ JoyShaheb</a></strong></p>
<h2 id="heading-credits">Credits</h2>
<ul>
<li><a target="_blank" href="https://www.freepik.com/free-vector/young-girl-holding-pile-papers-cartoon-illustration_12566300.htm">Cute Girl Illustration</a></li>
<li><a target="_blank" href="https://www.flaticon.com/packs/kitty-avatars-3">Kitty Avatar</a></li>
</ul>
<p># </p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Center Anything in CSS Using Flexbox and Grid ✨ ]]>
                </title>
                <description>
                    <![CDATA[ Today I'm gonna show you how you can center and align your content with CSS. Along the way, we'll look at various alignment techniques. So, let's get started! 🥇 Table of Contents -> How to Use Flexbox to center anything horizontally center anything... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-center-objects-using-css/</link>
                <guid isPermaLink="false">66b20957f31aa965000e5889</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS Grid ]]>
                    </category>
                
                    <category>
                        <![CDATA[ flexbox ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Fri, 11 Jun 2021 20:40:17 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/06/FCC--center.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today I'm gonna show you how you can <strong>center and align</strong> your content with CSS. Along the way, we'll look at various <strong>alignment techniques</strong>. So, let's get started! 🥇</p>
<h2 id="heading-table-of-contents-gt">Table of Contents -&gt;</h2>
<ul>
<li>How to Use <strong>Flexbox</strong> to<ul>
<li><a class="post-section-overview" href="#heading-how-to-center-anything-horizontally-using-flexbox">center anything horizontally</a></li>
<li><a class="post-section-overview" href="#heading-how-to-center-anything-vertically-using-flexbox">center anything vertically</a></li>
<li><a class="post-section-overview" href="#heading-how-to-center-a-div-horizontally-amp-vertically-using-flexbox">center both horizontally &amp; Vertically</a></li>
</ul>
</li>
<li>How to Use <strong>Grid</strong> to <ul>
<li><a class="post-section-overview" href="#heading-how-to-center-anything-horizontally-using-css-grid">center anything horizontally</a></li>
<li><a class="post-section-overview" href="#heading-how-to-center-anything-vertically-using-css-grid">center anything vertically</a></li>
<li><a class="post-section-overview" href="#heading-how-to-center-a-div-horizontally-amp-vertically-using-css-grid">center both horizontally &amp; Vertically</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-how-to-use-the-css-position-property-to-center-anything">The Transform &amp; position property</a></li>
<li><a class="post-section-overview" href="#heading-how-to-use-the-margin-property-to-center-anything">The Margin Property</a></li>
<li><a class="post-section-overview" href="#heading-additional-resources"><strong>Additional resources</strong></a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ul>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-73.png" alt="Image" width="600" height="400" loading="lazy">
<em>Methods</em></p>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like">You can watch this tutorial on YouTube as well if you like:</h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/RTEzXS_CT5w" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-but-wait-a-minute">But.... Wait A Minute!</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-35--3-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>First of all, let's understand:</p>
<ul>
<li>Main Axis </li>
<li>Cross Axis</li>
</ul>
<h2 id="heading-what-is-the-main-axis-in-css">What is the Main Axis in CSS?</h2>
<p>You can also call it the:</p>
<ul>
<li><strong>X-Axis</strong> </li>
<li>Main Axis </li>
<li>Horizontal Line </li>
</ul>
<p>The line from <strong>left</strong> to <strong>right</strong> is the Main-Axis.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-71.png" alt="Image" width="600" height="400" loading="lazy">
<em>Main Axis</em></p>
<h2 id="heading-what-is-the-cross-axis-in-css">What is the Cross Axis in CSS?</h2>
<p>You can also call it the:</p>
<ul>
<li><strong>Y-Axis</strong> </li>
<li>Cross Axis</li>
<li>Vertical Line </li>
</ul>
<p>The line from <strong>top</strong> to <strong>bottom</strong> is the Cross-Axis.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-72.png" alt="Image" width="600" height="400" loading="lazy">
<em>Cross Axis</em></p>
<h1 id="heading-project-setup">Project Setup</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-54.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>To experiment with all the properties and values, write the following code in your code editor.</p>
<h3 id="heading-html">HTML</h3>
<p>Write this code inside the body tag:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-css">CSS</h3>
<p>Clear the <strong>default</strong> browser styles so that we can work more accurately:</p>
<pre><code class="lang-css">*{
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;
}
</code></pre>
<p>Select the <strong>.container</strong> class and set it to 100vh. Otherwise, we can't see our result on the <strong>Vertical Axis</strong>:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>Style the <strong>.box-1</strong> class like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">width</span>: <span class="hljs-number">120px</span>;
   <span class="hljs-attribute">height</span>: <span class="hljs-number">120px</span>;
   <span class="hljs-attribute">background-color</span>: skyblue;
   <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
}
</code></pre>
<p>We're all set, now let's start coding!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-3--5-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-use-flexbox-to-center-anything">How to use Flexbox to center anything</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Thumbnail-hashnode.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We can use Flexbox to align our content <code>div</code> both along the X and Y Axis. To do that, we need to write the <code>display: flex;</code> property inside the <code>.container</code> class:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">display</span>: flex;

   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>We'll experiment with these 2 properties:</p>
<ul>
<li><code>justify-content</code></li>
<li><code>align-items</code></li>
</ul>
<h2 id="heading-how-to-center-anything-horizontally-using-flexbox">How to center anything horizontally using Flexbox</h2>
<p>We can use the <strong>justify-content</strong> property to do this using these values 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Justify-content-1.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>values of flexbox justify-content property</strong></em></p>
<p>To experiment with the values, write the following code👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">display</span>: flex;
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

 <span class="hljs-comment">/* Change values to  👇 experiment*/</span>
   <span class="hljs-attribute">justify-content</span>: center;
}
</code></pre>
<p>The result will look like this👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-6--2-.png" alt="Image" width="600" height="400" loading="lazy">
<em>result of justify-content flexbox</em></p>
<h2 id="heading-how-to-center-anything-vertically-using-flexbox">How to center anything vertically using Flexbox</h2>
<p>We can use the <strong><code>align-items</code></strong> property to do this using these values 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/align-items-1.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>values of Flexbox align-items property</strong></em></p>
<p>To experiment with the values, write the following code👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
   <span class="hljs-attribute">display</span>: flex;

 <span class="hljs-comment">/* Change values 👇 to experiment*/</span>
   <span class="hljs-attribute">align-items</span>: center;
}
</code></pre>
<p>The result looks like this👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-7--4-.png" alt="Image" width="600" height="400" loading="lazy">
<em>Result of align-items flexbox</em></p>
<h2 id="heading-how-to-center-a-div-horizontally-amp-vertically-using-flexbox">How to center a div horizontally &amp; vertically using Flexbox</h2>
<p>Here, we can combine both the <strong><code>justify-content</code></strong> and <strong><code>align-items</code></strong> properties to align a div both horizontally and vertically.</p>
<p>Write the following codes👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
   <span class="hljs-attribute">display</span>: flex;

<span class="hljs-comment">/* Change values 👇 to experiment*/</span>
   <span class="hljs-attribute">align-items</span>: center;
   <span class="hljs-attribute">justify-content</span>: center;
}
</code></pre>
<p>The result looks like this 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-8--1-.png" alt="Image" width="600" height="400" loading="lazy">
<em>Centering a div Horizontally &amp; vertically</em></p>
<p>You can check out this <a target="_blank" href="https://www.freecodecamp.org/news/css-flexbox-tutorial-with-cheatsheet/">cheatsheet</a> to learn more about various Flexbox properties.</p>
<h2 id="heading-how-to-use-css-grid-to-center-anything">How to use CSS Grid to center anything</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-70.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We can use <strong>grid</strong> to align our content <code>div</code> both along the X and Y Axis. To do that, we need to write the <code>display: grid;</code> property inside the <code>.container</code> class:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">display</span>: grid;

   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>We'll experiment with these 2 properties:</p>
<ul>
<li><code>justify-content</code></li>
<li><code>align-content</code></li>
</ul>
<p><strong>Alternatively</strong>, you can use these 2 properties:</p>
<ul>
<li><code>justify-items</code></li>
<li><code>align-items</code> </li>
</ul>
<h2 id="heading-how-to-center-anything-horizontally-using-css-grid">How to center anything horizontally using CSS Grid</h2>
<p>We can use the <strong><code>justify-content</code></strong> property to do this using these values 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/justify-content-1--1-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>values of Grid justify-content property</strong></em></p>
<p>Write the following code 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
   <span class="hljs-attribute">display</span>: grid;

  <span class="hljs-comment">/* Change  values   👇 to experiment*/</span>
   <span class="hljs-attribute">justify-content</span>: center;
}
</code></pre>
<p>The result looks like this👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-6--2--1.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>result of justify-content grid</strong></em></p>
<h2 id="heading-how-to-center-anything-vertically-using-css-grid">How to center anything vertically using CSS Grid</h2>
<p>We can use the <strong><code>align-content</code></strong> property to do this using these values 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/align-content-1.png" alt="Image" width="600" height="400" loading="lazy">
<em>Values of CSS grid align-content property</em></p>
<p>Write the following code 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
   <span class="hljs-attribute">display</span>: grid;

  <span class="hljs-comment">/*  Change values 👇 to experiment*/</span>
   <span class="hljs-attribute">align-content</span>: center;
}
</code></pre>
<p>The result will look like this👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-7--4--1.png" alt="Image" width="600" height="400" loading="lazy">
<em>result of align-content grid</em></p>
<h2 id="heading-how-to-center-a-div-horizontally-amp-vertically-using-css-grid">How to center a div horizontally &amp; vertically using CSS Grid</h2>
<p>Here, we can combine both the <strong><code>justify-content</code></strong> and <strong><code>align-content</code></strong> properties to align a div both horizontally and vertically.</p>
<p>Write the following code 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
   <span class="hljs-attribute">display</span>: grid;

<span class="hljs-comment">/* Change  values  👇 to experiment*/</span>
   <span class="hljs-attribute">align-content</span>: center;
   <span class="hljs-attribute">justify-content</span>: center;
}
</code></pre>
<p>The result looks like this👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-8--1--1.png" alt="Image" width="600" height="400" loading="lazy">
<em>Centering a div Horizontally &amp; vertically with Grid</em></p>
<h2 id="heading-alternative-way">Alternative way</h2>
<p>You can also use the <strong><code>justify-items</code></strong> and <strong><code>align-items</code></strong> properties to duplicate the same results:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
   <span class="hljs-attribute">display</span>: grid;

<span class="hljs-comment">/* Change  values  👇 to experiment*/</span>
   <span class="hljs-attribute">align-items</span>: center;
   <span class="hljs-attribute">justify-items</span>: center;
}
</code></pre>
<h2 id="heading-the-place-content-property-in-css-grid">The place-content Property in CSS Grid</h2>
<p>This is the <strong>shorthand</strong> of 2 properties of CSS Grid-&gt;</p>
<ul>
<li><code>justify-content</code></li>
<li><code>align-content</code></li>
</ul>
<p>Follow along 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
   <span class="hljs-attribute">display</span>: grid;

   <span class="hljs-attribute">place-content</span>: center;
}
</code></pre>
<p>We get the same result 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-8--1--2.png" alt="Image" width="600" height="400" loading="lazy">
<em>Centering a div Horizontally &amp; vertically</em></p>
<p>Check out this <a target="_blank" href="https://www.freecodecamp.org/news/css-grid-tutorial-with-cheatsheet">cheatsheet</a> to find out the difference between various Grid properties.</p>
<h2 id="heading-take-a-break">Take a break!</h2>
<p>So far so good – take a break.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-67--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-use-the-css-position-property-to-center-anything">How to use the CSS Position Property to center anything</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-12-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This is a combination of these properties -&gt;</p>
<ul>
<li><code>position</code></li>
<li><code>top, left</code></li>
<li><code>transform, translate</code> </li>
</ul>
<p>Write the following code 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
   <span class="hljs-attribute">position</span>: relative;
}
</code></pre>
<p>Along with this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">position</span>: absolute;

   <span class="hljs-attribute">width</span>: <span class="hljs-number">120px</span>;
   <span class="hljs-attribute">height</span>: <span class="hljs-number">120px</span>;
   <span class="hljs-attribute">background-color</span>: skyblue;
   <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
}
</code></pre>
<h2 id="heading-first-understand-the-center-point-of-a-div">First... Understand the center point of a div</h2>
<p>By default, this is the center point of a div 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-9.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Default Center point of a div</strong></em></p>
<p>That's why we see this odd behavior 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-8--2-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Box is not at exact center</strong></em></p>
<p>Notice that the box is not at the <strong>exact</strong> center in the image above. 👆</p>
<p>By writing this line 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">transform</span>: <span class="hljs-selector-tag">translate</span>(<span class="hljs-selector-tag">-50</span>%,<span class="hljs-selector-tag">-50</span>%);
</code></pre>
<p>We fix the problem 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-10--2-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>New Center point of our div</strong></em></p>
<p>And we get this result 👇 </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-11--1-.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Box is at exact center point</strong></em></p>
<h2 id="heading-what-is-the-translate-property-in-css">What is the Translate property in CSS?</h2>
<p>Translate is the shorthand of 3 properties -&gt;</p>
<ul>
<li><code>translateX</code></li>
<li><code>translateY</code></li>
<li><code>translateZ</code></li>
</ul>
<h2 id="heading-how-to-center-a-div-horizontally-using-css-position-property">How to center a div horizontally using CSS Position property</h2>
<p>We're gonna use the <code>left</code> property inside the<code>.box-</code> class. Follow along 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
<span class="hljs-comment">/* other codes are here*/</span>    

   <span class="hljs-attribute">left</span>: <span class="hljs-number">50%</span>;
   <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translate</span>(-<span class="hljs-number">50%</span>);
}
</code></pre>
<p>And we get this result 👇 </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-6--2--2.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>result of left &amp; transform property</strong></em></p>
<h2 id="heading-how-to-center-a-div-vertically-using-css-position-property">How to center a div vertically using CSS Position property</h2>
<p>We're gonna use the <code>top</code> property inside the<code>box-</code> class. Follow along 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
<span class="hljs-comment">/* Other codes are here*/</span>    

   <span class="hljs-attribute">top</span>: <span class="hljs-number">50%</span>;
   <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translate</span>(<span class="hljs-number">0</span>,-<span class="hljs-number">50%</span>);
}
</code></pre>
<p>And we get this result 👇 </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-7--4--2.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>result of top &amp; transform property</strong></em></p>
<h2 id="heading-how-to-center-a-div-horizontally-amp-vertically-using-css-position-property">How to center a div horizontally &amp; vertically using CSS position property</h2>
<p>To achieve this result, we're gonna combine these properties together -&gt;</p>
<ul>
<li><code>top, left</code></li>
<li><code>transform, translate</code></li>
</ul>
<p>Follow along 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
<span class="hljs-comment">/*Other codes are here */</span>    

   <span class="hljs-attribute">top</span>: <span class="hljs-number">50%</span>;
   <span class="hljs-attribute">left</span>: <span class="hljs-number">50%</span>;
   <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translate</span>(-<span class="hljs-number">50%</span>,-<span class="hljs-number">50%</span>);
}
</code></pre>
<p>And we get this result 👇 </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-8--1--3.png" alt="Image" width="600" height="400" loading="lazy">
<em>result of position &amp; transform property</em></p>
<h2 id="heading-how-to-use-the-margin-property-to-center-anything">How to use the margin Property to center anything</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-73--2-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The margin property is the shorthand of 4 properties </p>
<ul>
<li><code>margin-**top**</code>, <code>margin-**bottom**</code></li>
<li><code>margin-**left**</code>, <code>margin-**right**</code></li>
</ul>
<p>Write this code to set it up 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

   <span class="hljs-attribute">display</span>: flex;
}

<span class="hljs-selector-class">.box-1</span>{
   <span class="hljs-attribute">width</span>: <span class="hljs-number">120px</span>;
   <span class="hljs-attribute">height</span>: <span class="hljs-number">120px</span>;
   <span class="hljs-attribute">background-color</span>: skyblue;
   <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
}
</code></pre>
<h2 id="heading-how-to-center-a-div-horizontally-using-css-margin-property">How to center a div horizontally using CSS margin property</h2>
<p>We're gonna use the <code>margin</code> property inside the <code>.box-1</code> class. Write the following code 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
 //Other codes are here 

  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span> auto;    
}
</code></pre>
<p>The result looks like this👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-6--2--3.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>**result of</strong> CSS margin Property**</em></p>
<h2 id="heading-how-to-center-a-div-vertically-using-css-margin-property">How to center a div vertically using CSS margin property</h2>
<p>We're gonna use the <code>margin</code> property inside the <code>.box-1</code> class. Write the following code 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
 //Other codes are here 

  <span class="hljs-attribute">margin</span>: auto <span class="hljs-number">0px</span>;    
}
</code></pre>
<p>The result looks like this 👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-7--4--3.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>**result of</strong> CSS margin property**</em></p>
<h2 id="heading-how-to-center-a-div-horizontally-amp-vertically-using-css-margin-property">How to center a div horizontally &amp; vertically using CSS margin property</h2>
<p>We're gonna use the <code>margin</code> property inside the<code>.box-</code> class. Write the following code 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
 //Other codes are here 

  <span class="hljs-attribute">margin</span>: auto auto;    
}
</code></pre>
<p>The result looks like this👇</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/06/Frame-8--1--4.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>Result of CSS margin property</strong></em></p>
<h2 id="heading-additional-resources">Additional Resources</h2>
<ul>
<li><a target="_blank" href="https://www.freecodecamp.org/news/css-flexbox-tutorial-with-cheatsheet/">Complete Flexbox Tutorial W/ CheatSheet</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/css-grid-tutorial-with-cheatsheet/">Complete CSS Grid Tutorial W/ CheatSheet</a></li>
</ul>
<h1 id="heading-credits">Credits</h1>
<ul>
<li><a target="_blank" href="https://www.flaticon.com/packs/unicorn-4">uncorns</a>, <a target="_blank" href="https://www.flaticon.com/packs/kitty-avatars-3">kitty</a></li>
<li><a target="_blank" href="https://www.freepik.com/free-vector/collection-people-enjoying-their-free-time_4931926.htm#position=7">artist</a>, <a target="_blank" href="https://www.freepik.com/free-vector/cute-cat-unicorn-play-box-cartoon-icon-illustration_12567355.htm#position=0">kat</a></li>
</ul>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Now, you can confidently <strong>align or center</strong> your content using any of these four methods in CSS.</p>
<p>Here's your <strong>medal</strong> for reading until the end ❤️</p>
<h2 id="heading-suggestions-amp-criticisms-are-highly-appreciated">Suggestions &amp; Criticisms Are Highly Appreciated ❤️</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><strong>YouTube / <a target="_blank" href="https://www.youtube.com/c/JoyShaheb">Joy Shaheb</a></strong></p>
<p><strong>LinkedIn / <a target="_blank" href="https://www.linkedin.com/in/joyshaheb/">Joy Shaheb</a></strong></p>
<p><strong>Twitter / <a target="_blank" href="https://x.com/JoyShaheb">JoyShaheb</a></strong></p>
<p><strong>Instagram / <a target="_blank" href="https://www.instagram.com/joyshaheb/">JoyShaheb</a></strong></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Complete CSS Grid Tutorial with Cheat Sheet 🎖️ ]]>
                </title>
                <description>
                    <![CDATA[ Today we're going to learn CSS Grid properties so that you can make your own responsive websites. I'll explain how each of Grid's properties work along with a CheatSheet that covers everything you can do with Grid. Let's go. 🎖️ Table of Contents: C... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/css-grid-tutorial-with-cheatsheet/</link>
                <guid isPermaLink="false">66b2095227569435a9255afe</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS Grid ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Tue, 08 Jun 2021 16:02:45 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/06/FCC.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're going to learn <strong>CSS Grid</strong> properties so that you can make your own responsive websites. I'll explain how each of Grid's properties work along with a CheatSheet that covers everything you can do with Grid. Let's go. 🎖️</p>
<h1 id="heading-table-of-contents">Table of Contents:</h1>
<ul>
<li><a class="post-section-overview" href="#heading-css-grid-architecture">CSS Grid Architecture</a></li>
<li><a class="post-section-overview" href="#heading-css-grid-chart">CSS Grid Chart</a></li>
<li><a class="post-section-overview" href="#heading-css-grid-parent-properties">Grid Parent Properties</a><ul>
<li><a class="post-section-overview" href="#heading-the-grid-template-columns-property">grid-template-columns</a></li>
<li><a class="post-section-overview" href="#heading-the-grid-template-rows-property">grid-template-rows</a></li>
<li><a class="post-section-overview" href="#heading-the-grid-template-areas-property-1">grid-template-areas</a></li>
<li><a class="post-section-overview" href="#heading-the-column-gap-property">How to create column and row gaps in Grid</a></li>
<li><a class="post-section-overview" href="#heading-the-justify-items-property">How to justify items and align items with Grid</a></li>
<li><a class="post-section-overview" href="#heading-the-justify-content-property">How to justify content and align-content with Grid</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-css-grid-child-properties">Child Properties in CSS Grid</a><ul>
<li><a class="post-section-overview" href="#heading-the-grid-column-startend-properties">grid-column : start/end</a></li>
<li><a class="post-section-overview" href="#heading-the-grid-row-startend-properties">grid-row : start/end</a></li>
<li><a class="post-section-overview" href="#heading-the-grid-area-property">grid-area</a></li>
<li><a class="post-section-overview" href="#heading-the-justify-self-property">justify-self || align-self</a></li>
</ul>
</li>
<li><a class="post-section-overview" href="#heading-shorthand-for-css-grid-properties">Shorthand for Grid</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ul>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like">You can watch this tutorial on YouTube as well if you like:</h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/VXW1r09Y6Tw" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-first-what-is-css-grid">First, What is CSS Grid?</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6e3wc9k9qxw07o54e38x.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Grid is a blueprint for making websites.</p>
<p>The Grid model allows you to layout the content of your website. Not only that, it helps you create the structures you need for building responsive websites for multiple devices. This means your site will look good on desktop, mobile, and tablet.</p>
<p>Here's a simple demo which I created using Grid as the main blueprint.</p>
<h3 id="heading-desktop-view">Desktop View</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuz7de20oxw7t8kmid4s.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-mobile-view">Mobile View</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wqtabiihl0kexxdbvht.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h1 id="heading-css-grid-architecture">CSS Grid Architecture</h1>
<p>So how does Grid architecture work? The Grid items [Contents] are distributed along the main axis and cross axis. Using various Grid properties, you can manipulate the items to create your website layouts.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9qs07pm0a8s20scr6wr.png" alt="Grid Architecture" width="600" height="400" loading="lazy">
<em>grid architecture</em></p>
<p>By the way, you can join multiple rows and columns, just like in Excel software, which gives you more flexibility and options than Flexbox.</p>
<p>By the way, here's a CheatSheet I made for <a target="_blank" href="https://www.freecodecamp.org/news/css-flexbox-tutorial-with-cheatsheet/">Flexbox</a> if you want to learn more about that.</p>
<h1 id="heading-css-grid-chart">CSS Grid Chart</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3oecuzn41ee3gj4o1ny.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>This chart contains every possible property you can use when using grid. Grid properties are divided into:</p>
<ul>
<li>Parent properties</li>
<li>Child Properties</li>
</ul>
<p><strong>Note:</strong> The red colored text denotes the shorthand properties:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n93mkan7du7wz3zyibtw.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5g3msf9v3yw9qjpzkvj7.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>By the end of this tutorial, you'll have a clear understanding of how to use all of those properties.</p>
<h1 id="heading-how-to-set-up-the-project">How to Set Up the Project</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8c9bfd2puec1wjuk063k.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>For this project, you need to know little bit of HTML, CSS, and how to work with VS code. Follow along with me as we complete the following tasks:</p>
<ol>
<li>Create a folder named "Project-1" and Open VS Code</li>
<li>Create index.html and style.css files</li>
<li>Install Live Server and run it.</li>
</ol>
<p>Or, you can just open <a target="_blank" href="https://codepen.io">Codepen</a> and start coding.</p>
<p>At the end of this tutorial, you will be able to make accurate and beautiful website layouts.</p>
<p>And...we're all set! Let's start coding. 😊</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfjntw9islnyhv6mkigq.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-html">HTML</h2>
<p>Create three boxes inside the body tag, like this 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> A <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-2"</span>&gt;</span> B <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-3"</span>&gt;</span> C <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-css">CSS</h2>
<h3 id="heading-step-1">Step 1</h3>
<p>Let's clear out our default browser styles. Follow me 👇</p>
<pre><code class="lang-css">*{
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;
}
</code></pre>
<h3 id="heading-step-2">Step 2</h3>
<p>Inside the body selector, do these adjustments:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: sans-serif;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">min-height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h3 id="heading-step-3">Step 3</h3>
<p>Now, let's style our boxes by selecting all of them together -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-attr">[class^=<span class="hljs-string">"box-"</span>]</span> {
  <span class="hljs-attribute">background-color</span>: skyblue;

<span class="hljs-comment">/* To place the letter at the center */</span>
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">place-items</span>: center;
}
</code></pre>
<p><strong>Note:</strong> Don't worry, we'll discuss those grid properties in detail later.</p>
<h3 id="heading-step-4">Step 4</h3>
<p>Now, place some gaps between our boxes like this 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">20px</span>;
}
</code></pre>
<h2 id="heading-but-wait">But Wait....</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cq8exwor5aiciu2j6jwu.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Before starting, you need to understand the relationship between parent and child classes.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzq3w0bys78fveqb9z0z.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>For the Grid parent property, we will write inside the <code>.container</code> class. For the Grid child property, we will write in the <code>.box-*</code> classes.</p>
<h1 id="heading-css-grid-parent-properties">CSS Grid Parent Properties</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwnmeomlyekzuov7tcml.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>First, let's learn about CSS Grid's parent properties!</p>
<h2 id="heading-the-grid-template-columns-property">The grid-template-columns property</h2>
<p>You use this property to define the <strong>number and width</strong> of columns. You can either individually set the width of each column, or set a uniform width for all columns using the <code>repeat()</code> function.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pu3jedhac2u0onuan6go.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mov0pc7djie6gbj7m25g.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>grid-template-columns</em></p>
<p>To recreate these results, write the following CSS lines -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">20px</span>;

<span class="hljs-comment">/*  Change the values     👇 to experiment */</span>
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">200px</span> auto <span class="hljs-number">100px</span>;
}
</code></pre>
<p><strong>Note:</strong></p>
<ul>
<li>The pixel values will be an exact measurement. The "auto" keyword will cover the available space.</li>
<li>If you use fr (fraction unit) as a unit of measurement, all the boxes will be equal in size.</li>
</ul>
<h2 id="heading-the-grid-template-rows-property">The grid-template-rows property</h2>
<p>You use this property to define the <strong>number and height</strong> of rows. You can either individually set the height of each row, or set a uniform height for all rows using the <code>repeat()</code> function.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r78wfrp3rr4mmn3507ym.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/elpj9jw29ydlwao1yb3q.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>grid-template-rows</em></p>
<p>To test these results, write the following CSS code -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

<span class="hljs-comment">/* Change the values  👇 to experiment */</span>
  <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-number">200px</span> auto <span class="hljs-number">100px</span>;
}
</code></pre>
<h2 id="heading-the-grid-template-areas-property">The grid-template-areas property</h2>
<p>You use this property to specify the amount of space a grid cell should carry in terms of columns and rows across the parent container. Life's much easier with this property as it allows us to see visually what we're doing.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nw9e0e1wk96wg3uq99f.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>Standard 12X12 layout</em></p>
<p>Call it the blueprint(template) of our layout👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nffhbw0eho476i535eyu.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>The blueprint</em></p>
<p>To experiment with this, you need to understand both the parent and child properties:</p>
<ul>
<li><strong>grid-template-areas:</strong> The parent property that will create the blueprint</li>
<li><strong>grid-area:</strong> the child property that will follow the blueprint.</li>
</ul>
<h3 id="heading-first-create-the-blueprint">First, create the blueprint</h3>
<p>Like this 👇 inside the parent .container class:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

  <span class="hljs-attribute">grid-template-areas</span>: 
    <span class="hljs-string">"A A A A   A A A A   A A A A"</span>
    <span class="hljs-string">"B B B B   B B B B   B B C C"</span>
    <span class="hljs-string">"B B B B   B B B B   B B C C"</span>;
}
</code></pre>
<h3 id="heading-implement-the-blueprint">Implement the blueprint</h3>
<p>Target all our child <code>.box-*</code> classes and set the values like this -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
  <span class="hljs-attribute">grid-area</span>: A;
}
<span class="hljs-selector-class">.box-2</span>{
  <span class="hljs-attribute">grid-area</span>: B;
}
<span class="hljs-selector-class">.box-3</span>{
  <span class="hljs-attribute">grid-area</span>: C;
}
</code></pre>
<p><strong>Note:</strong> I'll discuss the grid-area property again in the grid child property section.</p>
<h2 id="heading-the-column-gap-property">The column-gap property</h2>
<p>You use this property to place a gap between <strong>Columns</strong> inside the grid 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fa59gtskckoh7ms1uk1h.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>column-gap</em></p>
<p>To test this, write the following in CSS 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">100px</span> <span class="hljs-number">100px</span> <span class="hljs-number">100px</span>;
//Change values👇 to experiment
  <span class="hljs-attribute">column-gap</span>:  <span class="hljs-number">50px</span>;
}
</code></pre>
<p><strong>Note:</strong> column-gap works with grid-template-columns.</p>
<h2 id="heading-the-row-gap-property">The row-gap property</h2>
<p>You use this property to place a gap between <strong>Rows</strong> inside the grid 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aw6l38lydlag1dtzw9j8.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>row gap</em></p>
<p>Let's test this 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

  <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-number">100px</span> <span class="hljs-number">100px</span> <span class="hljs-number">100px</span>;
// Change   👇  values to experiment
  <span class="hljs-attribute">row-gap</span>:  <span class="hljs-number">50px</span>;
}
</code></pre>
<p><strong>Note:</strong> row-gap works with grid-template-rows.</p>
<h2 id="heading-the-justify-items-property">The justify-items property</h2>
<p>You use this property to position grid-items (children) inside grid containers along the <strong>X-Axis [Main Axis]</strong>. The <strong>4</strong> values are 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oka32lvb2i0lrhcb8p4e.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4r2jv92rgp3514fsp4ft.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>justify-items</em></p>
<p>If you want to test this, then add 1 more box inside the HTML -&gt;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>

  <span class="hljs-comment">&lt;!--Other boxes - A, B, C are here--&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-4"</span>&gt;</span> D <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>and in the CSS -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

// each box is 200px by 200px
  <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;

//  Change values 👇  to experiment
  <span class="hljs-attribute">justify-items </span>: end;
}
</code></pre>
<h2 id="heading-the-align-items-property">The align-items property</h2>
<p>You use this property to position grid-items (children) inside the grid container along the <strong>Y-Axis [Cross Axis]</strong>. The <strong>4</strong> values are 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/abl60fsmjuys1kadh1ig.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>align-items</em></p>
<p>Don't change anything in the HTML, but in the CSS write -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

// each box is 200px by 200px
  <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;

//  Change values 👇  to experiment
  <span class="hljs-attribute">align-items</span>: center;
}
</code></pre>
<h2 id="heading-the-justify-content-property">The justify-content property</h2>
<p>You use this property to position your grid [Basically everything] inside the grid container along the <strong>X-Axis [Main Axis]</strong>. The <strong>7</strong> values are 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9a8eovy1t3i8x5yii4i.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onq2gubifwog2rccclqe.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>justify-content</em></p>
<p>Don't change anything in the HTML, but in the CSS write -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

// each box is 200px by 200px
  <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;

//  Change  values  👇  to experiment
  <span class="hljs-attribute">justify-content</span>: center;
}
</code></pre>
<h2 id="heading-the-align-content-property">The align-content property</h2>
<p>You use this property to position our grid [Basically everything] inside the grid container along the <strong>Y-Axis [Cross Axis]</strong>. The <strong>7</strong> values are 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfgzxrhe2ca4mzk1ies1.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1rjvr4bvwsc8ceevq96.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>align-content</em></p>
<p>Don't change anything in the HTML, but in the CSS write -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

// each box is 200px by 200px
  <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;

//  Change  values 👇  to experiment
  <span class="hljs-attribute">align-content </span>: center;
}
</code></pre>
<h1 id="heading-take-a-break">Take a Break</h1>
<p>So far so good! Take a break, you deserve it.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xanzksehasqcwc8qm8fw.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h1 id="heading-css-grid-child-properties">CSS Grid Child Properties</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2k1muu9oldsp02aigvx.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Now, let's look at Grid child properties.</p>
<h1 id="heading-the-css-grid-scale">The CSS Grid Scale</h1>
<p>I made this grid scale to demonstrate the calculations of how rows and columns are joined together. We can use any 1 of the 2 types of measurement:</p>
<ul>
<li>The digit [1,2,3,4, etc...]</li>
<li>The span keyword</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ie4paaplgo8rwf4fd41o.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>The Grid Scale</em></p>
<p>You'll get a clear picture of how this guide works ☝️ when we write code in just a short moment.</p>
<p>The illustration below 👇 shows the start and end points of rows and columns of a single cell.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bumrjjy06owkahoe49y3.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>Grid column &amp; row -&gt; start &amp; end points</em></p>
<h3 id="heading-html-1">HTML</h3>
<p>To experiment with the grid scale and understand the following properties, make 4 boxes inside body tag:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> A <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-2"</span>&gt;</span> B <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-3"</span>&gt;</span> C <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-4"</span>&gt;</span> D <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We're gonna utilize the <code>repeat()</code> function. It repeats our code a certain number of times. Here's an example 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">grid-template-columns</span> : <span class="hljs-selector-tag">repeat</span>(5,1<span class="hljs-selector-tag">fr</span>);
</code></pre>
<p>This ☝️ is the equivalent to writing this:👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">grid-template-columns</span> : 1<span class="hljs-selector-tag">fr</span> 1<span class="hljs-selector-tag">fr</span> 1<span class="hljs-selector-tag">fr</span> 1<span class="hljs-selector-tag">fr</span> 1<span class="hljs-selector-tag">fr</span> ;
</code></pre>
<h4 id="heading-a-quick-note">A Quick Note</h4>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2q151726koc8h8mp0ht.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>When we use the fr [ Fraction ] unit, we are dividing the screen area into equal proportions.</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">grid-template-columns</span>: <span class="hljs-selector-tag">repeat</span>(5, 1<span class="hljs-selector-tag">fr</span>);
</code></pre>
<p>This ☝️ code is dividing our screen width into 5 equal parts.</p>
<p>We're set, let's start!</p>
<h2 id="heading-the-grid-column-startend-properties">The grid-column: start/end properties</h2>
<p>You use these two properties to join multiple <strong>COLUMNS</strong> together. It is a shorthand of 2 properties:</p>
<ul>
<li><strong>grid-column-start</strong></li>
<li><strong>grid-column-end</strong></li>
</ul>
<p>Write this code in your CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">12</span>, <span class="hljs-number">1</span>fr);
  <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">12</span>, <span class="hljs-number">1</span>fr);
}
</code></pre>
<p>The result looks like this:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2pt6zzl39mvjkv6h1m2.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Here, we are dividing our screen [ both width and height ] into 12 equal parts. 1 box is occupying 1 part, or you can call it 1fr [ 1 fraction ]. The remaining 8 fractions along the width are empty.</p>
<p>As we are dealing with child properties, we need to target our <code>.box-*</code> classes like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{}
<span class="hljs-selector-class">.box-2</span>{}
<span class="hljs-selector-class">.box-3</span>{}
<span class="hljs-selector-class">.box-4</span>{}
</code></pre>
<p>You can experiment with any or all of these boxes, I'll stick with <code>.box-1</code>.</p>
<p>Let's bring our grid scale. We are dealing with columns – just focus on the columns, not rows.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ie4paaplgo8rwf4fd41o.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>The Grid Scale</em></p>
<p>The default scale of every <code>.box-*</code> class is:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">grid-column-start</span> : 1;
<span class="hljs-selector-tag">grid-column-end</span> : 2;

<span class="hljs-comment">/* The shorthand -&gt; */</span>
 <span class="hljs-selector-tag">grid-column</span> : 1 / 2
</code></pre>
<p>We can write this ☝️ in the span unit as well, like this 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">grid-column</span> : <span class="hljs-selector-tag">span</span> 1;
</code></pre>
<p>Let's assign the empty 8 fractions to <code>.box-1</code> like this 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
<span class="hljs-attribute">grid-column </span>: <span class="hljs-number">1</span>/<span class="hljs-number">10</span>
}
</code></pre>
<p>The result looks like this:👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgcg4yyxourlpk1b6z3m.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-a-quick-note-1">A quick note</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g10ckpo6g3tpn8e9kkpw.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>How did I do the calculation? The <code>box-1</code> occupies 1 box itself. And over that, we are adding more 8 boxes. At the last, you must add 1 to indicate the end so, 8+1+1 = 10.</p>
<h3 id="heading-how-to-use-the-span-keyword">How to use the span keyword</h3>
<p>If you're confused by the first property, you can use the <code>span</code> keyword as it is easier to understand.</p>
<p>We need to add 8 boxes to <code>.box-1</code> which already occupies one box. So, we have to do 8+1 = 9.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
  <span class="hljs-attribute">grid-column </span>: span <span class="hljs-number">9</span>;
}
</code></pre>
<p>This is gonna output the same result.</p>
<h2 id="heading-the-grid-row-startend-properties">The grid-row: start/end properties</h2>
<p>You use these two properties to join multiple <strong>ROWS</strong> together. It is shorthand of 2 properties:</p>
<ul>
<li><strong>grid-row-start</strong></li>
<li><strong>grid-row-end</strong></li>
</ul>
<p>Let's experiment with it! Here, I'll stick with .box-1, and here's our grid guide. Now, only focus on the row scale, not the column.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ie4paaplgo8rwf4fd41o.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>The Grid Scale</em></p>
<p>Let's join 9 boxes to <code>.box-1</code> along the row.</p>
<p>Write this code: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
  <span class="hljs-attribute">grid-row </span>: <span class="hljs-number">1</span>/<span class="hljs-number">11</span>;
}
</code></pre>
<p>The calculation looks like this -&gt; <code>.box-1</code> holds 1 box, and you add 9 more boxes, plus a mandatory 1 at the last position to indicate the end which gives you 9+1+1=11.</p>
<p>Here's the alternative 👇 using the span keyword:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
  <span class="hljs-attribute">grid-row </span>: span <span class="hljs-number">10</span>;
}
</code></pre>
<p>And here's that calculation -&gt; <code>.box-</code>1 holds 1 box, and you add 9 more boxes<br>9+1=10.</p>
<p>Here's the result so far: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqkoglmoh1whcyve7uad.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-the-grid-area-property">The grid-area property</h2>
<p>First, we need to set up <a class="post-section-overview" href="#heading-the-grid-template-areas-property-1">grid-template-areas</a> ☝️ Once we've done that, we have to specify the names used in parent class inside the children classes, like this:👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcoalgwdeiogd3tmazzf.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>Standard 12X12 layout</em></p>
<p>Then we need to specify grid-template-areas inside parent container: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3r84bvxt5230jyr5pydt.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Like this 👇 inside the parent class:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

  <span class="hljs-attribute">grid-template-areas</span>: 
    <span class="hljs-string">"A A A A   A A A A   A A A A"</span>
    <span class="hljs-string">"B B B B   B B B B   B B C C"</span>
    <span class="hljs-string">"B B B B   B B B B   B B C C"</span>;
}
</code></pre>
<p>Then we specify the names used in the parent container inside the child classes with grid-areas👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffwg22949ogu0m2bw3sn.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Like this 👇 inside the children classes:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
  <span class="hljs-attribute">grid-area</span>: A;
}
<span class="hljs-selector-class">.box-2</span>{
  <span class="hljs-attribute">grid-area</span>: B;
}
<span class="hljs-selector-class">.box-3</span>{
  <span class="hljs-attribute">grid-area</span>: C;
}
</code></pre>
<h2 id="heading-the-justify-self-property">The justify-self property</h2>
<p>You use this property to position <strong>1 individual</strong> grid-item (child) inside a grid container along the <strong>X-Axis [Main Axis]</strong>. The <strong>4</strong> values are 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g21475dm64kw2vyqgxvd.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>Justify-self</em></p>
<p>Write this code to experiment with the values 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap </span>:<span class="hljs-number">25px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

<span class="hljs-comment">/* // each box has equal size */</span>
  <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-number">1</span>fr <span class="hljs-number">1</span>fr;
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">1</span>fr <span class="hljs-number">1</span>fr;
}
</code></pre>
<p><strong>Remember!</strong> This only works on the child classes. So, target any <code>.box-*</code> class. I'll target the first box:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span> {
<span class="hljs-comment">/*  Change values 👇  to experiment */</span>
  <span class="hljs-attribute">justify-self </span>: start;  
}
</code></pre>
<h2 id="heading-the-align-self-property">The align-self property</h2>
<p>You use this property to position <strong>1 individual</strong> grid-item (child) inside a grid container along the <strong>Y-Axis [Cross Axis]</strong>. The <strong>4</strong> values are 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qr5dlqehevjclxys3bx6.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>align-self</em></p>
<p>Let's experiment with the values 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">gap </span>:<span class="hljs-number">25px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

<span class="hljs-comment">/* // each box has equal size */</span>
  <span class="hljs-attribute">grid-template-rows</span>: <span class="hljs-number">1</span>fr <span class="hljs-number">1</span>fr;
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">1</span>fr <span class="hljs-number">1</span>fr;
}
</code></pre>
<p><strong>Remember!</strong> This only works on the child classes. So, target any <code>.box-*</code> class. I'll target the 1st box:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span> {
<span class="hljs-comment">/*  Change values 👇  to experiment */</span>
  <span class="hljs-attribute">align-self </span>: start;  
}
</code></pre>
<h1 id="heading-shorthand-for-css-grid-properties">Shorthand for CSS Grid Properties</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f6s9qvnvjtwbj7r1ash6.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's look at these Grid shorthand properties:</p>
<ul>
<li><code>place-content</code></li>
<li><code>place-items</code></li>
<li><code>place-self</code></li>
<li><code>grid-template</code></li>
<li><code>gap</code> / <code>grid-gap</code></li>
</ul>
<h2 id="heading-place-content">place-content</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jz1dydjbbgxw8bxt86i4.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>place-content</em></p>
<p>This is the shorthand of 2 properties:</p>
<ul>
<li>align-content</li>
<li>justify-content</li>
</ul>
<h4 id="heading-an-example">An example</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">align-content</span> : <span class="hljs-selector-tag">center</span>;
<span class="hljs-selector-tag">justify-content</span> : <span class="hljs-selector-tag">end</span>;

<span class="hljs-comment">/* The shorthand */</span>
<span class="hljs-selector-tag">place-content</span> : <span class="hljs-selector-tag">center</span> / <span class="hljs-selector-tag">end</span> ;
</code></pre>
<h2 id="heading-place-items">place-items</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvu44ujney5ixzvzedbb.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>place-items</em></p>
<p>This is the shorthand of 2 properties:</p>
<ul>
<li>align-items</li>
<li>justify-items</li>
</ul>
<h4 id="heading-an-example-1">An example</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">align-items</span> : <span class="hljs-selector-tag">end</span>;
<span class="hljs-selector-tag">justify-items</span> : <span class="hljs-selector-tag">center</span>;

<span class="hljs-comment">/* The shorthand */</span>
<span class="hljs-selector-tag">place-items</span> : <span class="hljs-selector-tag">end</span> / <span class="hljs-selector-tag">center</span> ;
</code></pre>
<h2 id="heading-place-self">place-self</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jafsak8hqgfb78hbxs0b.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>place-self</em></p>
<p>This is the shorthand of 2 properties:</p>
<ul>
<li>align-self</li>
<li>justify-self</li>
</ul>
<h4 id="heading-an-example-2">An example</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">align-self</span> : <span class="hljs-selector-tag">start</span> ;
<span class="hljs-selector-tag">justify-self</span> : <span class="hljs-selector-tag">end</span> ;

<span class="hljs-comment">/* The shorthand */</span>
<span class="hljs-selector-tag">place-self</span> : <span class="hljs-selector-tag">start</span> / <span class="hljs-selector-tag">end</span> ;
</code></pre>
<h2 id="heading-grid-template">grid-template</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlratlmwgt0vvknnt82w.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>grid-template</em></p>
<p>This is the shorthand of 2 properties:</p>
<ul>
<li>grid-template-rows</li>
<li>grid-template-columns</li>
</ul>
<h4 id="heading-an-example-3">An example</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">grid-template-rows</span> : 100<span class="hljs-selector-tag">px</span> 100<span class="hljs-selector-tag">px</span>;
<span class="hljs-selector-tag">grid-template-columns</span> : 200<span class="hljs-selector-tag">px</span> 200<span class="hljs-selector-tag">px</span>;

<span class="hljs-comment">/* The shorthand */</span>
<span class="hljs-selector-tag">grid-template</span> : 100<span class="hljs-selector-tag">px</span> 100<span class="hljs-selector-tag">px</span> / 200<span class="hljs-selector-tag">px</span> 200<span class="hljs-selector-tag">px</span>;
</code></pre>
<h2 id="heading-gapgrid-gap">gap/grid-gap</h2>
<p>Gap and grid-gap are the same thing and do the same work. You can use either of them.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brubmtmvt2jvq1l9f1ep.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>gap</em></p>
<p>This is the shorthand of 2 properties:</p>
<ul>
<li>row-gap</li>
<li>column-gap</li>
</ul>
<h4 id="heading-an-example-4">An example</h4>
<pre><code class="lang-css"><span class="hljs-selector-tag">row-gap</span> : 20<span class="hljs-selector-tag">px</span> ; 
<span class="hljs-selector-tag">column-gap</span> : 30<span class="hljs-selector-tag">px</span> ;

<span class="hljs-comment">/* The shorthand */</span>
<span class="hljs-selector-tag">gap</span> : 20<span class="hljs-selector-tag">px</span>  30<span class="hljs-selector-tag">px</span>;
</code></pre>
<h2 id="heading-credits">Credits</h2>
<ul>
<li><a target="_blank" href="https://www.flaticon.com/packs/unicorn-4">Unicorns</a></li>
<li><a target="_blank" href="https://www.freepik.com/free-vector/cute-corgi-drink-milk-tea-boba-cartoon-vector-illustration-animal-drink-concept-isolated-vector-flat-cartoon-style_10336142.htm#position=13">Corgis</a>, <a target="_blank" href="https://www.freepik.com/free-psd/pet-food-banner-template_13762522.htm#position=1">kat</a></li>
</ul>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Now, you can confidently make responsive website layouts using your grid knowledge!</p>
<p>Here's your medal for reading until the end ❤️ </p>
<h2 id="heading-suggestions-amp-criticisms-are-highly-appreciated">Suggestions &amp; Criticisms Are Highly Appreciated ❤️</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><strong>YouTube / <a target="_blank" href="https://www.youtube.com/c/JoyShaheb">Joy Shaheb</a></strong></p>
<p><strong>LinkedIn / <a target="_blank" href="https://www.linkedin.com/in/joyshaheb/">Joy Shaheb</a></strong></p>
<p><strong>Twitter / <a target="_blank" href="https://x.com/JoyShaheb">JoyShaheb</a></strong></p>
<p><strong>Instagram / <a target="_blank" href="https://www.instagram.com/joyshaheb/">JoyShaheb</a></strong></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Async/Await Tutorial – Learn Callbacks, Promises, and Async/Await in JS by Making Ice Cream 🍧🍨🍦 ]]>
                </title>
                <description>
                    <![CDATA[ Today we're going to build and run an ice cream shop and learn asynchronous JavaScript at the same time. Along the way, you'll learn how to use: Callbacks Promises Async / Await Here's what we'll cover in this article: What is Asynchronous JavaSc... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-async-await-tutorial-learn-callbacks-promises-async-await-by-making-icecream/</link>
                <guid isPermaLink="false">66b2095aa5be9a107f4341cd</guid>
                
                    <category>
                        <![CDATA[ async/await ]]>
                    </category>
                
                    <category>
                        <![CDATA[ asynchronous programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ callbacks ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ promises ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Wed, 02 Jun 2021 14:45:18 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/05/FCC-Thumbnail--3-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're going to build and run an <strong>ice cream shop</strong> and learn <strong>asynchronous JavaScript</strong> at the same time. Along the way, you'll learn how to use:</p>
<ul>
<li>Callbacks</li>
<li>Promises</li>
<li>Async / Await</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1j935dg72g9u8zvh2oi.png" alt="Alt Text" width="1107" height="576" loading="lazy"></p>
<h1 id="heading-heres-what-well-cover-in-this-article">Here's what we'll cover in this article:</h1>
<ul>
<li>What is Asynchronous JavaScript?</li>
<li>Synchronous vs Asynchronous JavaScript</li>
<li>How Callbacks Work in JavaScript</li>
<li>How Promises Work in JavaScript</li>
<li>How Async / Await Works in JavaScript</li>
</ul>
<p>So let's dive in!</p>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like">You can watch this tutorial on YouTube as well if you like:</h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/n5ZtTO1ArWg" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-what-is-asynchronous-javascript">What is Asynchronous JavaScript?</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7yd96tgxvuowqmfgcx6b.png" alt="Alt Text" width="1498" height="703" loading="lazy"></p>
<p>If you want to build projects efficiently, then this concept is for you.</p>
<p>The theory of async JavaScript helps you break down big complex projects into smaller tasks.</p>
<p>Then you can use any of these three techniques – <strong>callbacks, promises or Async/await</strong> – to run those small tasks in a way that you get the best results.</p>
<p>Let's dive in!🎖️</p>
<h1 id="heading-synchronous-vs-asynchronous-javascript">Synchronous vs Asynchronous JavaScript</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arzbf1rc3pi4yi6u8wup.png" alt="Alt Text" width="1253" height="642" loading="lazy"></p>
<h2 id="heading-what-is-a-synchronous-system">What is a Synchronous System?</h2>
<p>In a synchronous system, tasks are completed one after another.</p>
<p>Think of this as if you have just one hand to accomplish 10 tasks. So, you have to complete one task at a time.</p>
<p>Take a look at the GIF 👇 – one thing is happening at a time here:</p>
<p><img src="https://media.giphy.com/media/ICIS16DkE9qB9HVxtq/giphy.gif" alt="Synchronous System" width="480" height="258" loading="lazy"></p>
<p>You'll see that until the first image is loaded completely, the second image doesn't start loading.</p>
<p>Well, JavaScript is by default Synchronous <strong>[single threaded]</strong>. Think about it like this – one thread means one hand with which to do stuff.</p>
<h2 id="heading-what-is-an-asynchronous-system">What is an Asynchronous System?</h2>
<p>In this system, tasks are completed independently.</p>
<p>Here, imagine that for 10 tasks, you have 10 hands. So, each hand can do each task independently and at the same time.</p>
<p>Take a look at the GIF 👇 – you can see that each image loads at the same time.</p>
<p><img src="https://media.giphy.com/media/MMDnmJnE7uhX6KtcKc/giphy.gif" alt="Asynchronous System" width="480" height="258" loading="lazy"></p>
<p>Again, all the images are loading at their own pace. None of them is waiting for any of the others.</p>
<h2 id="heading-to-summarize-synchronous-vs-asynchronous-js">To Summarize Synchronous vs Asynchronous JS:</h2>
<p>When three images are on a marathon, in a:</p>
<ul>
<li><strong>Synchronous</strong> system, three images are in the same lane. One can't overtake the other. The race is finished one by one. If image number 2 stops, the following image stops.</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1r9y4ghhq0t8wjb1u9h.png" alt="Alt Text" width="1544" height="748" loading="lazy"></p>
<ul>
<li><strong>Asynchronous system,</strong> the three images are in different lanes. They'll finish the race on their own pace. Nobody stops for anybody:</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ehknx5shc4orh32s0ktk.png" alt="Alt Text" width="1544" height="748" loading="lazy"></p>
<h2 id="heading-synchronous-and-asynchronous-code-examples">Synchronous and Asynchronous Code Examples</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzbnpcza9rbj8xgiby95.png" alt="Alt Text" width="1156" height="511" loading="lazy"></p>
<p>Before starting our project, let's look at some examples and clear up any doubts.</p>
<h3 id="heading-synchronous-code-example">Synchronous Code Example</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5m6p1qy522lj3auvl5ty.png" alt="Alt Text" width="1054" height="421" loading="lazy"></p>
<p>To test a synchronous system, write this code in JavaScript:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">" I "</span>);

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">" eat "</span>);

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">" Ice Cream "</span>);
</code></pre>
<p>Here's the result in the console: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54izy7zyo52j2z6netls.png" alt="Alt Text" width="919" height="358" loading="lazy"></p>
<h3 id="heading-asynchronous-code-example">Asynchronous code example</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5d0o8unbe8c67qeqz0w.png" alt="Alt Text" width="1054" height="421" loading="lazy"></p>
<p>Let's say it takes two seconds to eat some ice cream. Now, let's test out an asynchronous system. Write the below code in JavaScript.</p>
<p><strong>Note:</strong> Don't worry, we'll discuss the <code>setTimeout()</code> function later in this article.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"I"</span>);

<span class="hljs-comment">// This will be shown after 2 seconds</span>

<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"eat"</span>);
},<span class="hljs-number">2000</span>)

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Ice Cream"</span>)
</code></pre>
<p>And here's the result in the console: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o44c2t0r7bknkadoumgx.png" alt="Alt Text" width="924" height="364" loading="lazy"></p>
<p>Now that you know the difference between synchronous and async operations, let's build our ice cream shop.</p>
<h2 id="heading-how-to-setup-our-project">How to Setup our Project</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2mzbtcnm67v2iys7cix7.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>For this project you can just open <a target="_blank" href="https://codepen.io/">Codepen.io</a> and start coding. Or, you can do it in VS code or the editor of your choice.</p>
<p>Open the JavaScript section, and then open your developer console. We'll write our code and see the results in the console.</p>
<h1 id="heading-what-are-callbacks-in-javascript">What are Callbacks in JavaScript?</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5iloofqsv3lcdl4flsi.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>When you nest a function inside another function as an argument, that's called a callback.</p>
<p>Here's an illustration of a callback:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz3pl56lmoc2pq7wzi2s.png" alt="Image" width="600" height="400" loading="lazy">
<em><strong>An example of a callback</strong></em></p>
<p>Don't worry, we'll see some examples of callbacks in a minute.</p>
<h3 id="heading-why-do-we-use-callbacks">Why do we use callbacks?</h3>
<p>When doing a complex task, we break that task down into smaller steps. To help us establish a relationship between these steps according to time (optional) and order, we use callbacks.</p>
<p>Take a look at this example:👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o05q7ortgctx2oeyntfn.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>Chart contains steps to make ice cream</strong></em></p>
<p>These are the small steps you need to take to make ice cream. Also note that in this example, the order of the steps and timing are crucial. You can't just chop the fruit and serve ice cream.</p>
<p>At the same time, if a previous step is not completed, we can't move on to the next step.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2v1rn50smjul9arkneza.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>To explain that in more detail, let's start our ice cream shop business.</p>
<h2 id="heading-but-wait">But Wait...</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cq8exwor5aiciu2j6jwu.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>The shop will have two parts:</p>
<ul>
<li>The storeroom will have all the ingredients [Our Backend]</li>
<li>We'll produce ice cream in our kitchen [The frontend]</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i69bws707m5rvsj34i9o.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-lets-store-our-data">Let's store our data</h2>
<p>Now, we're gonna store our ingredients inside an object. Let's start!</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihezrht8dgg9xn8lm2k9.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>You can store the ingredients inside objects like this: 👇</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> stocks = {
    <span class="hljs-attr">Fruits</span> : [<span class="hljs-string">"strawberry"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"apple"</span>]
 }
</code></pre>
<p>Our other ingredients are here: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dcwr770l0ubupv0r2gj.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>You can store these other ingredients in JavaScript objects like this: 👇</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> stocks = {
    <span class="hljs-attr">Fruits</span> : [<span class="hljs-string">"strawberry"</span>, <span class="hljs-string">"grapes"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"apple"</span>],
    <span class="hljs-attr">liquid</span> : [<span class="hljs-string">"water"</span>, <span class="hljs-string">"ice"</span>],
    <span class="hljs-attr">holder</span> : [<span class="hljs-string">"cone"</span>, <span class="hljs-string">"cup"</span>, <span class="hljs-string">"stick"</span>],
    <span class="hljs-attr">toppings</span> : [<span class="hljs-string">"chocolate"</span>, <span class="hljs-string">"peanuts"</span>],
 };
</code></pre>
<p>The entire business depends on what a customer <strong>orders</strong>. Once we have an order, we start production and then we serve ice cream. So, we'll create two functions -&gt;</p>
<ul>
<li><code>order</code></li>
<li><code>production</code></li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3bnzniiyamo0b9l7e806.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>This is how it all works: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8h8ra9wor8cs3dgddpb.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em>Get order from customer, fetch ingredients, start production, then serve.</em></p>
<p>Let's make our functions. We'll use arrow functions here:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> order = <span class="hljs-function">() =&gt;</span>{};

<span class="hljs-keyword">let</span> production = <span class="hljs-function">() =&gt;</span>{};
</code></pre>
<p>Now, let's establish a relationship between these two functions using a callback, like this: 👇</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> order = <span class="hljs-function">(<span class="hljs-params">call_production</span>) =&gt;</span>{

  call_production();
};

<span class="hljs-keyword">let</span> production = <span class="hljs-function">() =&gt;</span>{};
</code></pre>
<h3 id="heading-lets-do-a-small-test">Let's do a small test</h3>
<p>We'll use the <code>console.log()</code> function to conduct tests to clear up any doubts we might have regarding how we established the relationship between the two functions.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> order = <span class="hljs-function">(<span class="hljs-params">call_production</span>) =&gt;</span>{

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Order placed. Please call production"</span>)

<span class="hljs-comment">// function 👇 is being called </span>
  call_production();
};

<span class="hljs-keyword">let</span> production = <span class="hljs-function">() =&gt;</span>{

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Production has started"</span>)

};
</code></pre>
<p>To run the test, we'll call the <strong><code>order</code></strong> function. And we'll add the second function named <code>production</code> as its argument.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// name 👇 of our second function</span>
order(production);
</code></pre>
<p>Here's the result in our console 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u41ugdxxed1q8coz5hol.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-take-a-break">Take a break</h2>
<p>So far so good – take a break!</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnr74waq6noc0djln3qx.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-clear-out-the-consolelog">Clear out the console.log</h2>
<p>Keep this code and remove everything [don't delete our stocks variable]. On our first function, pass another argument so that we can receive the order [Fruit name]:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Function 1</span>

<span class="hljs-keyword">let</span> order = <span class="hljs-function">(<span class="hljs-params">fruit_name, call_production</span>) =&gt;</span>{

  call_production();
};

<span class="hljs-comment">// Function 2</span>

<span class="hljs-keyword">let</span> production = <span class="hljs-function">() =&gt;</span>{};


<span class="hljs-comment">// Trigger 👇</span>

order(<span class="hljs-string">""</span>, production);
</code></pre>
<p>Here are our steps, and the time each step will take to execute.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rphpp2lqjnk7f0tv5g3d.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>Chart contains steps to make ice cream</strong></em></p>
<p>In this chart, you can see that step 1 is to place the order, which takes 2 seconds. Then step 2 is cut the fruit (2 seconds), step 3 is add water and ice (1 second), step 4 is to start the machine (1 second), step 5 is to select the container (2 seconds), step 6 is to select the toppings (3 seconds) and step 7, the final step, is to serve the ice cream which takes 2 seconds.</p>
<p>To establish the timing, the function <code>setTimeout()</code> is excellent as it is also uses a callback by taking a function as an argument.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwrg1taugyhvjnkx8xpp.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>Syntax of a setTimeout() function</strong></em></p>
<p>Now, let's select our fruit and use this function:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// 1st Function</span>

<span class="hljs-keyword">let</span> order = <span class="hljs-function">(<span class="hljs-params">fruit_name, call_production</span>) =&gt;</span>{

  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{

    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.Fruits[fruit_name]}</span> was selected`</span>)

<span class="hljs-comment">// Order placed. Call production to start</span>
   call_production();
  },<span class="hljs-number">2000</span>)
};

<span class="hljs-comment">// 2nd Function</span>

<span class="hljs-keyword">let</span> production = <span class="hljs-function">() =&gt;</span>{
  <span class="hljs-comment">// blank for now</span>
};

<span class="hljs-comment">// Trigger 👇</span>
order(<span class="hljs-number">0</span>, production);
</code></pre>
<p>And here's the result in the console: 👇</p>
<p><strong>Note</strong> that the result is displayed after 2 seconds.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edwji5vauypoezj3bxdk.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>If you're wondering how we picked strawberry from our stock variable, here's the code with the format 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ia38z3x6b96xpq3aid91.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Don't delete anything. Now we'll start writing our production function with the following code.👇 We'll use arrow functions:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> production = <span class="hljs-function">() =&gt;</span>{

  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"production has started"</span>)
  },<span class="hljs-number">0000</span>)

};
</code></pre>
<p>And here's the result 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yskzvg7rezo2sg4lklq.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We'll nest another <code>setTimeout</code> function in our existing <code>setTimeout</code> function to chop the fruit. Like this: 👇</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> production = <span class="hljs-function">() =&gt;</span>{

  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"production has started"</span>)


    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The fruit has been chopped"</span>)
    },<span class="hljs-number">2000</span>)


  },<span class="hljs-number">0000</span>)
};
</code></pre>
<p>And here's the result 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4659l1mua0rv40rwyem7.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>If you remember, here are our steps:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rphpp2lqjnk7f0tv5g3d.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>Chart contains steps to make ice cream</strong></em></p>
<p>Let's complete our ice cream production by nesting a function inside another function – this is also known as a callback, remember?</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> production = <span class="hljs-function">() =&gt;</span>{

  <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"production has started"</span>)
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"The fruit has been chopped"</span>)
      <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.liquid[<span class="hljs-number">0</span>]}</span> and <span class="hljs-subst">${stocks.liquid[<span class="hljs-number">1</span>]}</span> Added`</span>)
        <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
          <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"start the machine"</span>)
          <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Ice cream placed on <span class="hljs-subst">${stocks.holder[<span class="hljs-number">1</span>]}</span>`</span>)
            <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
              <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.toppings[<span class="hljs-number">0</span>]}</span> as toppings`</span>)
              <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"serve Ice cream"</span>)
              },<span class="hljs-number">2000</span>)
            },<span class="hljs-number">3000</span>)
          },<span class="hljs-number">2000</span>)
        },<span class="hljs-number">1000</span>)
      },<span class="hljs-number">1000</span>)
    },<span class="hljs-number">2000</span>)
  },<span class="hljs-number">0000</span>)

};
</code></pre>
<p>And here's the result in the console 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mq9bg6fqrc8apj7nu7b.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Feeling confused?</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/man5l5pwavp9prio1wc0.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>This is called callback hell. It looks something like this (remember that code just above?): 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5rk7f8d920jzn22smjh.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>Illustration of Callback hell</strong></em></p>
<p>What's the solution to this?</p>
<h1 id="heading-how-to-use-promises-to-escape-callback-hell">How to Use Promises to Escape Callback Hell</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3neys1hxsrifgg5qm6x.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Promises were invented to solve the problem of callback hell and to better handle our tasks.</p>
<h2 id="heading-take-a-break-1">Take a break</h2>
<p>But first, take a break!</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwfvel7kvm422gqvj0os.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>This is how a promise looks:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qo1zheuin2825osozvc.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>illustration of a promise format</strong></em></p>
<p>Let's dissect promises together.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gozy5r1nfubzeq5t5t25.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezi9ogz0ergprgkmu68a.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>An illustration of the life of a promise</strong></em></p>
<p>As the above charts show, a promise has three states:</p>
<ul>
<li><strong>Pending:</strong> This is the initial stage. Nothing happens here. Think of it like this, your customer is taking their time giving you an order. But they haven't ordered anything yet.</li>
<li><strong>Resolved:</strong> This means that your customer has received their food and is happy.</li>
<li><strong>Rejected:</strong> This means that your customer didn't receive their order and left the restaurant.</li>
</ul>
<p>Let's adopt promises to our ice cream production case study.</p>
<h2 id="heading-but-wait-1">But wait...</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/634b6oyglkyoccsvr8l7.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We need to understand four more things first -&gt;</p>
<ul>
<li>Relationship between time and work</li>
<li>Promise chaining</li>
<li>Error handling</li>
<li>The <code>.finally</code> handler</li>
</ul>
<p>Let's start our ice cream shop and understand each of these concepts one by one by taking baby steps.</p>
<h2 id="heading-relationship-between-time-and-work">Relationship between time and work</h2>
<p>If you remember, these are our steps and the time each takes to make ice cream"</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rphpp2lqjnk7f0tv5g3d.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>Chart contains steps to make ice cream</strong></em></p>
<p>For this to happen, let's create a variable in JavaScript: 👇</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> is_shop_open = <span class="hljs-literal">true</span>;
</code></pre>
<p>Now create a function named <code>order</code> and pass two arguments named <code>time, work</code>:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> order = <span class="hljs-function">(<span class="hljs-params"> time, work </span>) =&gt;</span>{

  }
</code></pre>
<p>Now, we're gonna make a promise to our customer, "We will serve you ice-cream" Like this -&gt;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> order = <span class="hljs-function">(<span class="hljs-params"> time, work </span>) =&gt;</span>{

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>( <span class="hljs-function">(<span class="hljs-params"> resolve, reject </span>)=&gt;</span>{ } )

  }
</code></pre>
<p>Our promise has 2 parts:</p>
<ul>
<li>Resolved [ ice cream delivered ]</li>
<li>Rejected [ customer didn't get ice cream ]</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> order = <span class="hljs-function">(<span class="hljs-params"> time, work </span>) =&gt;</span> {

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>( <span class="hljs-function">(<span class="hljs-params"> resolve, reject </span>)=&gt;</span>{

    <span class="hljs-keyword">if</span>( is_shop_open ){

      resolve( )

    }

    <span class="hljs-keyword">else</span>{

      reject( <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Our shop is closed"</span>) )

    }

  })
}
</code></pre>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wik2xel68yue93yapm6.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's add the time and work factors inside our promise using a <code>setTimeout()</code> function inside our <code>if</code> statement. Follow me 👇</p>
<p><strong>Note:</strong> In real life, you can avoid the time factor as well. This is completely dependent on the nature of your work.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> order = <span class="hljs-function">(<span class="hljs-params"> time, work </span>) =&gt;</span> {

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>( <span class="hljs-function">(<span class="hljs-params"> resolve, reject </span>)=&gt;</span>{

    <span class="hljs-keyword">if</span>( is_shop_open ){

      <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{

       <span class="hljs-comment">// work is 👇 getting done here</span>
        resolve( work() )

<span class="hljs-comment">// Setting 👇 time here for 1 work</span>
       }, time)

    }

    <span class="hljs-keyword">else</span>{
      reject( <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Our shop is closed"</span>) )
    }

  })
}
</code></pre>
<p>Now, we're gonna use our newly created function to start ice-cream production.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Set 👇 time here</span>
order( <span class="hljs-number">2000</span>, <span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.Fruits[<span class="hljs-number">0</span>]}</span> was selected`</span>))
<span class="hljs-comment">//    pass a ☝️ function here to start working</span>
</code></pre>
<p>The result 👇 after 2 seconds looks like this:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/erzjup8wt505j502e73n.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Good job!</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8taajvjy6pfq35hu90nq.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-promise-chaining">Promise chaining</h2>
<p>In this method, we defining what we need to do when the first task is complete using the <code>.then</code> handler.  It looks something like this 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l27ytifkoedl22kc97lh.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>Illustration of promise chaining using .then handler</strong></em></p>
<p>The .then handler returns a promise when our original promise is resolved.</p>
<h4 id="heading-heres-an-example">Here's an Example:</h4>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qpeewo19qbhzj47goos.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let me make it simpler: it's similar to giving instructions to someone. You tell someone to " First do this, then do that, then this other thing, then.., then.., then..." and so on.</p>
<ul>
<li>The first task is our original promise.</li>
<li>The rest of the tasks return our promise once one small bit of work is completed</li>
</ul>
<p>Let's implement this on our project. At the bottom of your code write the following lines. 👇</p>
<p><strong>Note:</strong> don't forget to write the <code>return</code> word inside your <code>.then</code> handler. Otherwise, it won't work properly. If you're curious, try removing the return once we finish the steps:</p>
<pre><code class="lang-javascript">order(<span class="hljs-number">2000</span>,<span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.Fruits[<span class="hljs-number">0</span>]}</span> was selected`</span>))

.then(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-keyword">return</span> order(<span class="hljs-number">0000</span>,<span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">'production has started'</span>))
})
</code></pre>
<p>And here's the result: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhhjaakbi6zshxhi6afy.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Using the same system, let's finish our project:👇</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// step 1</span>
order(<span class="hljs-number">2000</span>,<span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.Fruits[<span class="hljs-number">0</span>]}</span> was selected`</span>))

<span class="hljs-comment">// step 2</span>
.then(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-keyword">return</span> order(<span class="hljs-number">0000</span>,<span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">'production has started'</span>))
})

<span class="hljs-comment">// step 3</span>
.then(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-keyword">return</span> order(<span class="hljs-number">2000</span>, <span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Fruit has been chopped"</span>))
})

<span class="hljs-comment">// step 4</span>
.then(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-keyword">return</span> order(<span class="hljs-number">1000</span>, <span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.liquid[<span class="hljs-number">0</span>]}</span> and <span class="hljs-subst">${stocks.liquid[<span class="hljs-number">1</span>]}</span> added`</span>))
})

<span class="hljs-comment">// step 5</span>
.then(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-keyword">return</span> order(<span class="hljs-number">1000</span>, <span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"start the machine"</span>))
})

<span class="hljs-comment">// step 6</span>
.then(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-keyword">return</span> order(<span class="hljs-number">2000</span>, <span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`ice cream placed on <span class="hljs-subst">${stocks.holder[<span class="hljs-number">1</span>]}</span>`</span>))
})

<span class="hljs-comment">// step 7</span>
.then(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-keyword">return</span> order(<span class="hljs-number">3000</span>, <span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.toppings[<span class="hljs-number">0</span>]}</span> as toppings`</span>))
})

<span class="hljs-comment">// Step 8</span>
.then(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-keyword">return</span> order(<span class="hljs-number">2000</span>, <span class="hljs-function">()=&gt;</span><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Serve Ice Cream"</span>))
})
</code></pre>
<p>Here's the result: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0d0f4ys83ctnevkbgxs.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-error-handling">Error handling</h2>
<p>We need a way to handle errors when something goes wrong. But first, we need to understand the promise cycle:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlm7zwonbxszeaccyohv.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2ajcu52rxzwq64g81vp.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>An illustration of the life of a promise</strong></em></p>
<p>To catch our errors, let's change our variable to false.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> is_shop_open = <span class="hljs-literal">false</span>;
</code></pre>
<p>Which means our shop is closed. We're not selling ice cream to our customers anymore.</p>
<p>To handle this, we use the <code>.catch</code> handler. Just like <code>.then</code>, it also returns a promise, but only when our original promise is rejected.</p>
<p>A small reminder here:</p>
<ul>
<li><code>.then</code> works when a promise is resolved</li>
<li><code>.catch</code> works when a promise is rejected</li>
</ul>
<p>Go down to the very bottom and write the following code:👇</p>
<p>Just remember that there should be nothing between your previous <code>.then</code> handler and the <code>.catch</code> handler.</p>
<pre><code class="lang-javascript">.catch(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Customer left"</span>)
})
</code></pre>
<p>Here's the result:👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lot6engklu29y05q8xyr.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>A couple things to note about this code:</p>
<ul>
<li>The 1st message is coming from the <code>reject()</code> part of our promise</li>
<li>The 2nd message is coming from the <code>.catch</code> handler</li>
</ul>
<h2 id="heading-how-to-use-the-finally-handler">How to use the .finally() handler</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdq3i0agj4volq46ycue.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>There's something called the <code>finally</code> handler which works regardless of whether our promise was resolved or rejected.</p>
<p><strong>For example:</strong> whether we serve no customers or 100 customers, our shop will close at the end of the day</p>
<p>If you're curious to test this, come at very bottom and write this code: 👇</p>
<pre><code class="lang-javascript">.finally(<span class="hljs-function">()=&gt;</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"end of day"</span>)
})
</code></pre>
<p>The result:👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2j3jf2uofip1d6y2rtt.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Everyone, please welcome Async / Await~</p>
<h1 id="heading-how-does-async-await-work-in-javascript">How Does Async / Await Work in JavaScript?</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ra7483f90b69pjl2cbae.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>This is supposed to be the better way to write promises and it helps us keep our code simple and clean.</p>
<p>All you have to do is write the word <code>async</code> before any regular function and it becomes a promise.</p>
<h2 id="heading-but-first-take-a-break">But first, take a break</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vujyfxz7dg41jhjtcrx.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's have a look:👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17f08ygj1odk28hgl9eq.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-promises-vs-asyncawait-in-javascript">Promises vs Async/Await in JavaScript</h2>
<p>Before async/await, to make a promise we wrote this:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">order</span>(<span class="hljs-params"></span>)</span>{
   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>( <span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span>{

    <span class="hljs-comment">// Write code here</span>
   } )
}
</code></pre>
<p>Now using async/await, we write one like this:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//👇 the magical keyword</span>
 <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">order</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// Write code here</span>
 }
</code></pre>
<h2 id="heading-but-wait-2">But wait......</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1pjzw6zl0h21tyyh9u3.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>You need to understand -&gt;</p>
<ul>
<li>How to use the <code>try</code> and <code>catch</code> keywords</li>
<li>How to use the await keyword</li>
</ul>
<h2 id="heading-how-to-use-the-try-and-catch-keywords">How to use the Try and Catch keywords</h2>
<p>We use the <code>try</code> keyword to run our code while we use <code>catch</code> to catch our errors. It's the same concept we saw when we looked at promises.</p>
<p>Let's see a comparison. We'll see a small demo of the format, then we'll start coding.</p>
<h3 id="heading-promises-in-js-gt-resolve-or-reject">Promises in JS -&gt; resolve or reject</h3>
<p>We used resolve and reject in promises like this:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">kitchen</span>(<span class="hljs-params"></span>)</span>{

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span> (<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>)=&gt;</span>{
    <span class="hljs-keyword">if</span>(<span class="hljs-literal">true</span>){
       resolve(<span class="hljs-string">"promise is fulfilled"</span>)
    }

    <span class="hljs-keyword">else</span>{
        reject(<span class="hljs-string">"error caught here"</span>)
    }
  })
}

kitchen()  <span class="hljs-comment">// run the code</span>
.then()    <span class="hljs-comment">// next step</span>
.then()    <span class="hljs-comment">// next step</span>
.catch()   <span class="hljs-comment">// error caught here</span>
.finally() <span class="hljs-comment">// end of the promise [optional]</span>
</code></pre>
<h3 id="heading-async-await-in-js-gt-try-catch">Async / Await in JS -&gt; try, catch</h3>
<p>When we're using async/await, we use this format:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//👇 Magical keyword</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">kitchen</span>(<span class="hljs-params"></span>)</span>{

   <span class="hljs-keyword">try</span>{
<span class="hljs-comment">// Let's create a fake problem      </span>
      <span class="hljs-keyword">await</span> abc;
   }

   <span class="hljs-keyword">catch</span>(error){
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"abc does not exist"</span>, error)
   }

   <span class="hljs-keyword">finally</span>{
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Runs code anyways"</span>)
   }
}

kitchen()  <span class="hljs-comment">// run the code</span>
</code></pre>
<p>Don't panic, we'll discuss the <code>await</code> keyword next.</p>
<p>Now hopefully you understand the difference between promises and Async / Await.</p>
<h2 id="heading-how-to-use-javascripts-await-keyword">How to Use JavaScript's Await Keyword</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fry577xha7313ead96xy.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>The keyword <code>await</code> makes JavaScript wait until a promise settles and returns its result.</p>
<h3 id="heading-how-to-use-the-await-keyword-in-javascript">How to use the await keyword in JavaScript</h3>
<p>Let's go back to our ice cream shop. We don't know which topping a customer might prefer, chocolate or peanuts. So we need to stop our machine and go and ask our customer what they'd like on their ice cream.</p>
<p>Notice here that only our kitchen is stopped, but our staff outside the kitchen will still do things like:</p>
<ul>
<li>doing the dishes</li>
<li>cleaning the tables</li>
<li>taking orders, and so on.</li>
</ul>
<h2 id="heading-an-await-keyword-code-example">An Await Keyword Code Example</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8r5w5aapofalnq882wat.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's create a small promise to ask which topping to use. The process takes three seconds.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toppings_choice</span> (<span class="hljs-params"></span>)</span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve,reject</span>)=&gt;</span>{
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">()=&gt;</span>{

      resolve( <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"which topping would you love?"</span>) )

    },<span class="hljs-number">3000</span>)
  })
}
</code></pre>
<p>Now, let's create our kitchen function with the async keyword first.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">kitchen</span>(<span class="hljs-params"></span>)</span>{

  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"A"</span>)
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"B"</span>)
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"C"</span>)

  <span class="hljs-keyword">await</span> toppings_choice()

  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"D"</span>)
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"E"</span>)

}

<span class="hljs-comment">// Trigger the function</span>

kitchen();
</code></pre>
<p>Let's add other tasks below the <code>kitchen()</code> call.</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"doing the dishes"</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"cleaning the tables"</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"taking orders"</span>)
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0dr669gewtrrd5fd86p.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We are literally going outside our kitchen to ask our customer, "what is your topping choice?" In the mean time, other things still get done.</p>
<p>Once, we get their topping choice, we enter the kitchen and finish the job.</p>
<h3 id="heading-small-note">Small note</h3>
<p>When using Async/ Await, you can also use the <code>.then</code>, <code>.catch</code>, and <code>.finally</code>  handlers as well which are a core part of promises.</p>
<h3 id="heading-lets-open-our-ice-cream-shop-again">Let's open our Ice cream shop again</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzw8gp721oecwo2b3l6s.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We're gonna create two functions -&gt;</p>
<ul>
<li><code>kitchen</code>: to make ice cream</li>
<li><code>time</code>: to assign the amount of time each small task will take.</li>
</ul>
<p>Let's start! First, create the time function:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> is_shop_open = <span class="hljs-literal">true</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">time</span>(<span class="hljs-params">ms</span>) </span>{

   <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>( <span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> {

      <span class="hljs-keyword">if</span>(is_shop_open){
         <span class="hljs-built_in">setTimeout</span>(resolve,ms);
      }

      <span class="hljs-keyword">else</span>{
         reject(<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Shop is closed"</span>))
      }
    });
}
</code></pre>
<p>Now, let's create our kitchen:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">kitchen</span>(<span class="hljs-params"></span>)</span>{
   <span class="hljs-keyword">try</span>{

     <span class="hljs-comment">// instruction here</span>
   }

   <span class="hljs-keyword">catch</span>(error){
    <span class="hljs-comment">// error management here</span>
   }
}

<span class="hljs-comment">// Trigger</span>
kitchen();
</code></pre>
<p>Let's give small instructions and test if our kitchen function is working or not:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">kitchen</span>(<span class="hljs-params"></span>)</span>{
   <span class="hljs-keyword">try</span>{

<span class="hljs-comment">// time taken to perform this 1 task</span>
     <span class="hljs-keyword">await</span> time(<span class="hljs-number">2000</span>)
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.Fruits[<span class="hljs-number">0</span>]}</span> was selected`</span>)
   }

   <span class="hljs-keyword">catch</span>(error){
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Customer left"</span>, error)
   }

   <span class="hljs-keyword">finally</span>{
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Day ended, shop closed"</span>)
    }
}

<span class="hljs-comment">// Trigger</span>
kitchen();
</code></pre>
<p>The result looks like this when the shop is open: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lptup827qau72e83deuv.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>The result looks like this when the shop is closed: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8pjz1qlw58ap8pq7crz.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>So far so good.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnkgk63x51wth2byxzfe.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's complete our project.</p>
<p>Here's the list of our tasks again: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wthn0jr5vw7vb02e4qg.png" alt="Alt Text" width="600" height="400" loading="lazy">
<em><strong>Chart contains steps to make ice cream</strong></em></p>
<p>First, open our shop</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> is_shop_open = <span class="hljs-literal">true</span>;
</code></pre>
<p>Now write the steps inside our <code>kitchen()</code> function: 👇</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">kitchen</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">try</span>{
    <span class="hljs-keyword">await</span> time(<span class="hljs-number">2000</span>)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.Fruits[<span class="hljs-number">0</span>]}</span> was selected`</span>)

    <span class="hljs-keyword">await</span> time(<span class="hljs-number">0000</span>)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"production has started"</span>)

    <span class="hljs-keyword">await</span> time(<span class="hljs-number">2000</span>)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"fruit has been chopped"</span>)

    <span class="hljs-keyword">await</span> time(<span class="hljs-number">1000</span>)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.liquid[<span class="hljs-number">0</span>]}</span> and <span class="hljs-subst">${stocks.liquid[<span class="hljs-number">1</span>]}</span> added`</span>)

    <span class="hljs-keyword">await</span> time(<span class="hljs-number">1000</span>)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"start the machine"</span>)

    <span class="hljs-keyword">await</span> time(<span class="hljs-number">2000</span>)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`ice cream placed on <span class="hljs-subst">${stocks.holder[<span class="hljs-number">1</span>]}</span>`</span>)

    <span class="hljs-keyword">await</span> time(<span class="hljs-number">3000</span>)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${stocks.toppings[<span class="hljs-number">0</span>]}</span> as toppings`</span>)

    <span class="hljs-keyword">await</span> time(<span class="hljs-number">2000</span>)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Serve Ice Cream"</span>)
    }

    <span class="hljs-keyword">catch</span>(error){
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"customer left"</span>)
    }
}
</code></pre>
<p>And here's the result: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qs9yccq9209u7m9lquju.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Congratulations for reading until the end! In this article you've learned: </p>
<ul>
<li>The difference between synchronous and asynchronous systems</li>
<li>Mechanisms of asynchronous JavaScript using 3 techniques (callbacks, promises, and Async/ Await)</li>
</ul>
<p>Here's your medal for reading until the end. ❤️</p>
<h3 id="heading-suggestions-and-criticisms-are-highly-appreciated">Suggestions and criticisms are highly appreciated ❤️</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><strong>YouTube <a target="_blank" href="https://youtube.com/c/joyshaheb">/ Joy Shaheb</a></strong></p>
<p><strong>LinkedIn <a target="_blank" href="https://www.linkedin.com/in/joyshaheb/">/ JoyShaheb</a></strong></p>
<p><strong>Twitter <a target="_blank" href="https://twitter.com/JoyShaheb">/ JoyShaheb</a></strong></p>
<p><strong>Instagram <a target="_blank" href="https://www.instagram.com/joyshaheb/">/ JoyShaheb</a></strong></p>
<h1 id="heading-credits">Credits</h1>
<ul>
<li><a target="_blank" href="https://www.freepik.com/user/collections/promises-article/2046500">Collection of all the images used</a></li>
<li><a target="_blank" href="https://www.flaticon.com/packs/unicorn-4">Unicorns</a>, <a target="_blank" href="https://www.flaticon.com/packs/kitty-avatars-3">kitty avatar</a></li>
<li><a target="_blank" href="https://www.pexels.com/photo/brown-tabby-cat-with-slice-of-loaf-bread-on-head-4587955/">tabby cat</a>, <a target="_blank" href="https://www.pexels.com/photo/young-female-astrologist-predicting-future-with-shining-ball-6658693/">Astrologist Woman</a>, <a target="_blank" href="https://www.pexels.com/photo/woman-in-white-dress-holding-white-flower-bouquet-3981511/">girl-holding-flower</a></li>
<li><a target="_blank" href="https://www.vecteezy.com/vector-art/180695-people-mind-emotion-character-cartoon-vector-illustration">Character emotions</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ CSS Flexbox Tutorial with Flexbox Properties Cheat Sheet 🎖️ ]]>
                </title>
                <description>
                    <![CDATA[ In this article, I'll teach you CSS Flexbox basics so you can make your own responsive sites. I'll explain how each of Flexbox's properties work, and I'll give you a cheatsheet that covers everything you can do with Flexbox. Let's Go 🎖️ Table of Con... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/css-flexbox-tutorial-with-cheatsheet/</link>
                <guid isPermaLink="false">66b2094f4562dc719c606edf</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ flexbox ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Wed, 28 Apr 2021 19:33:57 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/04/FCC--Thumbnail-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, I'll teach you <strong>CSS Flexbox</strong> basics so you can make your own responsive sites. I'll explain how each of Flexbox's properties work, and I'll give you a cheatsheet that covers everything you can do with Flexbox. Let's Go 🎖️</p>
<h1 id="heading-table-of-contents">Table of Contents</h1>
<ul>
<li><a class="post-section-overview" href="#heading-flexbox-architecture">Flexbox Architecture</a></li>
<li><a class="post-section-overview" href="#heading-flex-direction-property">flex-direction</a></li>
<li><a class="post-section-overview" href="#heading-justify-content-property">justify-content</a></li>
<li><a class="post-section-overview" href="#heading-align-content-property">align-content</a></li>
<li><a class="post-section-overview" href="#heading-align-items-property">align-items</a></li>
<li><a class="post-section-overview" href="#heading-align-self-property">align-self</a></li>
<li><a class="post-section-overview" href="#heading-flex-grow-shrink-wrap-basis-properties">flex - grow | shrink | wrap | basis</a></li>
<li><a class="post-section-overview" href="#heading-shorthand-flexbox-properties">Shorthands</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ul>
<h3 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like">You can watch this tutorial on YouTube as well if you like:</h3>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/7YUR0Igl9eU" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-first-what-is-flexbox">First, What is Flexbox?</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/Frame-4--2-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>When you're building a house, you need a blueprint. In the same way, we need a blueprint when we're making websites. And Flexbox is the blueprint.</p>
<p>The Flexbox model allows us to <strong>layout the content</strong> of our website. Not only that, it helps us create the structures needed for creating <strong>responsive websites</strong> for multiple devices. </p>
<p>Here's a demo which I created using Flexbox as the main blueprint.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/Frame-35--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>This project is a <a target="_blank" href="https://www.freecodecamp.org/news/learn-css-media-queries-by-building-projects/">part of this article</a>.</p>
<h1 id="heading-flexbox-architecture">Flexbox Architecture</h1>
<p>So how does Flexbox architecture work? The flex-items [Contents] are distributed along the main axis and cross axis. And, depending on the flex-direction property, the layout position changes between rows and columns.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/hy2oqjvsbk60ef92nktg.png" alt="Flex Box model Architecture" width="600" height="400" loading="lazy"></p>
<h1 id="heading-flexbox-chart">Flexbox Chart</h1>
<p>This chart contains every possible property and value you can use when you're working with Flexbox. You can reference it while doing your projects and experiment with different values.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/gv3jyh4xt4fbwtq1qejn.png" alt="Flex Box property Value Chart" width="600" height="400" loading="lazy"></p>
<h1 id="heading-how-to-set-up-the-project">How to Set Up the Project</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jphj7d3c3oh6dvx40ogy.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>For this project, you need to know little bit of HTML, CSS, and how to work with VS code. Follow along with me as we complete the following tasks:</p>
<ol>
<li>Create a folder named "Project-1" &amp; Open VS Code</li>
<li>Create <code>index.html</code> and <code>style.css</code> files</li>
<li>Install Live Server and run it.</li>
</ol>
<p>Or, you can just open <a target="_blank" href="https://codepen.io/">Codepen</a> and start coding.</p>
<p>At the end of this tutorial, you will be able to make accurate and beautiful website layouts.</p>
<h2 id="heading-html">HTML</h2>
<p>In HTML, write these lines of code inside the body tag 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> A <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-2"</span>&gt;</span> B <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-3"</span>&gt;</span> C <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-css">CSS</h2>
<p>Target the <code>.container</code> class and all the boxes. Then style the boxes so that all of them look similar, like this: 👇</p>
<p><strong>Note:</strong> don't forget to put the height of the container.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}

<span class="hljs-selector-attr">[class ^=<span class="hljs-string">"box-"</span>]</span>{
    <span class="hljs-attribute">width</span>: <span class="hljs-number">140px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">140px</span>;
    <span class="hljs-attribute">background-color</span>: skyblue;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;

// To view the letter better
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">65px</span>;
}
</code></pre>
<h2 id="heading-but-wait">But Wait....</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cq8exwor5aiciu2j6jwu.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Before starting, you need to understand the relationship between parent and child classes.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzq3w0bys78fveqb9z0z.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Flexbox works on the <strong>parent class</strong>, not on the child classes.</p>
<p>Here, the <code>.container</code> class is the <strong>parent</strong> and our <code>.box-*</code> classes are our <strong>children</strong>.</p>
<p>So, apply the display: flex inside the <code>.container</code> class. And place the letters at the center of the box like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">display </span>: flex;
    <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;

// To place some gap between boxes
    <span class="hljs-attribute">gap </span>: <span class="hljs-number">25px</span>;
}
<span class="hljs-selector-attr">[class ^=<span class="hljs-string">"box-"</span>]</span>{
// Code from previous step are here

// Placing text at center 
    <span class="hljs-attribute">display </span>: flex;
    <span class="hljs-attribute">justify-content </span>: center;
    <span class="hljs-attribute">align-items </span>: center;
}
</code></pre>
<p>And...we're all set! Let's start coding. 😊</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4ufu718jzjopwn66ik8.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h1 id="heading-flex-direction-property">flex-direction property</h1>
<p>This property allows us to set the direction and orientation in which our flex-items should be distributed inside the flex-container.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/n2ggh6yy4sbgltrx2i40.png" alt="Flex Direction" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/6m9fg4n5a114q1va3b9p.png" alt="Flex Direction" width="600" height="400" loading="lazy"></p>
<p>To recreate these results, let's write these lines in our CSS:</p>
<p><strong>Please note</strong> that we'll write them inside the <code>.container</code> class.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
//code from setup stage are here

// Change the value  👇 here to see results
    <span class="hljs-attribute">flex-direction </span>: row;
}
</code></pre>
<h1 id="heading-justify-content-property">justify-content property</h1>
<p>This property arranges flex-items along the <strong>MAIN AXIS</strong> inside the flex-container.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/a5lhkhbhi7hxwjgyvl5x.png" alt="justify content" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/1vyg5nf1w7plistni582.png" alt="justify content" width="600" height="400" loading="lazy"></p>
<p>To recreate these results, write these lines in your CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
//code from setup stage are here

//  Change the value  👇 here to see results
    <span class="hljs-attribute">justify-content</span>: flex-start;
}
</code></pre>
<h1 id="heading-align-content-property">align-content property</h1>
<p>This property arranges flex-items along the <strong>CROSS AXIS</strong> inside the flex-container. This is similar to <strong>justify-content</strong>.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/nqvvc2rhf0vx3czy0rnr.png" alt="align content" width="600" height="400" loading="lazy"></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/zeet3705rsmz77v66x3c.png" alt="align content" width="600" height="400" loading="lazy"></p>
<p>Please note that without the <strong>flex-wrap</strong> property, this property doesn't work. Here's a demo:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{

//  Change the value  👇 here to see results
    <span class="hljs-attribute">align-content</span>: center;

// without this line, align-content won't work
    <span class="hljs-attribute">flex-wrap</span>: wrap;
}
</code></pre>
<h1 id="heading-align-items-property">align-items property</h1>
<p>This property distributes Flex-items along the <strong>Cross Axis</strong>.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/kt25wxicd7vm8ddtmq0l.png" alt="align items" width="600" height="400" loading="lazy"></p>
<p>To recreate these results, let's write the following code in CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
//code from setup stage are here

// Change the value 👇 here to see results
    <span class="hljs-attribute">align-items</span>: flex-end;
}
</code></pre>
<h1 id="heading-align-self-property">align-self property</h1>
<p>This property works on the child classes. It positions the selected item along the <strong>Cross Axis</strong>.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/383cxj4ippb21vjq31q2.png" alt="align self" width="600" height="400" loading="lazy"></p>
<p>In total we have 6 values:</p>
<ul>
<li>flex-start</li>
<li>flex-end</li>
<li>center</li>
<li>baseline</li>
<li>stretch</li>
<li>auto</li>
</ul>
<p>To recreate the results, select any <code>.box-*</code> and write the following code:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-2</span>{
// Change the value 👇 here to see results
     <span class="hljs-attribute">align-self </span>: center;
}
</code></pre>
<h2 id="heading-take-a-break">Take a Break</h2>
<p>So far so good. Take a break!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/Frame-45.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-flex-grow-shrink-wrap-basis-properties">flex - grow | shrink | wrap | basis properties</h1>
<p>The properties we'll discuss now will work when we resize the window. Let's dive right in.</p>
<h3 id="heading-flex-grow">flex-grow</h3>
<p>This property grows the size of a flex-item based on the width of the flex-container.</p>
<h3 id="heading-flex-shrink">flex-shrink</h3>
<p>This property helps a flex item shrink based on the width of the flex-container. It's the opposite of flex-grow.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/z094e3wehsoe8z6lsxnz.png" alt="flex grow flex shrink flex wrap" width="600" height="400" loading="lazy"></p>
<p>To achieve these results, follow me.</p>
<p><strong>Please note</strong> that flex-grow and flex-shrink work on child classes. So, we will target all our boxes like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
    <span class="hljs-attribute">flex-grow</span>: <span class="hljs-number">1</span>;
}
<span class="hljs-selector-class">.box-2</span>{
    <span class="hljs-attribute">flex-grow</span>: <span class="hljs-number">5</span>;
}
<span class="hljs-selector-class">.box-1</span>{
    <span class="hljs-attribute">flex-grow</span>: <span class="hljs-number">1</span>;
}
</code></pre>
<p>Resize the window and you'll see the results.</p>
<p>To duplicate the result of flex-shrink, write the following code:</p>
<p><strong>Please note</strong> that you need to delete the flex-wrap property first, otherwise it won't work.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-1</span>{
    <span class="hljs-attribute">flex-shrink</span>: <span class="hljs-number">1</span>;
}
<span class="hljs-selector-class">.box-2</span>{
    <span class="hljs-attribute">flex-shrink</span>: <span class="hljs-number">5</span>;
}
<span class="hljs-selector-class">.box-1</span>{
    <span class="hljs-attribute">flex-shrink</span>: <span class="hljs-number">1</span>;
}
</code></pre>
<p>Now, resize the window and you'll see the results.</p>
<h3 id="heading-flex-wrap">flex-wrap</h3>
<p>This property helps you set the number of flex-items you want in a line or row.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/fux9qc05e6rtat192vlm.png" alt="flex wrap flex grow flex shrink" width="600" height="400" loading="lazy"></p>
<p>This works on the <code>.container</code> parent class. So, write the following code:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
    //other codes are here 

// Change value 👇 here to see results
    <span class="hljs-attribute">flex-wrap </span>: wrap;
</code></pre>
<h3 id="heading-flex-basis">flex-basis</h3>
<p>This is similar to adding width to a flex-item, but only more flexible. flex-basis: 10em, for example, will set the initial size of a flex-item to 10em. Its final size will be based on the available space, flex-grow, and flex-shrink.</p>
<h1 id="heading-shorthand-flexbox-properties">Shorthand Flexbox Properties</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/eayovne50iwxx8ll5e01.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-flex-shorthand">flex shorthand</h3>
<p>This is the shorthand for the <strong>flex-grow</strong>, <strong>flex-shrink</strong> and <strong>flex-basis</strong> properties combined.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/onoxj7gs9xj4wuf87kjl.png" alt="flex flex basis" width="600" height="400" loading="lazy"></p>
<p>You can try this by writing the following code:</p>
<p><strong>Please note</strong> that it only works on the child classes:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box-2</span>{
    <span class="hljs-attribute">flex </span>: <span class="hljs-number">2</span> <span class="hljs-number">1</span> <span class="hljs-number">30em</span>;
}
</code></pre>
<h3 id="heading-flex-flow">flex-flow</h3>
<p>This is the shorthand for the <strong>flex-direction</strong> and <strong>flex-wrap</strong> properties:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/awniqyrepbha5jdquwxh.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>You can try this by writing the following code:</p>
<p><strong>Please note</strong> that it only works on the parent class.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">flex-flow </span>: row wrap;
}
</code></pre>
<h2 id="heading-place-content">place-content</h2>
<p>This is the shorthand for the justify-content and align-content properties:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72yaytxgighz0cjskp2e.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's duplicate the results:</p>
<p><strong>Please note</strong> that it works on the parent class.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">place-content </span>: center flex-end;
}
</code></pre>
<h2 id="heading-more-resources">More Resources</h2>
<p>If you want to <strong>exercise</strong> your Flexbox knowledge, you can <a target="_blank" href="https://www.freecodecamp.org/news/learn-flexbox-build-5-layouts/">read this article of mine</a> where you'll be <strong>building five responsive layouts using F</strong>lexbox. Here's a demo:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/Frame-5--2--1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Here's your medal for reading till the end ❤️</p>
<h3 id="heading-suggestions-amp-criticisms-are-highly-appreciated">Suggestions &amp; Criticisms Are Highly Appreciated ❤️</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><strong>YouTube <a target="_blank" href="https://www.youtube.com/c/joyshaheb">/ Joy Shaheb</a></strong></p>
<p><strong>Twitter <a target="_blank" href="https://twitter.com/JoyShaheb">/ JoyShaheb</a></strong></p>
<p><strong>Instagram <a target="_blank" href="https://www.instagram.com/joyshaheb/">/ JoyShaheb</a></strong></p>
<h2 id="heading-credits">Credits</h2>
<ul>
<li><p><a target="_blank" href="https://www.flaticon.com/packs/unicorn-4">unicorn pack</a>, <a target="_blank" href="https://www.flaticon.com/packs/kitty-avatars-3">Kitty Avatars</a></p>
</li>
<li><p><a target="_blank" href="https://www.flaticon.com/free-icon/cat_1864514?term=kitty&amp;page=1&amp;position=12&amp;page=1&amp;position=12&amp;related_id=1864514&amp;origin=search">cat-1</a>, <a target="_blank" href="https://www.flaticon.com/free-icon/cat_3629088?related_id=3629088">cat-2</a>, <a target="_blank" href="https://www.flaticon.com/free-icon/shiba_1623792?term=dog&amp;related_id=1623792">dog</a>, <a target="_blank" href="https://www.flaticon.com/free-icon/rabbit_1807972?term=rabbit&amp;page=1&amp;position=13&amp;page=1&amp;position=13&amp;related_id=1807972&amp;origin=search">rabbit</a></p>
</li>
<li><p><a target="_blank" href="https://www.freepik.com/free-vector/cute-astronaut-holding-coffee-cup-cartoon-illustration-science-food-drink-icon-concept-flat-cartoon-style_10479412.htm#position=0">Astronaut</a>, <a target="_blank" href="https://www.vecteezy.com/vector-art/668079-little-pony-in-coffee-cup">unicorn cup</a>, <a target="_blank" href="https://www.vecteezy.com/vector-art/668109-rainbow-cat-unicorn-mermaid">rainbow cat</a></p>
</li>
<li><p><a target="_blank" href="https://css-tricks.com/snippets/css/a-guide-to-flexbox/">CSS Tricks</a></p>
</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/uzo5e477tn0sc4oz3mec.png" alt="Credits" width="600" height="400" loading="lazy"></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn CSS Media Queries by Building Three Projects ]]>
                </title>
                <description>
                    <![CDATA[ Today we're going to learn how to use CSS Media Queries to build responsive websites. And we'll practice what we learn by completing three projects. Let's go 🏅 Table of Contents What are CSS Media Queries? Steps to follow The Syntax Practice Projec... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-css-media-queries-by-building-projects/</link>
                <guid isPermaLink="false">66b2096252d7e556f48445f3</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ responsive design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ responsive images ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Mon, 26 Apr 2021 15:21:29 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/04/FCC-Thumbnail.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're going to learn how to use CSS Media Queries to build responsive websites. And we'll practice what we learn by completing three projects. Let's go 🏅</p>
<h1 id="heading-table-of-contents">Table of Contents</h1>
<ul>
<li><a class="post-section-overview" href="#heading-what-are-css-media-queries">What are CSS Media Queries?</a></li>
<li><a class="post-section-overview" href="#heading-how-to-set-up-the-project">Steps to follow</a></li>
<li><a class="post-section-overview" href="#heading-css-media-query-syntax">The Syntax</a></li>
<li><a class="post-section-overview" href="#heading-lets-build-some-projects-using-css-media-queries">Practice Projects</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ul>
<p>Topics to discuss at a glance:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7vrrjohwnmdvbtexg1r2.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-you-can-watch-this-tutorial-on-youtube-as-well-if-you-like">You can watch this tutorial on YouTube as well if you like:</h2>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/HY8q4TD3KGM" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-what-are-css-media-queries">What are CSS Media Queries?</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kyjadc0b95rjuzjlcgck.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>CSS Media Queries allow you to create responsive websites across all screen sizes, ranging from desktop to mobile. So you can see why it's important to learn this topic.</p>
<p>Here's a demo of the magic of Media Queries 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bb0qwxrhg0705lvs6ihs.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We'll build this in project 2 below. This layout is called the <strong>Card Layout</strong>. You can see more Layout Examples <a target="_blank" href="https://csslayout.io/patterns/">here!</a></p>
<h1 id="heading-how-to-set-up-the-project">How to Set Up the Project</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wa8xgu2o74y2d60cq3g.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>For this project, you need to know little bit of HTML, CSS, and how to work with VS code. Follow along with me -&gt;</p>
<ol>
<li>Create a folder named "Project-1"</li>
<li>Open VS Code</li>
<li>Create <strong>index.html, style.scss,</strong> and <strong>main.js</strong> files</li>
<li>Install Live Server and SASS Compiler</li>
<li>Run Live Server and SASS Compiler</li>
</ol>
<h2 id="heading-html">HTML</h2>
<p>In HTML, write this code inside the body tag 👇</p>
<pre><code class="lang-html"> <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span> = <span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We also need to see the exact size of our window. Here's a demo of what I mean:</p>
<p><img src="https://media.giphy.com/media/06zg3tXmCsfA6hX5zo/giphy.gif" alt="Demo" width="600" height="400" loading="lazy"></p>
<p>So, write this line inside the html file:</p>
<pre><code class="lang-html">  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"size"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-what-is-scss">What is SCSS?</h2>
<p>We'll Use SCSS, not CSS. But..... what is SCSS?</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5a3hp7070ls26dovja2.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>SCSS is a pre-processor of CSS which is more powerful than regular CSS. Using SCSS we can -&gt;</p>
<ol>
<li>Nest our selectors like a branch of a tree and better manage our code.</li>
<li>Store various values into variables</li>
<li>Use Mixins to stop code repetition and save time</li>
</ol>
<p>And much more!</p>
<p>In our SCSS, we'll remove our default browser settings, and we'll change box-sizing, font-size, and font-family like this: 👇</p>
<pre><code class="lang-scss">*{
  <span class="hljs-attribute">margin</span> : <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">padding</span> : <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">box-sizing</span> : border-box; 

  <span class="hljs-selector-tag">body</span>{
    <span class="hljs-attribute">font-size</span> : <span class="hljs-number">35px</span>;
    <span class="hljs-attribute">font-family</span> : sans-serif;
  }
}
</code></pre>
<p><strong>Don't forget</strong> to set the <strong>height</strong> of the <strong>.container class.</strong> Otherwise we'll fail to achieve our desired results:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">height</span> : <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>Remember the additional id we wrote in HTML? We'll style it and position it in our browser here:</p>
<pre><code class="lang-scss"><span class="hljs-selector-id">#size</span> {
  <span class="hljs-attribute">position</span>: absolute;

<span class="hljs-comment">// positioning screen size below our main text</span>
  <span class="hljs-attribute">top</span> : <span class="hljs-number">60%</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">50%</span>;

  <span class="hljs-attribute">transform</span>: translateX(-<span class="hljs-number">50%</span>);

  <span class="hljs-attribute">color</span> : red;
  <span class="hljs-attribute">font-size</span> : <span class="hljs-number">35px</span>;
}
</code></pre>
<h2 id="heading-javascript">JavaScript</h2>
<p>We need to update our screen size inside our id every time we resize our window. So, write this code in your <code>main.js</code> file:</p>
<pre><code class="lang-javascript">
<span class="hljs-comment">// 'screen' is name 👇 of a function</span>
<span class="hljs-built_in">window</span>.onresize = screen;
<span class="hljs-built_in">window</span>.onload = screen;

<span class="hljs-comment">// Function named 'screen' 👇</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">screen</span>(<span class="hljs-params"></span>) </span>{
  Width = <span class="hljs-built_in">window</span>.innerWidth;
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"size"</span>).innerHTML 
   = <span class="hljs-string">"Width : "</span> + Width + <span class="hljs-string">" px"</span> 
}
</code></pre>
<h2 id="heading-download-the-images-for-the-project">Download the images for the project</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u72rvfe5181640ikqa32.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Responsive website also means <strong>Responsive Images</strong>. So we're also going to make our images responsive in this project. The images are on my <strong><a target="_blank" href="https://github.com/JoyShaheb/Project-image-repo/tree/main/Media-Query-Project">GitHub repository</a></strong>. Here's how to get them:</p>
<ol>
<li>Visit and copy the link above ☝️</li>
<li>Go to <strong><a target="_blank" href="https://minhaskamal.github.io/DownGit/#/home">downgit</a></strong> and paste the link you copied</li>
<li>Follow the steps in this video 👇</li>
</ol>
<p><img src="https://cloud.githubusercontent.com/assets/5456665/17822364/940bded8-6678-11e6-9603-b84d75bccec1.gif" alt="Down Git Steps to follow" width="600" height="400" loading="lazy"></p>
<p>And.... we're all set! Let's start coding. 😊</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5qj78v41btdq7sewdzs.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h1 id="heading-css-media-query-syntax">CSS Media Query Syntax</h1>
<p>Here's the syntax of a Media Query:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (max-width: <span class="hljs-number">768px</span>){
  <span class="hljs-selector-class">.container</span>{
   <span class="hljs-comment">//Your code's here</span>
  }
}
</code></pre>
<p>And here's an illustrated explanation -&gt;</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1wvg9w8co1hq3d24uxi.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's divide the syntax into four sections:</p>
<ol>
<li>Media Query Declaration</li>
<li>The Media Type</li>
<li>min-width &amp; max-width Functions</li>
<li>The Code itself</li>
</ol>
<h3 id="heading-to-understand-all-4-section-of-the-syntax-lets-start-our-first-project">To understand all 4 section of the syntax, let's start our <strong>First Project</strong>:</h3>
<p><img src="https://media.giphy.com/media/t4QOOfmnupAqnHcI9H/giphy.gif" alt="Project-1 Video" width="600" height="400" loading="lazy"></p>
<p>We'll build this. ☝️ It's a small project where the background-color changes on resizing the window by taking one small step at a time. Let's start!</p>
<h3 id="heading-the-html">The HTML</h3>
<p>Place the following code inside your HTML, like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span> = <span class="hljs-string">"container"</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span> = <span class="hljs-string">"text"</span>&gt;</span>
      Hello Screen !
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-the-scss">The SCSS</h3>
<p>Now, we'll store four color codes inside variables like this:👇</p>
<pre><code class="lang-scss"><span class="hljs-variable">$color-1</span> : <span class="hljs-number">#cdb4db</span> ; <span class="hljs-comment">// Mobile</span>
<span class="hljs-variable">$color-2</span> : <span class="hljs-number">#fff1e6</span> ; <span class="hljs-comment">// Tablet</span>
<span class="hljs-variable">$color-3</span> : <span class="hljs-number">#52b788</span> ; <span class="hljs-comment">// Laptop</span>
<span class="hljs-variable">$color-4</span> : <span class="hljs-number">#bee1e6</span> ; <span class="hljs-comment">// Desktop</span>
</code></pre>
<p>You can find more colors at <a target="_blank" href="https://coolors.co/palettes/trending">coolors.co</a> if you want to choose your own.</p>
<p>Now, at the bottom, target the <code>.container</code> and <code>.text</code> classes. We'll also center our text like this👇</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
<span class="hljs-comment">//To place text at center</span>

  <span class="hljs-attribute">display</span> : grid;
  place-items : center;

  <span class="hljs-attribute">background-color</span> : <span class="hljs-variable">$color-1</span>;
  <span class="hljs-attribute">height</span> : <span class="hljs-number">100vh</span>;
}

<span class="hljs-selector-class">.text</span>{
 <span class="hljs-comment">// keep it blank for now</span>
}
</code></pre>
<p>So far so good!</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grc7r9mbpw7yvoisgj6c.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-1-how-to-declare-media-queries">1. How to declare media queries</h2>
<p>Media Queries start with the <code>@media</code> declaration. The main purpose of writing this is to <strong>tell the browser</strong> that we have specified a media query. In your CSS, write it like this:👇</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span>
</code></pre>
<h2 id="heading-2-how-to-set-the-media-type">2. How to Set the Media Type</h2>
<p>This is used to specify the nature of the device we're working with. The four values are:</p>
<ul>
<li>all</li>
<li>print</li>
<li>screen</li>
<li>speech</li>
</ul>
<p>Here's the purpose of each of the values at a glance 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ea8yqchxnmdlqyq9es0m.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We declare the <strong>media type</strong> after the <code>@media</code> declaration, like this:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> screen
</code></pre>
<h2 id="heading-why-do-we-write-the-and-operator">Why do we write the "and" operator?</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8qezvhk9m896ns97jta.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's say we're placing an order at a restaurant, "A burger <strong>and</strong> a pizza". Notice that the two orders are separated by <strong>[and]</strong>.</p>
<p>Likewise, the media type, min-width, and max-width functions are basically conditions we are giving to the browser. We don't write the <strong>"and"</strong> operator if we have one condition. Like this -&gt;</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> screen {
  <span class="hljs-selector-class">.container</span>{
     <span class="hljs-comment">// Your code here </span>
  }
}
</code></pre>
<p>We write the <strong>and</strong> operator if we have two conditions, like this:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (max-width : <span class="hljs-number">768px</span>) {
  <span class="hljs-selector-class">.container</span>{
     <span class="hljs-comment">// Your code here </span>
  }
}
</code></pre>
<p>You can also skip the media type and work with just min-width &amp; max-width, like this:</p>
<pre><code class="lang-scss"><span class="hljs-comment">//Targeting screen sizes between 480px &amp; 768px </span>

<span class="hljs-keyword">@media</span> (min-width : <span class="hljs-number">480px</span>) <span class="hljs-keyword">and</span> (max-width : <span class="hljs-number">768px</span>) {
  <span class="hljs-selector-class">.container</span>{
     <span class="hljs-comment">// Your code here </span>
  }
}
</code></pre>
<p>If you have three conditions or more, you can use a <strong>comma</strong>, like this:</p>
<pre><code class="lang-scss"><span class="hljs-comment">//Targeting screen sizes between 480px &amp; 768px </span>

<span class="hljs-keyword">@media</span> screen, (min-width : <span class="hljs-number">480px</span>) <span class="hljs-keyword">and</span> (max-width : <span class="hljs-number">768px</span>) {
  <span class="hljs-selector-class">.container</span>{
     <span class="hljs-comment">// Your code here </span>
  }
}
</code></pre>
<h2 id="heading-3-how-to-use-the-min-width-amp-max-width-functions">3. How to Use the min-width &amp; max-width Functions</h2>
<p>Let's discuss the Most important component of a media query, screen breakpoints.</p>
<p>To be honest, there's no such thing as a standard screen break-point guide because there are so many screen sizes on the market. But, for our project, we'll follow <a target="_blank" href="https://getbootstrap.com/docs/5.0/layout/breakpoints/">The Official Bootstrap 5</a> screen break-point values. Here they are:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7pqqlkxksxgouje83vhw.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Here's a list of every device screen resolution on <a target="_blank" href="https://css-tricks.com/snippets/css/media-queries-for-standard-devices/">CSS-Tricks</a>.</p>
<h3 id="heading-the-max-width-function">The max-width function:</h3>
<p>Using this function, we are creating a boundary. This will work as long as we are <strong>inside the boundary</strong>. Here's a sample 👇</p>
<p>Our Boundary is 500px:</p>
<p><img src="https://media.giphy.com/media/50L0raPo5ZSdxCOlmP/giphy.gif" alt="max-width" width="600" height="400" loading="lazy"></p>
<p>Notice how the light purple color gets <strong>Disabled</strong> when we hit above 500px.</p>
<p>To recreate this, write this code in SCSS:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-color</span>: white ;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
  <span class="hljs-attribute">display</span>: grid;
  place-items: center;
}
</code></pre>
<p>At the bottom, insert the media query like this 👇</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (max-width : <span class="hljs-number">500px</span>){
  <span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$color-1</span>;
  }
}
</code></pre>
<h3 id="heading-the-min-width-function">The min-width function:</h3>
<p>We are also creating a boundary here. But this will work if we go <strong>outside the boundary</strong>. Here's a sample: 👇</p>
<p>Our boundary is 500px:</p>
<p><img src="https://media.giphy.com/media/iThpfWPRTBSQXn2gpO/giphy.gif" alt="Min-width" width="600" height="400" loading="lazy"></p>
<p>Notice how the light purple color gets <strong>enabled</strong> after we hit above 500px width.</p>
<p>To recreate this, write this code in SCSS:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-color</span>: white ;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
  <span class="hljs-attribute">display</span>: grid;
  place-items: center;
}
</code></pre>
<p>At the bottom, insert the media query like this: 👇</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (min-width : <span class="hljs-number">500px</span>){
  <span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$color-1</span>;
  }
}
</code></pre>
<p>To sum it up, remember that:</p>
<ul>
<li><strong>max-width</strong> sets styles inside the set boundary</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/227jg6drq6faqc47e2ox.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<ul>
<li><strong>min-width</strong> sets styles outside the set boundary</li>
</ul>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8ds85udh6l20hdh6lbm.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-the-code-itself">The code itself</h2>
<p>Let's put our first project together!</p>
<p>We will have four screen breakpoints:</p>
<ul>
<li>Mobile  -&gt; 576px</li>
<li>Tablet  -&gt; 768px</li>
<li>Laptop  -&gt; 992px</li>
<li>Desktop -&gt; 1200px</li>
</ul>
<p>Yes, we are following the official <a target="_blank" href="https://getbootstrap.com/docs/5.0/layout/breakpoints/">bootstrap 5</a> screen breakpoints. And each breakpoint will get these colors:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d42pdgggmovcrsq8p61n.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>For four device types, we will have four Media Queries. Before touching the media queries, first let's store the breakpoint values in variables, like this:</p>
<p><strong>Note:</strong> Don't forget to add the <strong>$</strong> sign:</p>
<pre><code class="lang-scss"><span class="hljs-variable">$mobile</span>  : <span class="hljs-number">576px</span>;
<span class="hljs-variable">$tablet</span>  : <span class="hljs-number">768px</span>;
<span class="hljs-variable">$laptop</span>  : <span class="hljs-number">992px</span>; 
<span class="hljs-variable">$desktop</span> : <span class="hljs-number">1200px</span>;
</code></pre>
<p>And our <code>.container</code> class should look like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-color</span>: white ;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
  <span class="hljs-attribute">display</span>: grid;
  place-items: center;
}
</code></pre>
<p>We're all 50% done! Now let's setup the four media queries.</p>
<h2 id="heading-but-wait">But Wait...</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/net2cuugxdaz9idwbwhl.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>You need to follow the proper order while writing the media queries. Start writing from the <strong>largest display to the smallest display.</strong></p>
<h3 id="heading-first-breakpoint-for-desktop-1200px">First breakpoint for desktop – 1200px</h3>
<p>For the desktop screen, write this code in SCSS:👇</p>
<pre><code class="lang-scss"><span class="hljs-comment">// using variable here which is  👇 1200px</span>
<span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (max-width: <span class="hljs-variable">$desktop</span>){
  <span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$color-4</span>;
  }
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1cuuy0m4zw57sh9zdzf.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-second-breakpoint-for-laptop-992px">Second breakpoint for laptop – 992px</h3>
<p>For laptop screens, write this code in SCSS: 👇</p>
<pre><code class="lang-scss"><span class="hljs-comment">// using variable here which is  👇 992px</span>
<span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (max-width: <span class="hljs-variable">$laptop</span>){
  <span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$color-3</span>;
  }
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fng9y622xtb9pa6ngfwj.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-third-breakpoint-for-tablet-768px">Third breakpoint for tablet – 768px</h3>
<p>For tablets screens, write this code in SCSS: 👇</p>
<pre><code class="lang-scss"><span class="hljs-comment">// using variable here which is  👇 768px</span>
<span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (max-width: <span class="hljs-variable">$tablet</span>){
  <span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$color-2</span>;
  }
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wqp9xjt4gg272pc3hkt.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-fourth-breakpoint-for-mobile-576px">Fourth breakpoint for mobile – 576px</h3>
<p>For mobile screens, write this code in SCSS: 👇</p>
<pre><code class="lang-scss"><span class="hljs-comment">// using variable here which is  👇 576px</span>
<span class="hljs-keyword">@media</span> screen <span class="hljs-keyword">and</span> (max-width : <span class="hljs-variable">$mobile</span>){
  <span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">background-color</span>: <span class="hljs-variable">$color-1</span>;
  }
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z10764qm7cjjc8a2yb7j.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-take-a-break">Take a Break</h2>
<p>Congratulations on completing project 1! Now <strong>take a break. You deserve it.</strong></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6j0uyekswj04ag3p2cl.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h1 id="heading-lets-build-some-projects-using-css-media-queries">Let's build some projects using CSS Media Queries</h1>
<h2 id="heading-how-to-build-a-responsive-portfolio">How to Build a Responsive Portfolio</h2>
<p>We'll build a small responsive Website for our second project.</p>
<h3 id="heading-heres-what-the-desktop-view-will-look-like">Here's what the desktop view will look like:</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ay01oqdseulalsw3gpdh.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-and-heres-the-mobile-view">And here's the mobile view:</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hob05vxov52hrm5quz0a.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Okay then, let's start coding! First, let's work with the desktop view by taking small baby steps.</p>
<h3 id="heading-before-we-start">Before we start</h3>
<p>Create a folder named 'images' inside our Project-1 Folder. Place all the images you downloaded from my <a target="_blank" href="https://github.com/JoyShaheb/Project-image-repo/tree/main/Media-Query-Project">GitHub Repository</a> inside the images folder.</p>
<h2 id="heading-the-html-1">The HTML</h2>
<h3 id="heading-step-1-create-the-sections">Step 1 – Create the sections</h3>
<p>We'll create three sections for our website. Write this code in your HTML:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span> 

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"footer"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-step-2-logo-and-menu-items">Step 2 – Logo and menu items</h3>
<p>We'll place the logo and menu items inside the .header div, like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header"</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header__logo"</span>&gt;</span>Miya Ruma<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header__menu"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header__menu-1"</span>&gt;</span> Home <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header__menu-2"</span>&gt;</span> Portfolio <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header__menu-3"</span>&gt;</span> Contacts <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-step-3-image-and-text">Step 3 – Image and text</h3>
<p>We'll place the image and text inside the .main div, like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main"</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main__image"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main__text"</span>&gt;</span>

       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main__text-1"</span>&gt;</span>Hello 👋<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main__text-2"</span>&gt;</span>I'm <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Miya Ruma<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main__text-3"</span>&gt;</span>A Designer From<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"main__text-4"</span>&gt;</span>Tokyo, Japan<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-step-4-social-media-icons">Step 4 – Social media icons</h3>
<p>We'll place the social media icons inside the .footer div, like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"footer"</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"footer__instagram"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./images/instagram.png"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"footer__twitter"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./images/twitter-sign.png"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"footer__dribbble"</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./images/dribbble-logo.png"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"footer__behance"</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./images/behance.png"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-the-scss-1">The SCSS</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ou3jrlk5g9mjodfh88n.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step-1-update-the-scss">Step 1 – Update the SCSS</h3>
<p>Delete everything inside our SCSS and write this code instead:</p>
<pre><code class="lang-scss">* {
  <span class="hljs-comment">// placing Margin to left &amp; right</span>
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span> <span class="hljs-number">5px</span>;

  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;

  <span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">font-family</span>: sans-serif;
  }
}
</code></pre>
<p>This is what we have so far:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3egdxy9f5wf8xgc2ekn8.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step-2-select-all-classes-in-html">Step 2 – Select all classes in HTML</h3>
<p>Select all the classes we created in HTML on our stylesheet.</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{}

<span class="hljs-selector-class">.header</span>{}

<span class="hljs-selector-class">.main</span>{}

<span class="hljs-selector-class">.footer</span>{}
</code></pre>
<h3 id="heading-step-3-select-all-children">Step 3 – Select all children</h3>
<p>Now select all the children of the parent classes.</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.header</span>{

  &amp;__logo{}

  &amp;__menu{}
}

<span class="hljs-selector-class">.main</span>{

  &amp;__image{}

  &amp;__text{}
}

<span class="hljs-selector-class">.footer</span>{

  <span class="hljs-selector-attr">[class ^="footer__"]</span>{}

}
</code></pre>
<p><strong>Note</strong> that <code>&amp;__logo</code> nested inside <code>.header</code> is a shortcut of <code>.header__logo</code>.</p>
<h3 id="heading-step-4-define-the-container">Step 4 – Define the .container</h3>
<p>Define the <code>.container</code> for the desktop layout, like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{

<span class="hljs-comment">// Defining height</span>
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

  <span class="hljs-attribute">display</span>: flex;

  <span class="hljs-attribute">flex-direction</span>: column;
}
</code></pre>
<p>Apply <code>display: flex;</code> to <code>.header</code> and to the menu items so that it behaves like a row, not a column:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.header</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;

  &amp;__logo{}

  &amp;__menu{
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">flex-direction</span>: row;
  }
}
</code></pre>
<p>Divide each section and create borders to see what we are doing:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.header</span>{
  <span class="hljs-attribute">display</span>: flex;

<span class="hljs-comment">// The border &amp; height</span>
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid red;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">10%</span>;

<span class="hljs-comment">// Other selectors are here</span>

}

<span class="hljs-selector-class">.main</span>{

<span class="hljs-comment">//The border &amp; height</span>
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">80%</span>;

<span class="hljs-comment">// Other selectors are here</span>

}

<span class="hljs-selector-class">.footer</span>{

<span class="hljs-comment">// Border &amp; height</span>
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid green;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">10%</span>;

<span class="hljs-comment">// Other selectors are here</span>
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o77rk1bj2m722jf41mju.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step-5-complete-header-styling">Step 5 – Complete .header styling</h3>
<p>Let's complete the styling of our <code>.header</code> section using flex-box properties and the appropriate font-size:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.header</span> {
<span class="hljs-comment">// height</span>
  <span class="hljs-attribute">height</span>: <span class="hljs-number">10%</span>;

  <span class="hljs-attribute">display</span>: flex;
<span class="hljs-comment">// Aligning logo &amp; menu at center</span>
  <span class="hljs-attribute">align-items</span>: center;

<span class="hljs-comment">// space between logo &amp; menu</span>
  <span class="hljs-attribute">justify-content</span>: space-between;

  &amp;__logo {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">4vw</span>;
  }

  &amp;__menu {
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2.5vw</span>;

<span class="hljs-comment">// to put gap between menu items</span>
    gap: <span class="hljs-number">15px</span>;
  }
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx4d43xmeggdaw2h2pdf.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step-6-add-the-image">Step 6 – Add the image</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tyojtcpe7o1o9mukum6.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's add the image inside <code>.main</code> section and create a partition for image and text.</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.main</span> {
  <span class="hljs-comment">// image &amp; text will act like a row</span>
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;

  <span class="hljs-comment">//The border &amp; height</span>
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">80%</span>;

  &amp;__image {
    <span class="hljs-comment">//Adding the image</span>
    <span class="hljs-attribute">background-image</span>: url(<span class="hljs-string">"./images/Portrait.png"</span>);
    <span class="hljs-comment">// will cover half of screen width</span>
    <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
  }

  &amp;__text {
    <span class="hljs-comment">// will cover half of screen width</span>
    <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
  }
}
</code></pre>
<p>The result is a bit ugly at the moment, but don't lose hope~</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q13at8wcniamqpwh7jv.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step7-make-the-image-responsive">Step7 – Make the image responsive</h3>
<p>Style the image to be responsive like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.main</span>{
  &amp;__image{
  <span class="hljs-comment">//make image fluid</span>
    <span class="hljs-attribute">background-size</span>: contain;

  <span class="hljs-comment">// stop image repetition</span>
    <span class="hljs-attribute">background-repeat</span>: no-repeat;

  <span class="hljs-comment">// position the image</span>
    <span class="hljs-attribute">background-position</span>: left center;
  }
}
</code></pre>
<p>And here's what we have so far:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24r955u9wwww9vdwwhk5.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>The image is responsive all the way from <strong>4k</strong> to your <strong>smart watch screen</strong>. Don't believe me? Open chrome developer tools and test it yourself and see.</p>
<p>You can learn more about <a target="_blank" href="https://www.freecodecamp.org/news/learn-css-background-properties/">Background Properties here</a> if you want to make responsive images for responsive websites.</p>
<p><img src="https://media.giphy.com/media/7Us5yEqyNW6IkOR1fs/giphy.gif" alt="4k test" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step-8-style-the-text">Step 8 – Style the text</h3>
<p>Let's style our text now. First, we'll bring it to the exact center with this code:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.main</span>{

  &amp;__text {
    <span class="hljs-comment">// will cover half of screen width</span>
    <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">flex-direction</span>: column;

<span class="hljs-comment">// To bring it at the center </span>
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">align-items</span>: center;
  }

<span class="hljs-comment">// To color The name </span>
  <span class="hljs-selector-tag">span</span>{
    <span class="hljs-attribute">color</span>: red;
  }

}
</code></pre>
<p>Now, let's set font sizes for the text:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.main</span>{


  &amp;__text{

<span class="hljs-comment">// To add gaps between texts vertically</span>
    gap: <span class="hljs-number">15px</span>;

<span class="hljs-comment">// font size for "hello"</span>
    &amp;-1{
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">10vw</span>;
    }

<span class="hljs-comment">// font size for other texts</span>
    &amp;-2,&amp;-3,&amp;-4{
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">5vw</span>;

    }

  }
}
</code></pre>
<p>The result looks like this:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kne4bezy1ft0ro6ore0p.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>At this point, you can remove all the borders we placed inside our header, main, and footer classes.</p>
<h3 id="heading-step-9-the-footer-section">Step 9 – The footer section</h3>
<p>First, resize the images like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.footer</span>{
  <span class="hljs-selector-attr">[class^="footer__"]</span> {
    <span class="hljs-selector-tag">img</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">5.3vw</span>;
    }
  }
}
</code></pre>
<p>Then, position the images to our desired place, with a small gap between the icons, like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.footer</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;

<span class="hljs-comment">// To align icons along x-axis</span>
  <span class="hljs-attribute">align-items</span>: center;
<span class="hljs-comment">// placing image to the right side</span>
  <span class="hljs-attribute">justify-content</span>: flex-end;
<span class="hljs-comment">// Gap between icons</span>
  gap: <span class="hljs-number">20px</span>;

<span class="hljs-comment">// margin to right side of icons </span>
  <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">10%</span>;
}
</code></pre>
<p>And here's the result, without the guides:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itzk2rwz621vjm1k833c.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step-10-the-mobile-layout">Step 10 – The mobile layout</h3>
<p>We're almost there...</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9w8h1owma15wmrzqb5cd.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Create a media query at the 650px mark and style the <code>.header</code> class like this:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> (max-width: <span class="hljs-number">650px</span>) {

  <span class="hljs-selector-class">.header</span> {

<span class="hljs-comment">// To place logo at center</span>
    <span class="hljs-attribute">justify-content</span>: center;

    &amp;__logo {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
    }
<span class="hljs-comment">//hiding the menu on mobile device</span>
    &amp;__menu {
      <span class="hljs-attribute">display</span>: none;
    }
  }
}
</code></pre>
<h3 id="heading-step-11-center-main">Step 11 – Center .main</h3>
<p>Now, place the .main section at the exact center with this code:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> (max-width: <span class="hljs-number">650px</span>){
<span class="hljs-comment">// styles of header section of step-10...</span>

<span class="hljs-comment">// main section here </span>
  <span class="hljs-selector-class">.main</span> {
    <span class="hljs-attribute">flex-direction</span>: column;
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">align-items</span>: center;
}
</code></pre>
<h3 id="heading-step-12-style-the-image-and-text-for-mobile">Step 12 – Style the image and text for mobile</h3>
<p>Style the image and text for mobile layout like this:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> (max-width: <span class="hljs-number">650px</span>){

 <span class="hljs-selector-class">.main</span> {
   &amp;__image {
<span class="hljs-comment">// Image size </span>
      <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
      <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
      <span class="hljs-attribute">background-size</span>: <span class="hljs-number">100%</span>;

<span class="hljs-comment">// To have rounded image </span>
      <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">100%</span>;
      <span class="hljs-attribute">background-position</span>: center;
    }

<span class="hljs-comment">// Styles for the text -&gt;</span>
    &amp;__text {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;

      &amp;-1 {
        <span class="hljs-attribute">display</span>: none;
      }
      &amp;-2, &amp;-3, &amp;-4 {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
      }
    }
}
</code></pre>
<h3 id="heading-step-13-style-the-footer-for-mobile">Step 13 – Style the footer for mobile</h3>
<p>The last step is to style the footer section for the mobile layout:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> (max-width: <span class="hljs-number">650px</span>){
  <span class="hljs-selector-class">.footer</span> {
<span class="hljs-comment">// placing icons along the X-axis</span>
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span>;

    <span class="hljs-selector-attr">[class^="footer__"]</span> {

<span class="hljs-comment">// Resizing images for mobile layout</span>
      <span class="hljs-selector-tag">img</span> {
        <span class="hljs-attribute">width</span>: <span class="hljs-number">45px</span>;
        <span class="hljs-attribute">height</span>: <span class="hljs-number">45px</span>;
      }
    }
  }
}
</code></pre>
<p>And here's our result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dxzqtz8it2xhzpxf4ll.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-take-a-break-1">Take a break</h2>
<p>Good job so far! Take a break before moving on to the next project. 😊</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdd7i01tivcfgg4nki0m.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-project-3-how-to-build-a-card-layout">Project 3 – How to Build a Card Layout</h2>
<p>In project 3, we'll build this:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tf2o5njarct4ew24dnqj.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>So let's start.</p>
<h2 id="heading-the-scss-2">The SCSS</h2>
<p>On your stylesheet, delete everything except the styles of <code>#size</code>. And then add this code there:</p>
<pre><code class="lang-scss">* {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span> <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;

  <span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">font-family</span>: sans-serif;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">55px</span>;
  }
}

<span class="hljs-selector-id">#size</span>{
  <span class="hljs-attribute">position</span>: absolute;
<span class="hljs-comment">// Positioning the text</span>
  <span class="hljs-attribute">top</span>: <span class="hljs-number">60%</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">50%</span>;
  <span class="hljs-attribute">transform</span>: translateX(-<span class="hljs-number">50%</span>);
<span class="hljs-comment">// color &amp; size of text</span>
  <span class="hljs-attribute">color</span>: red;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">40px</span>;
}
</code></pre>
<h2 id="heading-the-html-2">The HTML</h2>
<p>Your HTML should look like this inside the body tags: 👇</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span> 
   // We'll place code here
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

// This will show our window width Live 
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"size"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Now, create three classes with class names <code>.row-*</code> like this 👇 inside <code>.container</code>:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span> 

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row-1"</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row-2"</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row-3"</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Each row will have three boxes with class names <code>.box-*</code> like this. 👇 And yes, you'll insert letters inside the boxes:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span> 

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row-1"</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span>A<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-2"</span>&gt;</span>B<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-3"</span>&gt;</span>C<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row-2"</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-4"</span>&gt;</span>D<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-5"</span>&gt;</span>E<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-6"</span>&gt;</span>F<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row-3"</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-7"</span>&gt;</span>G<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-8"</span>&gt;</span>H<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-9"</span>&gt;</span>I<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We're done with the HTML part, and the result should look like this: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3jg1iphmhefdpn8gy12.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-the-scss-3">The SCSS</h2>
<p>Follow these small baby steps one by one to style the project.</p>
<h3 id="heading-step-1-add-some-scss-code">Step 1 – Add some SCSS code</h3>
<p>To select and style all the boxes and rows together, this is what we write in our CSS: 👇</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-comment">// styles here </span>
}

<span class="hljs-selector-attr">[class ^="row-"]</span>{
  <span class="hljs-comment">// Styles applied on all rows</span>
}

<span class="hljs-selector-attr">[class ^="box-"]</span>{
  <span class="hljs-comment">// Styles applied on all boxes</span>
}
</code></pre>
<h3 id="heading-step-2-make-boxes-behave-like-rows">Step 2 – Make boxes behave like rows</h3>
<p>Boxes should behave like a row. This code will make that happen:</p>
<pre><code class="lang-scss"><span class="hljs-selector-attr">[class ^="row-"]</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;
}
</code></pre>
<p>And here's the result: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4utfjrr1bfmwwh81rb1a.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step-3-define-the-boxes">Step 3 – Define the boxes</h3>
<p>Expand the boxes across the width and height and place the letters at the center.</p>
<pre><code class="lang-scss"><span class="hljs-selector-attr">[class ^="box-"]</span>{

  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#c4c4c4</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid black;

<span class="hljs-comment">// Defining the size of the boxes </span>
  <span class="hljs-attribute">width</span> : (<span class="hljs-number">100%</span>)/<span class="hljs-number">3</span>;
  <span class="hljs-attribute">height</span>: (<span class="hljs-number">100vh</span>)/<span class="hljs-number">3</span>;

<span class="hljs-comment">// Place letter at the center</span>
  <span class="hljs-attribute">display</span>: grid;
  place-items: center;
}
</code></pre>
<p>Here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4m7snz4kklns6xjinzh.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step-4-create-gaps-between-rows">Step 4 – Create gaps between rows</h3>
<p>Next we'll create a gap between the rows, like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: column;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;

<span class="hljs-comment">// Creating gap between rows </span>
  gap: <span class="hljs-number">30px</span>;
}
</code></pre>
<p>Now let's create a gap between the boxes:</p>
<pre><code class="lang-scss"><span class="hljs-selector-attr">[class ^="row-"]</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;

<span class="hljs-comment">// Creating gap between boxes</span>
  gap : <span class="hljs-number">30px</span>;
}
</code></pre>
<p>And here's what it looks like:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpus9dx40nxzysz9iceh.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-step-5-set-up-the-mobile-layout">Step 5 – Set up the mobile layout</h3>
<p>Create the media query which will be applied at the 650px mark:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> (max-width: <span class="hljs-number">650px</span>){
  <span class="hljs-comment">// We'll write code here</span>
}
</code></pre>
<p>Change the orientation of the boxes on the mobile screen from row to column, and stretch the boxes to 100% of the width with this code:</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@media</span> (max-width: <span class="hljs-number">650px</span>){

<span class="hljs-comment">//Change orientation</span>
  <span class="hljs-selector-attr">[class ^="row-"]</span>{
    <span class="hljs-attribute">flex-direction</span>: column;
  }

<span class="hljs-comment">// Change width of boxes</span>
  <span class="hljs-selector-attr">[class ^="box-"]</span>{
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  }
}
</code></pre>
<p>And here's the final mobile result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pyis7sloasdv03lljhn9.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>By the way, project 2 is a part of <a target="_blank" href="https://www.freecodecamp.org/news/learn-flexbox-build-5-layouts/">this article</a> of mine. If you're interested to learn more and practice both your Flexbox and media query skills, then go for it!</p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Here's your medal for reading all the way until the end ❤️</p>
<h3 id="heading-suggestions-and-criticisms-are-highly-appreciated">Suggestions and criticisms are highly appreciated ❤️</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><strong>YouTube <a target="_blank" href="https://youtube.com/c/joyshaheb">/ Joy Shaheb</a></strong></p>
<p><strong>Twitter <a target="_blank" href="https://twitter.com/JoyShaheb">/ JoyShaheb</a></strong></p>
<p><strong>Instagram <a target="_blank" href="https://www.instagram.com/joyshaheb/">/ JoyShaheb</a></strong></p>
<h2 id="heading-credits">Credits</h2>
<ul>
<li><a target="_blank" href="https://css-tricks.com/a-complete-guide-to-css-media-queries/">CSS Tricks</a></li>
<li><a target="_blank" href="https://www.pexels.com/photo/woman-wearing-brown-bucket-cap-732425/">Portrait</a> used for the example project</li>
<li><a target="_blank" href="https://www.vecteezy.com/members/joyshaheb/collections/blog-idea-1">Images from Vecteesy</a></li>
<li><a target="_blank" href="https://www.freepik.com/free-vector/cute-panda-hug-boba-milk-tea-cartoon-icon-illustration-animal-drink-icon-concept-premium-flat-cartoon-style_12571361.htm#position=0">Panda</a>,  <a target="_blank" href="https://www.freepik.com/free-vector/kawaii-fast-food-cute-ice-cream-cookie-illustration_5769154.htm#position=1">Ice-cream</a> &amp; <a target="_blank" href="https://www.freepik.com/free-vector/cute-cats-set-funny-character-cartoon-illustration_12566246.htm">Cute Cat</a></li>
<li><a target="_blank" href="https://www.flaticon.com/packs/unicorn-4">Unicorn Pack</a> &amp; <a target="_blank" href="https://www.flaticon.com/packs/kitty-avatars-3">Kitty Avatar</a></li>
<li><a target="_blank" href="https://www.flaticon.com/free-icon/instagram_1384031">instagram</a>,<a target="_blank" href="https://www.flaticon.com/free-icon/twitter-sign_25347">Twitter</a>,<a target="_blank" href="https://www.flaticon.com/free-icon/behance_254383">Behance</a> and <a target="_blank" href="https://www.flaticon.com/free-icon/dribbble-logo_87400">Dribbble icons</a></li>
<li><a target="_blank" href="https://www.freepik.com/free-vector/collection-kawaii-bubble-tea_10048123.htm#position=6">Bubble tea</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Every CSS Background Property Illustrated and Explained with Code Examples 🎖️ ]]>
                </title>
                <description>
                    <![CDATA[ Today we're gonna learn about every single CSS background property with every possible value. We'll learn the short-hand, too. Let's go !🏅 Table of Contents All properties background-image background-size background-repeat background-position backg... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-css-background-properties/</link>
                <guid isPermaLink="false">66b2095ff31aa965000e588e</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Fri, 02 Apr 2021 18:55:22 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/03/FreeCodeCamp--1-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Today we're gonna learn about every single CSS <strong>background</strong> property with every possible <strong>value</strong>. We'll learn the <strong>short-hand</strong>, too. Let's go !🏅</p>
<h1 id="heading-table-of-contents">Table of Contents</h1>
<ul>
<li><a class="post-section-overview" href="#heading-all-properties">All properties</a></li>
<li><a class="post-section-overview" href="#background-image">background-image</a></li>
<li><a class="post-section-overview" href="#background-size">background-size</a></li>
<li><a class="post-section-overview" href="#background-repeat">background-repeat</a></li>
<li><a class="post-section-overview" href="#background-position">background-position</a></li>
<li><a class="post-section-overview" href="#background-origin">background-origin</a></li>
<li><a class="post-section-overview" href="#background-clip">background-clip</a></li>
<li><a class="post-section-overview" href="#background-attachment">background-attachment</a></li>
<li><a class="post-section-overview" href="#background-color">background-color</a></li>
<li><a class="post-section-overview" href="#short-hand">Short hand</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ul>
<p>You can watch this tutorial on YouTube as well if you like:</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/hwJKjsZUPjY" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h1 id="heading-all-properties">All Properties</h1>
<p>This is a list of <strong>All the properties</strong> we're gonna discuss today. The red text at the end is the <strong>shorthand</strong>.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l25y304vndphll4795hr.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-what-are-css-background-properties">What Are CSS Background Properties?</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iic3rs5ewx8c9xp6vryq.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>CSS background properties let us to control the size and properties of images so that we can make <strong>responsive images</strong> for both smaller and larger screens. This in turn helps us create responsive websites.</p>
<p>For example,</p>
<ul>
<li>The property <strong>background-size</strong> allows us to reset the width and height of our image according to screen size.</li>
<li><strong>background-position</strong> allows us to tell the browser where to put the image on the screen.</li>
</ul>
<p>And many more!</p>
<h2 id="heading-how-to-set-up-the-project">How to Set Up the Project</h2>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u72rvfe5181640ikqa32.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Before coding, you need to know little bit of HTML, CSS, and how to use VS code.</p>
<p>To test out the properties and their values, follow these steps 👇</p>
<ol>
<li>Create a new folder named 'BACKGROUND-PROJECT'. Open it in VS code.</li>
<li>Create <code>index.html</code> and <code>style.css</code> files.</li>
<li>Install 'live server' on VS code.</li>
<li>Start live server.</li>
</ol>
<h2 id="heading-html">HTML</h2>
<p>Create one div with the class name 'container' inside the <strong>body tag</strong> in the HTML file.</p>
<pre><code class="lang-html">   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h2 id="heading-css">CSS</h2>
<p>In CSS, you <strong>MUST</strong> include a height for the container, otherwise we won't be able to see the image. In our case, we will set it to 100vh, like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h2 id="heading-download-the-images-for-the-project">Download the images for the project.</h2>
<p>The images are on my <strong><a target="_blank" href="https://github.com/JoyShaheb/Project-image-repo/tree/main/Background-property-images">GitHub repository</a></strong>. Here's how to get them:</p>
<ol>
<li>Visit and copy the link above ☝️</li>
<li>Go to <a target="_blank" href="https://minhaskamal.github.io/DownGit/#/home">downgit</a> and paste the link you copied</li>
<li>Follow the steps in this video 👇 </li>
</ol>
<p><img src="https://cloud.githubusercontent.com/assets/5456665/17822364/940bded8-6678-11e6-9603-b84d75bccec1.gif" alt="Down Git Video" width="600" height="400" loading="lazy"></p>
<p>And..... we're all set!</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmf39ig7wzdiunfje9lr.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Let's start coding 😊</p>
<h1 id="heading-the-css-background-image-property">The CSS background-image property</h1>
<p>Using this property, we can add images <strong>throughout our stylesheet.</strong></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhoch2auowlf2xdf4h8f.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We write the syntax after writing the selector name, like this:👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// We'll put image path/URL 👇 inside quotes
   <span class="hljs-attribute">background-image  </span>:  <span class="hljs-built_in">url</span>(<span class="hljs-string">' '</span>);
}
</code></pre>
<p>We can use background-image in <strong>2 ways:</strong></p>
<ul>
<li>By locating <strong>image path</strong> in the directory</li>
<li>By specifying the <strong>image URL</strong></li>
</ul>
<h3 id="heading-how-to-use-background-image-through-the-directory-path">How to Use <code>background-image</code> through the Directory Path</h3>
<p>Here's the syntax for background-image when using the directory path 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
//  Put  image  path  👇 inside quotes
  <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">' '</span>);
}
</code></pre>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jfuda4p0ki1hish775o.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>There are three cases when you'll want to specify an image path in our CSS:</p>
<ol>
<li>When <code>image</code> and <code>style.css</code> are in the same folder</li>
<li>When <code>image</code> is in the next folder</li>
<li>When <code>image</code> is in the previous folder</li>
</ol>
<p>When <code>image</code> and <code>style.css</code> are in the <strong>Same Folder</strong>, it looks something like the below. 👇 </p>
<p>Notice that <strong><code>kitty.png</code></strong> and <strong><code>style.css</code></strong> are in the same parent folder named <strong>Background-project</strong>:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/Frame-25--1--1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>To locate the file path of the <code>kitty.png</code>, write the following code in <code>style.css</code>:</p>
<pre><code class="lang-css"> <span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">"kitty.png"</span>);

   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
// set size &amp; stop image repetition 
   <span class="hljs-attribute">background-repeat </span>: no-repeat;
   <span class="hljs-attribute">background-size </span>: contain;
 }
</code></pre>
<p>When the image is in the <strong>Next Folder</strong>, <code>style.css</code> is in previous folder. Notice on the image below that <code>kitty.png</code> is in the Assets folder while <code>style.css</code> is in the previous folder.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/Frame-26.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>To go forward and locate the file path of <code>kitty.png</code>, we write one dot and slash like this (./) after the quote in <code>style.css</code>. Then we write the name of the folder then slash (/) and finally we write the name of image, like this:👇 </p>
<pre><code class="lang-css"> <span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">"./Assets/kitty.png"</span>);

   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
// set size &amp; stop image repetition 
   <span class="hljs-attribute">background-repeat </span>: no-repeat;
   <span class="hljs-attribute">background-size </span>: contain;
 }
</code></pre>
<p>If the image is in the <strong>Previous Folder</strong>, then we need to go back. Notice in the image below👇 that <code>style.css</code> is in the <strong>src</strong> folder and <code>kitty.png</code> is <strong>outside the src folder.</strong></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/04/Frame-27.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>To go back and locate the file path of <code>kitty.png</code>, we write two dots and a slash (../) after the quote in <code>style.cs</code>. Then we write the name of the image, like this: 👇</p>
<pre><code class="lang-css"> <span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">"../kitty.png"</span>);

   <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
// set size &amp; stop image repetition 
   <span class="hljs-attribute">background-repeat </span>: no-repeat;
   <span class="hljs-attribute">background-size </span>: contain;
 }
</code></pre>
<h3 id="heading-how-to-use-background-image-by-direct-link">How to Use <code>background-image</code> by Direct Link</h3>
<p>This is pretty easy. Write the property and insert the link inside <code>url()</code>. </p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szxp3jqyjyksrep1ep82.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>To work with an image which is a <strong>direct link,</strong> we need to write the following code:</p>
<pre><code class="lang-css">//<span class="hljs-selector-tag">example</span> <span class="hljs-selector-tag">-</span>&gt;
 <span class="hljs-selector-class">.container</span>{
    <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">"https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szxp3jqyjyksrep1ep82.png"</span>);

  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
// set size &amp; stop image repetition 
   <span class="hljs-attribute">background-repeat </span>: no-repeat;
   <span class="hljs-attribute">background-size </span>: contain;
 }
</code></pre>
<h3 id="heading-take-a-break">Take a Break</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4diremjrrbvcm2o4l77m.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h1 id="heading-the-css-background-size-property">The CSS background-size property</h1>
<p>We can adjust the size of an image using the <code>background-size</code> property.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxbrgckb20fy8tmrg9ik.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We write the syntax after writing the selector name, like this  👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// We'll write values 👇 here
  <span class="hljs-attribute">background-size  </span>: cover;
}
</code></pre>
<p>You can use background-size in <strong>3 ways</strong>:</p>
<ul>
<li>use the Cover / Contain value</li>
<li>set the image width and height</li>
<li>use auto</li>
</ul>
<p>Let's start by discussing the <strong>cover &amp; contain values</strong>.<br>Bear size : [718px X 614px]</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uixn5c8mrafpmlhth9iy.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-cover-value">Cover value</h3>
<p>For this to work, we must include an image, set the height, and stop image repetition. We do that like this in CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'cute-bear.png'</span>);
  <span class="hljs-attribute">background-repeat</span>: no-repeat;
  <span class="hljs-attribute">background-size </span>: cover;

// Must include the height
  <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>When we use this property, it will stretch the image to the whole screen even when we resize the window. Watch the video below to see how it looks:👇</p>
<p><img src="https://media.giphy.com/media/9OdZ0B1wjO1kdofBBu/giphy.gif" alt="Cover" width="600" height="400" loading="lazy"></p>
<h3 id="heading-the-contain-value">The contain value</h3>
<p>Same steps here – we must include an image, set its height, and stop image repetition like this:👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'cute-bear.png'</span>);
  <span class="hljs-attribute">background-repeat</span>: no-repeat;
  <span class="hljs-attribute">background-size </span>: contain;

// Must include the height
  <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>This value will preserve the image size [Responsive Image] even when we resize the window. Check out this video below to see how it works: 👇</p>
<p><img src="https://media.giphy.com/media/VaqDcSh38DTz7YbV6p/giphy.gif" alt="Contain" width="600" height="400" loading="lazy"></p>
<h3 id="heading-image-width-and-height">Image width and height</h3>
<p>We can set the width and height of the image using the background-size property.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36p9azoztkvawbvy6244.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Here's the syntax in CSS: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// here, we see  width👇  &amp;  👇 height
  <span class="hljs-attribute">background-size </span>: <span class="hljs-number">200px</span>   <span class="hljs-number">200px</span>;
}
</code></pre>
<p>Also, don't forget to insert the image, set its height, and stop image repetition. The code snippet looks like this: </p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'cute-bear.png'</span>);
  <span class="hljs-attribute">background-repeat</span>: no-repeat;

// here, we see  width👇 &amp;  👇 height
  <span class="hljs-attribute">background-size </span>: <span class="hljs-number">200px</span>  <span class="hljs-number">200px</span>;

// Must include the height
  <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h3 id="heading-auto-sizing">Auto sizing</h3>
<p>When using this value, the image will stay at its original size. It won't change when we resize the window. It looks like this:</p>
<p><img src="https://media.giphy.com/media/hHc7ZhU7BB4NX8gLRR/giphy.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-the-css-background-repeat-property">The CSS background-repeat property</h1>
<p>This property allows us to repeat the same image multiple times.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/629rnxirqrdr8p5fapcd.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We write the syntax after writing the selector name, like this  👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// we'll change values 👇 here
  <span class="hljs-attribute">background-repeat </span>: repeat;
}
</code></pre>
<p>This property has six values:</p>
<ul>
<li>repeat</li>
<li>repeat-x</li>
<li>repeat-y</li>
<li>no-repeat</li>
<li>space</li>
<li>round</li>
</ul>
<p>Here are the results of each of these six values at a glance. Note that the kitty size in these examples is [200px X 200px].</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jj2jwfwh0pboqpylkeq0.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p><img src="https://media.giphy.com/media/3BUBxpCxmcDrBN4aZF/giphy.gif" alt="Round" width="600" height="400" loading="lazy"></p>
<p><img src="https://media.giphy.com/media/cO0jNSpVi0I3FWD62G/giphy.gif" alt="Space" width="600" height="400" loading="lazy"></p>
<p>Now, Let's investigate what's happening with each value. BUT, before that, note that we need to insert an image using the <code>background-image</code> property, like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'kitty.png'</span>);
   <span class="hljs-attribute">background-size </span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
   <span class="hljs-attribute">background-repeat </span>:  ; //we will play with values here 

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h3 id="heading-the-repeat-value">The repeat value</h3>
<p>By using this value, we can repeat the same image multiple times along <strong>both the X and Y axes</strong> as long as the screen space doesn't end. Here, the kitty size is 200px x 200px.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26zsa1dn161pawjqxuqp.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>To duplicate this result, we write -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'kitty.png'</span>);
   <span class="hljs-attribute">background-size </span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
   <span class="hljs-attribute">background-repeat </span>: repeat;

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h3 id="heading-the-repeat-x-value">The repeat-x value</h3>
<p>This value allows us to repeat the same image multiple times along the <strong>X-Axis</strong> as long as the screen space doesn't end. Kitty size: 200px X 200px.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pl4znzrwcevpr5w1a4i5.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>to make this happen, we write -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'kitty.png'</span>);
   <span class="hljs-attribute">background-size </span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
   <span class="hljs-attribute">background-repeat </span>: repeat-x;

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h3 id="heading-the-repeat-y-value">The repeat-y value</h3>
<p>This one works the same way as "repeat-x", but works along the <strong>Y-Axis</strong> as long as the screen space doesn't end. Kitty size: 200px X 200px.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7yo3i3bp8cw2r6zqhtvm.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>for this outcome, we write -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'kitty.png'</span>);
   <span class="hljs-attribute">background-size </span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
   <span class="hljs-attribute">background-repeat </span>: repeat-y ;

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h3 id="heading-the-no-repeat-value">The no-repeat value</h3>
<p>We can have our original image without repetition using this value. Kitty size: 200px X 200px.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2okgurnuakrnqbyv6kr.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>For this result, we write -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'kitty.png'</span>);
   <span class="hljs-attribute">background-size </span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
   <span class="hljs-attribute">background-repeat </span>: no-repeat ; 

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h3 id="heading-the-space-value">The space value</h3>
<p>This works both along the X and Y axes. We can see the main difference between the values <strong>space and round</strong> when we resize the window. Notice that we have <strong>empty spaces</strong> when we resize the window:</p>
<p><img src="https://media.giphy.com/media/cO0jNSpVi0I3FWD62G/giphy.gif" alt="Space" width="600" height="400" loading="lazy"></p>
<p>to experiment with this value, write -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'kitty.png'</span>);
   <span class="hljs-attribute">background-size </span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
   <span class="hljs-attribute">background-repeat </span>: space ;

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h3 id="heading-the-round-value">The round value</h3>
<p>This works both along the X and Y axes. Notice that the image is <strong>stretching</strong> when we resize the window.</p>
<p><img src="https://media.giphy.com/media/3BUBxpCxmcDrBN4aZF/giphy.gif" alt="Round" width="600" height="400" loading="lazy"></p>
<p>Follow along &amp; write -&gt;</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'kitty.png'</span>);
   <span class="hljs-attribute">background-size </span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
   <span class="hljs-attribute">background-repeat </span>: round ; 

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h1 id="heading-the-css-background-position-property">The CSS background-position property</h1>
<p>This property is used to change the position of an image on the screen.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4ndvr71f0yl9c44kbc7.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Here's the syntax: 👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// This is       X-Axis👇  &amp;  👇 Y-Axis
<span class="hljs-attribute">background-position </span>: <span class="hljs-number">300px</span>  <span class="hljs-number">200px</span>;
}
</code></pre>
<p>Also, don't forget to insert the image, set its height, and stop image repetition. We've set the kitty size to 200px X 200px using the <code>background-size</code> property:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">"kitty-idea.png"</span>);
  <span class="hljs-attribute">background-size</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">background-repeat</span>: no-repeat;

// This is         X-Axis👇  &amp; 👇 Y-Axis
  <span class="hljs-attribute">background-position </span>: <span class="hljs-number">300px</span> <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65p2htkztmijbm1hxlug.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>You can also use a combination of these values:</p>
<ul>
<li>top</li>
<li>left</li>
<li>right</li>
<li>bottom</li>
<li>percentage values</li>
</ul>
<p>For an example, let's set our kitty at the very bottom right. Here's the code snippet for this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">"kitty-idea.png"</span>);
  <span class="hljs-attribute">background-size</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">background-repeat</span>: no-repeat;

// This is         X-Axis👇  &amp; 👇 Y-Axis
  <span class="hljs-attribute">background-position </span>: bottom right;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ga6veuh8ea3yzq7nend2.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Calculating the available space of the screen, the % values determine the position of the image. Here's what it looks like in code:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">"kitty-idea.png"</span>);
  <span class="hljs-attribute">background-size</span>: <span class="hljs-number">200px</span> <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">background-repeat</span>: no-repeat;

// This is         X-Axis👇  &amp; 👇 Y-Axis
  <span class="hljs-attribute">background-position </span>: <span class="hljs-number">25%</span> <span class="hljs-number">15%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>And here's the result:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fazbxgdpkqeomum02qv7.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h1 id="heading-the-css-background-origin-property">The CSS background-origin property</h1>
<p>This property allows us to set the origin of our image across the CSS box model.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wc2b6ypgcfdtol6am14g.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We write the syntax after writing the selector name, like this  👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// We'll write values   👇 here
  <span class="hljs-attribute">background-origin</span>: border-box;
}
</code></pre>
<p>Its four values are:</p>
<ul>
<li>border-box</li>
<li>padding-box</li>
<li>content-box</li>
<li>inherit</li>
</ul>
<p>In the standard CSS box model, the outermost part is the border. Then comes the padding and finally we have the content itself at the center.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3mdn6hpd1u892akrkj5.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>Here's the result of every property at a glance:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27ptyulhvxqi4idrw68n.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>To recreate these results:</p>
<ul>
<li>First we need an image, we need to stop image repetition, and set the height and width of <strong>both the container and the image.</strong></li>
<li>Once done, we will insert 40px of padding, otherwise we can't see the difference between the padding box and the content box.</li>
<li>Then, insert a 25px red border. Set the border-style to dashed to get a <strong>dashed border</strong> on the screen.</li>
<li>set the background-size to 400px &amp; 400px</li>
</ul>
<p>Here's what that looks like in code:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'cute-girl.png'</span>);
  <span class="hljs-attribute">background-repeat</span>: no-repeat;
  <span class="hljs-attribute">background-size</span>: <span class="hljs-number">400px</span> <span class="hljs-number">400px</span>;

// Change  values here  👇  to see difference 
  <span class="hljs-attribute">background-origin</span>: border-box;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">40px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">25px</span> solid red;
  <span class="hljs-attribute">border-style</span>: dashed;

// Width &amp; height for container 👇
  <span class="hljs-attribute">width </span>: <span class="hljs-number">400px</span>;
  <span class="hljs-attribute">height </span>: <span class="hljs-number">400px</span>;
}
</code></pre>
<h3 id="heading-take-a-break-1">Take A Break</h3>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yahewko7hckdgp7p4xux.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h1 id="heading-the-css-background-clip-property">The CSS background-clip property</h1>
<p>This is the same as the <code>background-origin</code> property. The main difference is that <code>background-clip</code> <strong>CUTS</strong> the image to fit inside the box, while <code>background-origin</code> <strong>PUSHES</strong> the content inside the box to fit.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4ga97rke3kgppd7qlxn.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We write the syntax after writing the selector name, like this  👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// We'll write values   👇 here
  <span class="hljs-attribute">background-clip  </span>: border-box;
}
</code></pre>
<p>Its four values are:</p>
<ul>
<li>border-box</li>
<li>padding-box</li>
<li>content-box</li>
<li>inherit</li>
</ul>
<p>Here's the result of every property at a glance:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xgd6sh8237bmvpnujl8r.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>To recreate these results:</p>
<ul>
<li>First we need an image, we need to stop image repetition, and we need to set the height and width of <strong>both the container and the image.</strong></li>
<li>Once done, we will insert 40px padding, otherwise we won't be able to see the <strong>difference</strong> between the padding box and content box.</li>
<li>Then, insert a 25px red border. Set the border-style to dashed to see the <strong>dashed border</strong> on screen.</li>
<li>Set the background-size to 400px &amp; 400px</li>
</ul>
<p>The code looks like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'cute-girl.png'</span>);
  <span class="hljs-attribute">background-repeat</span>: no-repeat;
  <span class="hljs-attribute">background-size</span>: <span class="hljs-number">400px</span> <span class="hljs-number">400px</span>;

// Change  values here  👇  to see difference 
  <span class="hljs-attribute">background-clip</span>: border-box;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">40px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">25px</span> solid red;
  <span class="hljs-attribute">border-style</span>: dashed;

// Width &amp; height for container 👇
  <span class="hljs-attribute">width </span>: <span class="hljs-number">400px</span>;
  <span class="hljs-attribute">height </span>: <span class="hljs-number">400px</span>;
}
</code></pre>
<h1 id="heading-the-css-background-attachment-property">The CSS background-attachment property</h1>
<p>This property allows us to control the behavior of our content and image <strong>when we scroll.</strong></p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1xx67vtt5w3c861sskx.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We write the syntax after writing the selector name, like this  👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// We'll  change  values 👇  here
<span class="hljs-attribute">background-attachment</span>: scroll;
}
</code></pre>
<p>Its three values are:</p>
<ul>
<li>scroll</li>
<li>fixed</li>
<li>local</li>
</ul>
<p>When we use <strong>scroll</strong>, the image is fixed and we can freely scroll our content. The <strong>fixed</strong> value gives us a parallax effect on mouse scroll and <strong>local</strong> produces multiple images as long as our content doesn't end.</p>
<p>You can see the results live here 👇</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/ematte/embed/GRjJjro" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p><a target="_blank" href="https://dev.to/hadrysmateusz/learn-all-8-background-css-properties-in-5-minutes-2lk4">Here's where I got this pen</a>.</p>
<h1 id="heading-the-css-background-color-property">The CSS background-color property</h1>
<p>You can use this property to fill your background with color.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mh7pe7phpj2vrvz304ma.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>We write the syntax after writing the selector name, like this  👇</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// we'll change values 👇  here
   <span class="hljs-attribute">background-color </span>:  red;
}
</code></pre>
<p>Out of the many options, the most popular ones are:</p>
<ul>
<li>Solid color by name or hex value</li>
<li>Using the <code>RGB()</code> color function</li>
<li>Using the <code>linear-gradient()</code> function</li>
</ul>
<h3 id="heading-how-to-get-a-solid-background-color-by-name-or-hex-value">How to get a solid background color by name or hex value</h3>
<p>You can use color names to set the background color, like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-color </span>: red;

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>Or, you can use a hex color code like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
   <span class="hljs-attribute">background-color </span>: <span class="hljs-number">#ff0000</span>; //red color

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>You can check out these resources for more colors:</p>
<ul>
<li><a target="_blank" href="https://coolors.co/">coolors.co</a></li>
<li><a target="_blank" href="https://flatuicolors.com/">flatuicolors.com</a></li>
</ul>
<h3 id="heading-how-to-use-the-rbg-color-function-to-set-background-color">How to use the RBG() color function to set background color</h3>
<p>You can use <code>RGB()</code> color function to set the background color like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// color name is "American River"
   <span class="hljs-attribute">background-color </span>: <span class="hljs-built_in">rgb</span>(<span class="hljs-number">99</span>, <span class="hljs-number">110</span>, <span class="hljs-number">114</span>);

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>Or, you can use <code>RGBA()</code> to set both color and opacity like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{
// The 0.5 at last represents        50% 👇 opacity 
   <span class="hljs-attribute">background-color </span>:  <span class="hljs-built_in">rgba</span>(<span class="hljs-number">99</span>, <span class="hljs-number">110</span>, <span class="hljs-number">114</span>, <span class="hljs-number">0.5</span>);

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>This is an experiment with the color named 'Eton blue' with various opacity levels: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yzc82sudq8es7ocok12g.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-set-the-background-color-with-the-linear-gradient-function">How to set the background color with the linear-gradient() function</h3>
<p>You can use this function to create a gradient color of more than 1 color. Here are some examples of gradient colors:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0j3e3r6kobycowckdxg.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>You can visit <a target="_blank" href="https://uigradients.com/#Summer">this website</a> for more color resources with CSS code snippets.</p>
<p>Let's recreate this background color:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkf21o97m7gqzpme810k.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>'#22c1c3' represents the color on the left, and '#fdbb2d' represents color on right. '90deg' tells us how the two colors will be angled to create a gradient.</p>
<p>The code snippet looks like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{

   <span class="hljs-attribute">background</span>: <span class="hljs-built_in">linear-gradient</span>(<span class="hljs-number">90deg</span>, #<span class="hljs-number">22</span>c1c3, #fdbb2d);  

   <span class="hljs-attribute">height </span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<h1 id="heading-the-short-hand-for-these-css-properties">The short-hand for these CSS properties</h1>
<p>This is the order of the shorthand for the background properties:</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/newvcc4rvegnbkblwzyb.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<p>For this experiment, let's put <code>kitty.png</code> in our browser with a blue background at 200px on the X-Axis and 200px on the Y-axis. The code snippet looks like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{

   <span class="hljs-attribute">background-color </span>: skyblue;
   <span class="hljs-attribute">background-image </span>: <span class="hljs-built_in">url</span>(<span class="hljs-string">'kitty.png);
   background-repeat: no-repeat;
   background-attachment: fixed;
   background-position: 200px 200px;

   height : 100vh;
}</span>
</code></pre>
<p>And here's the code snippet using the shorthand:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span>{

   <span class="hljs-attribute">background</span>: skyblue <span class="hljs-built_in">url</span>(<span class="hljs-string">'kitty.png) no-repeat fixed 200px 200px;

   height : 100vh;
}</span>
</code></pre>
<p>This shorthand really saves us time. If you want to skip one value, you can do it as long as you maintain the order of these properties.</p>
<h1 id="heading-conclusion">Conclusion</h1>
<p>Here's your medal for reading till the the end ❤️</p>
<p>Suggestions and criticisms are highly appreciated ❤️</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h3 id="heading-credits">Credits</h3>
<ul>
<li><a target="_blank" href="https://www.pexels.com/photo/woman-lying-on-plants-2125610/">Cute Girl I have a crush on 🥰</a></li>
<li><a target="_blank" href="https://www.flaticon.com/packs/kitty-avatars-3">kitty Avatar</a></li>
<li><a target="_blank" href="https://www.freepik.com/free-vector/cute-bear-is-happy-cartoon-illustration_12341167.htm#position=4">Cute panda</a></li>
<li><a target="_blank" href="https://www.freepik.com/free-vector/set-happy-cute-cats-cartoon-illustration_12566295.htm#position=11">cute cat with duck</a></li>
<li><a target="_blank" href="https://www.freepik.com/free-vector/young-girl-different-gestures-cartoon-illustration_12566309.htm#page=1&amp;position=22">cute girl illustration</a></li>
<li><a target="_blank" href="https://www.freepik.com/free-vector/set-cute-rabbit-with-duck-feel-happy-sad-cartoon-illustration_12573654.htm#position=7">Rabbit with duck</a></li>
<li><a target="_blank" href="https://css-tricks.com/almanac/properties/b/background/">CSS-Tricks</a></li>
</ul>
<p><strong>YouTube <a target="_blank" href="https://youtube.com/c/joyshaheb">/ Joy Shaheb</a></strong></p>
<p><strong>Twitter <a target="_blank" href="https://twitter.com/JoyShaheb">/ JoyShaheb</a></strong></p>
<p><strong>Instagram <a target="_blank" href="https://www.instagram.com/joyshaheb/">/ JoyShaheb</a></strong></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn CSS Flexbox by Building 5 Responsive Layouts ]]>
                </title>
                <description>
                    <![CDATA[ Here's a practical guide to help you learn CSS Flexbox in 2021 by building 5 responsive layouts. Let's dive right in.🥇 Table of Contents Flexbox Architecture Setup Level-1 Level-2 Level-3 Level-4 Level-5 Conclusion You can check out the Figma desi... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-flexbox-build-5-layouts/</link>
                <guid isPermaLink="false">66b2096782069b4c678c9925</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ css flex ]]>
                    </category>
                
                    <category>
                        <![CDATA[ flexbox ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ responsive design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joy Shaheb ]]>
                </dc:creator>
                <pubDate>Mon, 29 Mar 2021 23:11:34 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/03/FCC--4-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Here's a practical guide to help you learn CSS Flexbox in 2021 by building 5 responsive layouts. Let's dive right in.🥇</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-flexbox-architecture">Flexbox Architecture</a></li>
<li><a class="post-section-overview" href="#heading-project-setup">Setup</a></li>
<li><a class="post-section-overview" href="#heading-layout-difficulty-level-1-how-to-build-a-card-layout">Level-1</a></li>
<li><a class="post-section-overview" href="#heading-layout-difficulty-level-2-how-to-build-a-nav-bar-layout">Level-2</a></li>
<li><a class="post-section-overview" href="#heading-layout-difficulty-level-3-how-to-build-a-sidebar-layout">Level-3</a></li>
<li><a class="post-section-overview" href="#heading-layout-difficulty-level-4-how-to-build-a-card-layout-2">Level-4</a></li>
<li><a class="post-section-overview" href="#heading-layout-difficulty-level-5-the-holy-grail-of-layouts">Level-5</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ul>
<h3 id="heading-you-can-check-out-the-figma-design-herehttpswwwfigmacomfiled1bg4msazxixv9jwp8e4namaster-css-flexbox-layoutnode-id03a1">You can <a target="_blank" href="https://www.figma.com/file/d1bG4msAzxixv9jWP8e4nA/Master-CSS-Flexbox-Layout?node-id=0%3A1"><strong>check out the Figma design here</strong></a></h3>
<h3 id="heading-you-can-also-watch-this-tutorial-on-my-youtube-channel-here">You can also watch this tutorial on my YouTube channel here:</h3>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/m8BSEUUB5so" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<h2 id="heading-what-is-flexbox">What is Flexbox?</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/03/Desktop---1--6--1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We need a blueprint when constructing a house. In the same way, we need a blueprint to <strong>layout</strong> our <strong>content across our browser.</strong> At the same time, we need to make <strong>responsive layouts</strong> for <strong>various screen sizes.</strong> </p>
<p>This is what <strong>Flexbox</strong> helps us do – create responsive layouts.</p>
<h1 id="heading-flexbox-architecture">Flexbox Architecture</h1>
<p>So how does Flexbox architecture work? The <strong>flex-items</strong> [Contents] are distributed along the <strong>Main Axis</strong> and <strong>Cross Axis.</strong> And, depending on the <strong>flex-direction</strong> property, the layout position changes between rows and columns.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/hy2oqjvsbk60ef92nktg.png" alt="Flex Box Architecture" width="600" height="400" loading="lazy"></p>
<h1 id="heading-flexbox-chart">Flexbox Chart</h1>
<p>This chart contains <strong>every possible property and value</strong> you can use when using flexbox. You can reference it while doing the project and experimenting with different values. </p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/gv3jyh4xt4fbwtq1qejn.png" alt="Flex Box Chart" width="600" height="400" loading="lazy"></p>
<h2 id="heading-before-we-get-started">Before We Get Started...</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/03/Desktop---2--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>You need to know <strong>a little bit of HTML</strong> and <strong>CSS</strong>. Throughout this tutorial, you'll learn how Flexbox works, how its various properties and values work, and how media queries work (which help us make responsive websites).</p>
<h1 id="heading-project-setup">Project Setup</h1>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/03/Frame-2--2-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Before coding, we need to save some values in variables and clear the default browser styles. Along with that, we will define some media query mixins to save time and avoid code repetition.</p>
<h2 id="heading-scss">SCSS</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/03/Frame-3--3-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><strong>SCSS</strong> is a preprocessor of CSS which gives us much more functionality than regular CSS. </p>
<p>For example, we can nest child classes inside their parent class, store values in variables, and much more. It really helps us save time.</p>
<p>Let's start playing around with SCSS. Open <a target="_blank" href="https://codepen.io/">CodePen</a> or any code editor and go through this with me, these step by step.👇</p>
<h3 id="heading-how-to-enable-scss-in-codepen">How to Enable SCSS in Codepen 👇</h3>
<p><img src="https://media.giphy.com/media/AkEzX8BD8Ef23fyGXP/giphy.gif" alt="SCSS" width="600" height="400" loading="lazy"></p>
<p>First, define the border color, gap, and padding values in your variables.</p>
<pre><code class="lang-scss"><span class="hljs-variable">$gap</span> : <span class="hljs-number">4vh</span>;
<span class="hljs-variable">$padding</span> : <span class="hljs-number">4vh</span>;
<span class="hljs-variable">$color</span> : <span class="hljs-number">#48CAE4</span>;
</code></pre>
<p>Next, define various screen breakpoints in the SCSS map. <strong>Remember,</strong> on our SCSS map, [mobile, tablet, and desktop] are <strong>keys</strong> and the pixels are <strong>values</strong>.</p>
<pre><code class="lang-scss"><span class="hljs-variable">$bp</span> : (
  mobile : <span class="hljs-number">480px</span>,
  tablet : <span class="hljs-number">768px</span>,
  desktop : <span class="hljs-number">1440px</span>,
);
</code></pre>
<p>To save time and code, when defining media queries we'll use <strong>mixins and loop</strong> the breakpoints we defined above. 👆</p>
<pre><code class="lang-scss"><span class="hljs-keyword">@mixin</span> query(<span class="hljs-variable">$display</span>){
  <span class="hljs-keyword">@each</span> <span class="hljs-variable">$key</span>, <span class="hljs-variable">$value</span> in <span class="hljs-variable">$bp</span>{

    <span class="hljs-comment">//  defining max-width</span>
    <span class="hljs-keyword">@if</span> (<span class="hljs-variable">$display</span> == <span class="hljs-variable">$key</span>){
      <span class="hljs-keyword">@media</span> (max-width: <span class="hljs-variable">$value</span>){<span class="hljs-keyword">@content</span>;}
    }
  }
}
</code></pre>
<p>Now we have to <strong>change the default styles of our browser</strong>. We remove the margin and padding and set the box-sizing to border-box.</p>
<pre><code class="lang-scss"><span class="hljs-comment">//Changing The Default Settings..</span>

*{
  <span class="hljs-attribute">margin</span>:<span class="hljs-number">0px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">box-sizing</span>: border-box;

<span class="hljs-comment">// Changing the body here</span>

  <span class="hljs-selector-tag">body</span>{
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">min-height</span>: <span class="hljs-number">100vh</span>;
    <span class="hljs-attribute">font-family</span>: sans-serif;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">45px</span>;
  }
}
</code></pre>
<p>Let's talk about the relationship between the HTML classes we're going to use. Always remember that Flexbox works on child classes. </p>
<p>For example, Flexbox properties defined on the <strong>Container class</strong> work on <strong>Block</strong>, and properties defined on the <strong>Block class</strong> work on the <strong>Box class</strong>. Here's an illustrated explanation of what I mean: 👇</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/ivmqdkg948wclyfs1n0t.png" alt="FLOW" width="600" height="400" loading="lazy"></p>
<p>Now let's create these layouts. We'll start with an easier difficulty level and move to more advanced layouts.</p>
<h1 id="heading-layout-difficulty-level-1-how-to-build-a-card-layout">Layout Difficulty Level-1 – How to Build a Card Layout</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/yjx1adsec062quujq7ke.png" alt="Level-1" width="600" height="400" loading="lazy"></p>
<h3 id="heading-html">HTML</h3>
<p>The <strong>container class</strong> will hold 3 children -&gt; block-1, block-2, and block-3.</p>
<p>The <strong>block-1</strong> class will carry 3 boxes -&gt; box-1, box-2, and box-3.</p>
<p>The same rules apply for the <strong>block-2 and block-3</strong> classes, but the values will be changed.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>

<span class="hljs-comment">&lt;!--block-1 has 3 children, box-1,box-2,box-3 --&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-1"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> A <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-2"</span>&gt;</span> B <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-3"</span>&gt;</span> C <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-comment">&lt;!--similar to block-1, values are changed --&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-2"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-4"</span>&gt;</span> D <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-5"</span>&gt;</span> E <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-6"</span>&gt;</span> F <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-comment">&lt;!--similar to block-1, values are changed --&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-3"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-7"</span>&gt;</span> G <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-8"</span>&gt;</span> H <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-9"</span>&gt;</span> I <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-scss-1">SCSS</h3>
<p>Now, we're going to style our container class. Remember, to trigger Flexbox and access all its powers, you need to write <strong><code>display: flex;</code></strong> like this: </p>
<pre><code class="lang-scss"><span class="hljs-comment">// Style rules for container class</span>

<span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">display</span>: flex;

<span class="hljs-comment">//to lay .block-* classes in a column</span>
  <span class="hljs-attribute">flex-direction</span>: column;

<span class="hljs-comment">//Setting gap between the .block-* classes</span>
  gap: <span class="hljs-variable">$gap</span>;

<span class="hljs-comment">// to set some padding &amp; border inside</span>
  <span class="hljs-attribute">padding</span>: <span class="hljs-variable">$padding</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1vh</span> solid <span class="hljs-variable">$color</span>;
}
</code></pre>
<p>Select all the <strong><code>.block-*</code></strong> classes and style them together. At the same time, at the bottom, we will define our media query using the <strong>mixin</strong> we created during the setup phase.</p>
<pre><code class="lang-scss"><span class="hljs-selector-attr">[class ^="block-"]</span>{

<span class="hljs-comment">//To lay the boxes in a row.</span>
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;

<span class="hljs-comment">//Removing border(1vh+1vh), gap, &amp; padding from the height</span>
<span class="hljs-comment">// And then equally distributing spaces between the </span>
<span class="hljs-comment">// .block-* classes by dividing it by 3</span>

  <span class="hljs-attribute">height</span>: (<span class="hljs-number">100vh</span>-<span class="hljs-number">2vh</span> -<span class="hljs-variable">$gap</span>*<span class="hljs-number">2</span>-<span class="hljs-variable">$padding</span>*<span class="hljs-number">2</span>) / <span class="hljs-number">3</span>;

<span class="hljs-comment">// putting gap between .box-* classes</span>
  gap: <span class="hljs-variable">$gap</span>;

<span class="hljs-comment">// Style rules for mobile display</span>

  <span class="hljs-keyword">@include</span> query (mobile){
    <span class="hljs-attribute">flex-direction</span>: column;

<span class="hljs-comment">// you can pick any value you wish.</span>
    <span class="hljs-attribute">height</span>: <span class="hljs-number">500px</span>;
  }

}
</code></pre>
<p>Alright then, next select all the <strong><code>.box-*</code> classes</strong> and style them together like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-attr">[class ^="box-"]</span>{

<span class="hljs-comment">// To set the text at center of every box</span>
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;

<span class="hljs-comment">// To divide spaces among the boxes</span>
<span class="hljs-comment">// try flex-gap:1; you can see the difference.</span>
<span class="hljs-comment">// flex-grow: 1; // 1+1+1 =3 =&gt; 1/3 X 100% =&gt; 33.33% each</span>

  <span class="hljs-attribute">flex-basis</span>: (<span class="hljs-number">100%</span>)/<span class="hljs-number">3</span>; <span class="hljs-comment">// 33.33% each</span>
  <span class="hljs-attribute">border</span> : <span class="hljs-number">2px</span> solid black;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#c1c1c1</span>;
}
</code></pre>
<h1 id="heading-layout-difficulty-level-2-how-to-build-a-nav-bar-layout">Layout Difficulty Level-2 – How to Build a Nav Bar Layout</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/dhkcw4npzbmp34lcm4yf.png" alt="Level-2" width="600" height="400" loading="lazy"></p>
<h3 id="heading-html-1">HTML</h3>
<p>Remove the HTML code of level-1 and write this code instead. Basically it's <strong>1 parent class with 4 child classes.</strong></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item-1"</span>&gt;</span> Home <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item-2"</span>&gt;</span> About <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item-3"</span>&gt;</span> Services <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item-4"</span>&gt;</span> Contact <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-scss-2">SCSS</h3>
<p>Here are the style rules for the <strong>container class</strong> for level-2. At the bottom, we will set up a media query using the mixin.  </p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">35px</span>;
  <span class="hljs-attribute">display</span>: flex;

<span class="hljs-comment">//To set orientation of the items</span>
  <span class="hljs-attribute">flex-direction</span>: row;

<span class="hljs-comment">// To distribute available space</span>
  <span class="hljs-attribute">justify-content</span>: space-evenly;
  <span class="hljs-attribute">padding</span>: <span class="hljs-variable">$padding</span>;
  <span class="hljs-attribute">border</span> : <span class="hljs-number">1vh</span> solid <span class="hljs-variable">$color</span>;

<span class="hljs-comment">// style rules starts from Tablet Screens</span>
  <span class="hljs-keyword">@include</span> query(tablet){
    <span class="hljs-attribute">height</span> : <span class="hljs-number">100vh</span>;
<span class="hljs-comment">//Changing orientation of the items</span>
    <span class="hljs-attribute">flex-direction</span>: column;
    <span class="hljs-attribute">align-items</span>: center;

<span class="hljs-comment">//Setting gap for items Vertically</span>
    gap: <span class="hljs-variable">$gap</span> 
  }
}
</code></pre>
<h1 id="heading-layout-difficulty-level-3-how-to-build-a-sidebar-layout">Layout Difficulty Level-3 – How to Build a Sidebar Layout</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/evaqt7tcdldtu7g3f9io.png" alt="Level-3" width="600" height="400" loading="lazy"></p>
<h3 id="heading-html-2">HTML</h3>
<p>Now, we'll work with this code block:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-1"</span>&gt;</span> Left <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-2"</span>&gt;</span> Right <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-scss-3">SCSS</h3>
<p>The Style rules of the <strong>container class</strong> with the media query mixin are included at the bottom for level-3:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;
  gap: <span class="hljs-variable">$gap</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-variable">$padding</span>;

<span class="hljs-comment">// Style Rules for Mobile Display</span>

  <span class="hljs-keyword">@include</span> query(mobile){
    <span class="hljs-attribute">flex-direction</span>: column;
  }
}
</code></pre>
<p>Here, we select and style all <strong><code>.block-*</code> classes</strong> along with the media query for mobile display:</p>
<pre><code class="lang-scss"><span class="hljs-selector-attr">[class ^="block-"]</span>{

<span class="hljs-comment">//To put the left, right text at center</span>
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;

  <span class="hljs-attribute">border</span> : <span class="hljs-number">4px</span> solid <span class="hljs-variable">$color</span>;
<span class="hljs-comment">//Setting height of each block</span>
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span> -<span class="hljs-variable">$padding</span>*<span class="hljs-number">2</span>;

<span class="hljs-comment">// Style Rules for Mobile Display</span>
  <span class="hljs-keyword">@include</span> query(mobile){
    <span class="hljs-attribute">height</span>: <span class="hljs-number">50vh</span> -<span class="hljs-variable">$padding</span>*<span class="hljs-number">2</span>;
  }
}
</code></pre>
<p>Now we individually target the block-1 and block-2 classes. We also change the flex-grow value in order to distribute screen space.</p>
<pre><code class="lang-scss"><span class="hljs-comment">// Style Rules Left Block</span>

<span class="hljs-selector-class">.block-1</span>{
<span class="hljs-comment">//will occupy 20% of the Available width</span>
  <span class="hljs-attribute">flex-grow</span>: <span class="hljs-number">2</span>;
}

<span class="hljs-comment">// Style Rules Right Block</span>

<span class="hljs-selector-class">.block-2</span>{
<span class="hljs-comment">//will occupy 80% of the Available width</span>
  <span class="hljs-attribute">flex-grow</span>: <span class="hljs-number">8</span>;
}
</code></pre>
<h3 id="heading-take-a-break-d">Take A Break :D</h3>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/03/Break--1-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h1 id="heading-layout-difficulty-level-4-how-to-build-a-card-layout-2">Layout Difficulty Level-4 – How to Build a Card Layout #2</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/6bwmcadjacdyh5fobk1d.png" alt="Level-4" width="600" height="400" loading="lazy"></p>
<h3 id="heading-html-3">HTML</h3>
<p>The HTML rules here are similar to level-1 with a few changes:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-1"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> A <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>


  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-2"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-2"</span>&gt;</span> B <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-3"</span>&gt;</span> E <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>


  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-3"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-4"</span>&gt;</span> C <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-5"</span>&gt;</span> D <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-scss-4">SCSS</h3>
<p>Style the container class like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: column;
  <span class="hljs-attribute">padding</span>: <span class="hljs-variable">$padding</span>;
  gap: <span class="hljs-variable">$gap</span>;
}
</code></pre>
<p>Next, select and style all the <strong><code>block-*</code> classes</strong> together along with the media query mixin at the bottom for mobile devices:</p>
<pre><code class="lang-scss"><span class="hljs-selector-attr">[class ^="block-"]</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;
  gap: <span class="hljs-variable">$gap</span>;

<span class="hljs-comment">// Removing Padding, Gap &amp; divide by 3</span>
  <span class="hljs-attribute">height</span>: (<span class="hljs-number">100vh</span> -<span class="hljs-variable">$gap</span>*<span class="hljs-number">2</span> -<span class="hljs-variable">$padding</span>*<span class="hljs-number">2</span>)/<span class="hljs-number">3</span>;

<span class="hljs-comment">// Style Rules for Mobile Version  </span>
  <span class="hljs-keyword">@include</span> query(mobile){
    <span class="hljs-attribute">flex-direction</span>: column;
  }
}
</code></pre>
<p>Now select and style all the <strong><code>box-*</code> classes</strong> together:</p>
<pre><code class="lang-scss"><span class="hljs-selector-attr">[class ^="box-"]</span>{

<span class="hljs-comment">// To place the letter at center</span>
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;

<span class="hljs-comment">// Border, Border-radius &amp; background-color</span>
  <span class="hljs-attribute">border</span> : <span class="hljs-number">1vh</span> solid <span class="hljs-variable">$color</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#c1c1c1</span>;
}
</code></pre>
<p>Now, we'll individually target the boxes and <strong>use flex-basis to distribute screen space.</strong></p>
<pre><code class="lang-scss"><span class="hljs-comment">//A</span>
<span class="hljs-selector-class">.box-1</span>{
  <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">100%</span>;
}

<span class="hljs-comment">//B &amp; D</span>
<span class="hljs-selector-class">.box-2</span>, <span class="hljs-selector-class">.box-5</span>{
  <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">70%</span>;
}
<span class="hljs-comment">//E  &amp; C</span>
<span class="hljs-selector-class">.box-3</span>,<span class="hljs-selector-class">.box-4</span>{
  <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">30%</span>
}
</code></pre>
<p>Finally, we will include the media query mixin for the mobile version. </p>
<pre><code class="lang-scss"><span class="hljs-comment">// Style Rules for Mobile Version </span>

<span class="hljs-keyword">@include</span> query(mobile){
  <span class="hljs-selector-class">.block-2</span>{
    <span class="hljs-attribute">height</span>: (<span class="hljs-number">100vh</span>*<span class="hljs-number">2</span>)/<span class="hljs-number">3</span>; <span class="hljs-comment">// 66.66vh</span>
  }
  <span class="hljs-selector-class">.block-3</span>{
    <span class="hljs-attribute">flex-direction</span>: row;
  }

<span class="hljs-comment">//   Selecting B, E, C, D</span>
  <span class="hljs-selector-class">.box-2</span>,<span class="hljs-selector-class">.box-3</span>,<span class="hljs-selector-class">.box-4</span>,<span class="hljs-selector-class">.box-5</span>{
    <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">50%</span>;
  }
}
</code></pre>
<h3 id="heading-are-you-winning-yet-lets-turn-up-the-heat">Are you winning yet? Let's turn up the heat. 🥵</h3>
<h1 id="heading-layout-difficulty-level-5-the-holy-grail-of-layouts">Layout Difficulty Level-5 – The Holy Grail of Layouts</h1>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/8lb4gh8povgvcb40iz0h.png" alt="Level-5" width="600" height="400" loading="lazy"></p>
<h3 id="heading-html-4">HTML</h3>
<p>The HTML rules here are similar to <strong>level-1</strong> and <strong>level-4</strong> with a few changes: </p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-1"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-1"</span>&gt;</span> A <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-2"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-2"</span>&gt;</span> B <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-3"</span>&gt;</span> C <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-4"</span>&gt;</span> D <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"block-3"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"box-5"</span>&gt;</span> E <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<h3 id="heading-scss-5">SCSS</h3>
<p>First, change the <strong>container class styles</strong> like this:</p>
<pre><code class="lang-scss"><span class="hljs-selector-class">.container</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: column;
  gap: <span class="hljs-variable">$gap</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-variable">$padding</span>;
}
</code></pre>
<p>Then, target and style all <strong><code>block-*</code> classes</strong> together.</p>
<pre><code class="lang-scss"><span class="hljs-comment">// Style rules of all .block-*</span>

<span class="hljs-selector-attr">[class ^="block-"]</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: row;
  gap: <span class="hljs-variable">$gap</span>;
}
</code></pre>
<p>Next, target and style all <strong><code>box-*</code> classes</strong> together.</p>
<pre><code class="lang-scss"><span class="hljs-comment">// Style rules of all .box-*</span>

<span class="hljs-selector-attr">[class ^="box-"]</span>{
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">border</span> : <span class="hljs-number">1vh</span> solid <span class="hljs-variable">$color</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;
}
</code></pre>
<p>Then individually target the boxes and <strong>use flex-basis to distribute screen space.</strong></p>
<pre><code class="lang-scss"><span class="hljs-comment">// Defining A &amp; E Together</span>
<span class="hljs-selector-class">.box-1</span>,<span class="hljs-selector-class">.box-5</span>{
  <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">20vh</span>;
}

<span class="hljs-comment">// Defining C here</span>
<span class="hljs-selector-class">.box-3</span>{
  <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">60%</span>;
<span class="hljs-comment">//   Removing Gap &amp; padding</span>
  <span class="hljs-attribute">height</span>: <span class="hljs-number">60vh</span> -<span class="hljs-variable">$gap</span>*<span class="hljs-number">2</span>-<span class="hljs-variable">$padding</span>*<span class="hljs-number">2</span>;
}

<span class="hljs-comment">// Defining B &amp; D Together</span>
<span class="hljs-selector-class">.box-2</span>,<span class="hljs-selector-class">.box-4</span>{
  <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">20%</span>;
}
</code></pre>
<p>Lastly, include the media query mixin for the mobile version. Notice that we are <strong>hiding box-2</strong> for the mobile version.</p>
<pre><code class="lang-scss"><span class="hljs-comment">// Media query for mobile Screen</span>

<span class="hljs-keyword">@include</span> query(mobile){
  <span class="hljs-selector-class">.block-2</span>{
    <span class="hljs-attribute">flex-direction</span>: column;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">60vh</span>;
  }
<span class="hljs-comment">// Hiding our B block</span>
  <span class="hljs-selector-class">.box-2</span>{
    <span class="hljs-attribute">display</span>: none;
  }
<span class="hljs-comment">// Increasing Height of C  </span>
  <span class="hljs-selector-class">.box-3</span>{
    <span class="hljs-attribute">flex-basis</span>: <span class="hljs-number">80%</span>;
  }
}
</code></pre>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Thanks for following along! Hopefully now you understand the basics of Flexbox. Here's your medal for reading till the end. ❤️</p>
<p>Suggestions and criticisms are highly appreciated ❤️ Do get in touch with me via the social media links below if you have any questions.</p>
<p><img src="https://dev-to-uploads.s3.amazonaws.com/i/usxsz1lstuwry3jlly4d.png" alt="Alt Text" width="600" height="400" loading="lazy"></p>
<h2 id="heading-image-credits">Image Credits</h2>
<p><a target="_blank" href="https://www.flaticon.com/packs/kitty-avatars-3">Kitty Avatar</a>
<a target="_blank" href="https://www.freepik.com/free-vector/cute-cats-set-funny-character-cartoon-illustration_12566246.htm#page=3&amp;position=2">Kitty with duck</a>
<a target="_blank" href="https://www.freepik.com/free-vector/set-cute-rabbit-with-duck-cartoon-illustration_12573651.htm#page=3&amp;position=41">Cute Rabbit</a>
<a target="_blank" href="https://www.freepik.com/free-vector/cute-bear-character-cartoon-illustration_12341164.htm#page=1&amp;position=40#&amp;position=40">Cute Bear</a></p>
<p><a target="_blank" href="https://csslayout.io/patterns/">More layout Designs here</a></p>
<p><strong>YouTube <a target="_blank" href="https://www.youtube.com/c/joyshaheb">/ Joy Shaheb</a></strong></p>
<p><strong>Twitter <a target="_blank" href="https://twitter.com/JoyShaheb">/ JoyShaheb</a></strong></p>
<p><strong>Instagram <a target="_blank" href="https://www.instagram.com/joyshaheb/">/ JoyShaheb</a></strong></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
