<?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[ css flex - 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[ css flex - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 14 May 2026 20:25:06 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/css-flex/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Flexbox vs Grid in CSS – Which Should You Use? ]]>
                </title>
                <description>
                    <![CDATA[ Have you ever had issues with replicating a particular user interface from a Figma design? Do you find it difficult displaying elements in different sections of your browser screen? A major cause of rift between user interface (UI) designers and fron... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/flexbox-vs-grid-in-css/</link>
                <guid isPermaLink="false">66c71fd6cdd5b7ce3d9c37e8</guid>
                
                    <category>
                        <![CDATA[ css flex ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS Grid ]]>
                    </category>
                
                    <category>
                        <![CDATA[ layout ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Mabel Obadoni ]]>
                </dc:creator>
                <pubDate>Fri, 16 Feb 2024 11:02:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/02/pankaj-patel-6JVlSdgMacE-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Have you ever had issues with replicating a particular user interface from a Figma design? Do you find it difficult displaying elements in different sections of your browser screen?</p>
<p>A major cause of rift between user interface (UI) designers and frontend developers is the issue of having an awesome UI design being difficult to translate into real life features.</p>
<p>Almost every frontend developer has faced similar fears at certain points during their coding journey. This article will help you understand how CSS displays work with regards to flexbox and grid layout.</p>
<h2 id="heading-table-of-contents">Table of Contents:</h2>
<ul>
<li><a class="post-section-overview" href="#heading-origin-of-css">Origin of CSS</a></li>
<li><a class="post-section-overview" href="#heading-basic-structure-of-css">Basic Structure of CSS</a></li>
<li><a class="post-section-overview" href="#heading-css-layout-flex-and-grid">CSS Layout (Flex and Grid)</a></li>
<li><a class="post-section-overview" href="#heading-how-to-use-the-flex-layout-in-css">How to Use the Flex Layout in CSS</a></li>
<li><a class="post-section-overview" href="#heading-how-to-use-the-flex-layout-in-css">How to Use the Grid Layout in CSS</a></li>
<li><a class="post-section-overview" href="#heading-similarities-between-flex-and-grid">Similarities between Flex and Grid</a></li>
<li><a class="post-section-overview" href="#heading-when-to-use-flex">When to Use Flex</a></li>
<li><a class="post-section-overview" href="#heading-when-to-use-grid">When to Use Grid</a></li>
<li><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></li>
</ul>
<h2 id="heading-origin-of-css">Origin of CSS</h2>
<p>The earliest websites were not only static, but were also text-based. During the early years of the World Wide Web, web pages were written in markup languages such as XHTML and the like.</p>
<p>This is what they looked like:</p>
<p><img src="https://lh5.googleusercontent.com/Wx0Au7mNXzu7gi_nFVSJ1T-CRXYiBBcaXBMEEWYJ_GNvm7e3vsN1UTdkPXmGGuKicXmdl60JjMEm2mJQT4vUmJc4oAxTfMiwV-MoK8tMIwhNcQZo58ziVlONzkRc9CrkAMYfz2GaxQykIOacs9-vAv4" alt="Image" width="600" height="400" loading="lazy">
<em><a target="_blank" href="https://www.history.com/news/the-worlds-first-web-site">https://www.history.com/news/the-worlds-first-web-site</a></em></p>
<p>With advancements in various technologies, technology enthusiasts were interested in making websites look more pleasant by adding some design. This led to the development of Cascading Style Sheets, better known as CSS. </p>
<p>Although CSS has gone through several evolutions, it is responsible for the aesthetics of websites. Imagine a website or a software application with just text and no design.</p>
<p>Hey! This article is not about history, so let’s get straight to business.</p>
<p>As a software developer majoring in client side applications (often referred to as front-end development), you’d agree with me that CSS is that lovely fellow who looks simple yet sophisticated. </p>
<p>CSS can keep you awake at night while trying to find out why your page is not aligning as expected or the reason your page content is chopped off on certain screens and not responsive too.</p>
<p>One of the reasons why beginners in software development often abandon front-end to major in back-end is the “stress” that comes with writing CSS code, especially with vanilla CSS where you have to manually input virtually every style rule. This is because CSS requires some level of calculation mixed with attention to the most minimal detail.</p>
<p>A major aspect of CSS that often creates confusion is the CSS layout — writing the CSS rule for the page layout can be a bit confusing as a beginner.</p>
<p>In the sections that follow, we'll focus on:</p>
<ul>
<li>The similarities between flex and grid displays.</li>
<li>When to use flex or grid.</li>
</ul>
<h2 id="heading-basic-structure-of-css"><strong>Basic Structure of CSS</strong></h2>
<p>Before discussing the CSS display in detail, let’s take a brief look at the CSS structure. </p>
<p>For starters, the CSS structure is a box-model. This means that every webpage is treated like a box, similar to having a pizza in a pizza box. </p>
<p>The pizza box here represents the browser page while the pizza represents the content of your website or application, which can be text, graphics, or multimedia.</p>
<p><img src="https://lh7-us.googleusercontent.com/EJbG9BG5EZ7CU9sBtOQJVBWo7cbc8nwG8pjYxqQ6kgll7lTN7wH8XQYy35zZFgbMhiQhZLy3BqnEA-brNtaS8NBcB-XPLMzk0zBvAr-frniqSogpYmDWs7qqgiloBjXuNK2mWZIIymLfpfa880m1HjU" alt="Image" width="600" height="400" loading="lazy">
<em>A box of pizza</em></p>
<p><img src="https://lh7-us.googleusercontent.com/lxBLqGMf-ZhMcZwKDpCPRHmax_Y3ZBst69uUF0pmZBZdmuRnh_woEy48OzbqXBwGAy5I38HAZ5SFW5fU-3LwsJArJNX3KBk0E2K6d2nH7SoXZKGHwJpQlLtxZrlbxwIWnjO9kLSfyi-wHf7u1WWAy4A" alt="Image" width="600" height="400" loading="lazy">
<em>CSS box model</em></p>
<h2 id="heading-css-layout-flex-and-grid"><strong>CSS Layout (Flex and Grid)</strong></h2>
<p>Now that you know how CSS structures the content of every web or application page, a vital aspect of CSS which you should get a grasp of is the layout, which is the crux of this article.</p>
<p>In architecture, every building has its structure. The structure for a commercial building might differ from that of a residential building. Same goes for front-end development. </p>
<p>The purpose of the application goes a long way in the selecting a layout for the. For instance, an e-commerce website would opt for a grid layout for proper arrangement and display of their products.</p>
<p>The layout of every website or application determines how the content on the page will be arranged: either in stacks of rows and columns or just columns across various screen-sizes. </p>
<p>It is also worthy to note that you can use more than one display on a particular webpage. This means that you can display a particular <code>div</code> as a grid and another <code>div</code> as a flexbox. It all boils down to the content of each <code>div</code> and how you want them to appear.</p>
<p>There are two ways to design the layout of a web page’s content in CSS:</p>
<ul>
<li>Flex</li>
<li>Grid</li>
</ul>
<h3 id="heading-how-to-use-the-flex-layout-in-css">How to Use the Flex Layout in CSS</h3>
<p>The flex layout uses a method of arranging the web content in rows (main axis) or columns (perpendicular axis). This implies that it is a one-dimensional layout. The main axis can move in reverse order, from right to left.</p>
<p>The flex direction can be set to:</p>
<ul>
<li>Row</li>
<li>Row-reverse</li>
<li>Column</li>
<li>Column-reverse</li>
</ul>
<p>Using flexbox does not split the screen or content into equal parts. It is worthy to note that, flexbox doesn’t consider splitting content into equal columns across the row or stacking columns in the same alignment. Rather, it expands or shrinks the content to contain the available screen space.</p>
<p>Below is a diagram of a flex display using rows:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/Flex-1.png" alt="Image" width="600" height="400" loading="lazy">
<em>A graphical representation of a display set to flex</em></p>
<h4 id="heading-how-to-set-display-to-flex">How to Set Display to Flex</h4>
<p>To create a flex layout:</p>
<ul>
<li>Give the parent element a style rule of flex display</li>
<li>Give the children element some margin and padding for a better look</li>
<li>Specify the direction of the flex if there is a need to</li>
</ul>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span> Setting Display to Flex<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-selector-class">.fl</span>{
        <span class="hljs-attribute">display</span>: flex;
        <span class="hljs-attribute">flex-wrap</span>: wrap;
    }
    <span class="hljs-selector-class">.ex</span> {
        <span class="hljs-attribute">flex-direction</span>:row;
        <span class="hljs-attribute">flex</span>: <span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">100px</span>; 
        <span class="hljs-attribute">margin</span>: <span class="hljs-number">5px</span>; 
        <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>; 
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0000FF</span>; 
        <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ffff</span>; <span class="hljs-comment">/* I added a border all round each column for better visibility and separation of the columns */</span>
    }
</span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<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">"fl"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"ex"</span>&gt;</span>Content 1<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">"ex"</span>&gt;</span>Content 2<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">"ex"</span>&gt;</span>Content 3<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">"ex"</span>&gt;</span>Content 4<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">"ex"</span>&gt;</span>Content 5<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>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>The code above  styles the body by selecting the parent container and assigning it a layout of <code>flex</code>. Then the direction of the children element is set to <code>row</code> using the <code>flex-direction</code> rule.</p>
<h3 id="heading-how-to-use-the-grid-layout-in-css">How to Use the Grid Layout in CSS</h3>
<p>The grid layout, on the other hand, divides a web-page into 12 equal columns. These columns can be further split into the desired rows and columns. </p>
<p>The entire screen (100%) divided into 12 gives about 3.33% per column. The screen can also be styled using the number of columns in multiples of 2. That is: 2,4,6,8,10 and 12 with each number specifying the number of columns or width the element is expected to occupy.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/02/image-120.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h4 id="heading-how-to-set-display-to-grid">How to Set Display to Grid</h4>
<ul>
<li>Set the parent element's display to grid</li>
<li>Specify the dimension(i.e rows or columns) of the grid using the grid-template property</li>
<li>Give some spacing(margin,padding, row-gap, etc) to the child element so the grid can be more visible</li>
</ul>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Setting Display to Grid<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-selector-class">.fl</span>{
        <span class="hljs-attribute">display</span>: grid;
        <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>fr); <span class="hljs-comment">/* This would display the children elements in  three equal columns with equal width */</span>
        <span class="hljs-attribute">gap</span>: <span class="hljs-number">5px</span>; <span class="hljs-comment">/* To set a gap between  thegrid items */</span>
    }
    <span class="hljs-selector-class">.gr</span> {
        <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>; 
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0000FF</span>; 
        <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ffff</span>; <span class="hljs-comment">/* I added a border all round each column for better visibility and separation of the columns */</span>
    }
</span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<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">"gr"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"gr"</span>&gt;</span>Content 1<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">"gr"</span>&gt;</span>Content 2<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">"gr"</span>&gt;</span>Content 3<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">"gr"</span>&gt;</span>Content 4<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">"gr"</span>&gt;</span>Content 5<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>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h2 id="heading-similarities-between-flex-and-grid">Similarities between Flex and Grid</h2>
<ul>
<li><p><strong>They are both responsive layouts</strong>: Irrespective of which layout you decide to use for your application, provided that you style the properties correctly, both layout types are responsive to different screen sizes.</p>
</li>
<li><p><strong>Setting the parent element's layout first</strong>: In styling a flex or grid layout, the parent element is the one styled with the exact layout in question.</p>
</li>
</ul>
<h2 id="heading-when-to-use-flex"><strong>When to Use Flex</strong></h2>
<p>CSS flex is popular for its order ability (row-reverse, column-reverse), which enables the developer to reorder content without having to change the HTML content manually. There are several uses cases of the CSS flex, such as:</p>
<ul>
<li><strong>Building One-dimensional Layouts:</strong> For web-pages or sections with a single layout, it is best to use flex as it helps in proper arrangement of the content. </li>
<li><strong>Alignment and Distribution of Content</strong>: Thanks to justify-content, <code>align-self</code> and other properties, alignment and distribution of content is made easy using flex.</li>
<li><strong>Displaying Columns in Equal heights</strong>: Using the <code>align-items</code> property and setting it to a value of <code>stretch</code>, that is: <code>align-items:stretch</code>, CSS flex ensures that columns within a flexbox are of equal heights. 
This implies that if there is a higher column, other columns will be stretched to meet up with the highest column. You can use CSS flex to display your columns in equal height irrespective of the height of the content.</li>
</ul>
<h2 id="heading-when-to-use-grid"><strong>When to Use Grid</strong></h2>
<p>The grid layout is the most commonly used by frontend developers because it allows you to place elements on different sections of the browser page while maintaining proper alignment.  You can use the grid layout when:</p>
<ul>
<li><strong>Building a Responsive Design:</strong> Often times, user interfaces are developed to be adaptable to whatever screen they're being displayed on. In such cases, the grid layout is your best bet because it gives room to flexibility and resizing of the element.</li>
<li><strong>Control of whitespace:</strong> Unlike the flex display that leaves some white space at the extreme, the CSS grid controls white space by distributing elements equally along the row and also based on the allocated column space.</li>
<li><strong>Consistency in Design Layout:</strong> The CSS grid offers a consistent pattern in the structure of a web page. This arrangement of elements makes future editing of the page easier.</li>
</ul>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>One tip that comes very handy when styling a page is to choose which of the display modes to employ. </p>
<p>Once that is settled, the various sections of the page can follow suit bearing in mind what the entire body holds. </p>
<p>Although not an advice, recent practices have seen developers use grid more often than flexbox. Whichever the case, ensure not to mix up the code for the respective displays so you do not run into errors.</p>
<p>Happy Coding!  </p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Use CSS Flexbox to Break Elements Out of Their Containers ]]>
                </title>
                <description>
                    <![CDATA[ When you're building a webpage, sometimes you might want images or other elements to go beyond the boundaries of a container. So in this tutorial, I'll show you a couple ways you can accomplish this. To illustrate a situation when you might find this... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/break-out-elements-from-containers-using-flexbox/</link>
                <guid isPermaLink="false">66bb9089b0d3ac3d7acde3ff</guid>
                
                    <category>
                        <![CDATA[ css flex ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ kyw ]]>
                </dc:creator>
                <pubDate>Mon, 08 Jan 2024 18:15:47 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/01/pexels-dagmara-dombrovska-19707371.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When you're building a webpage, sometimes you might want images or other elements to go beyond the boundaries of a container. So in this tutorial, I'll show you a couple ways you can accomplish this.</p>
<p>To illustrate a situation when you might find this useful, let's say we are given a webpage that looks like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/847shots_so.png" alt="Image" width="600" height="400" loading="lazy">
<em>A webpage whose contents span 100% of the window's width</em></p>
<p>As you will probably agree, that's not very pleasant to read. One of the things you can do is set a <code>max-width</code> on the container – in this case it's the <code>article</code> element so that it's "contained" in a more pleasing way:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">article</span> {
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">60ch</span>; <span class="hljs-comment">/* no more than 60 characters per line in this container */</span>
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/645shots_so-1.png" alt="Image" width="600" height="400" loading="lazy">
<em>Use max-width property to constrain the width of a container</em></p>
<p>That's better, but it's off to the left. So now, let's place our article text and image at the center of the page by setting <code>margin: 0 auto</code> on the container:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">article</span> {
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">60ch</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto; <span class="hljs-comment">/* center this container */</span>
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/945shots_so.png" alt="Image" width="600" height="400" loading="lazy">
<em>Center our article with margin: 0 auto</em></p>
<p>Much better now, right? To further optimize readability, we can tweak the font's size and line height as I have done in the playground below:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/kilgarenone/embed/NWJPePO" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p>Now, notice the width of the image: It has been constrained by the width of the container. What if you want the image (or any other element) to span the entire page (that is, full-bleed), like this?</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/868shots_so.png" alt="Image" width="600" height="400" loading="lazy">
<em>Example of a full-bleed layout</em></p>
<p>Or what if you just want the image to "break out" to have any width that is wider than its container, like this?</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/539shots_so.png" alt="Image" width="600" height="400" loading="lazy">
<em>An element breaking out to have a width wider than that of the container</em></p>
<p>Currently, as far as I know, there are two main ways to accomplish this:</p>
<ol>
<li>You can either do a manual <a target="_blank" href="https://css-tricks.com/full-width-containers-limited-width-parents/">horizontal offset</a> via negative <code>margin-left</code> and <code>translateX</code> to shift the image to the left, or </li>
<li>You can use <a target="_blank" href="https://www.bram.us/2017/06/20/breaking-elements-out-of-their-containers-in-css/">CSS Grid</a>.</li>
</ol>
<p>But to my mind, the first solution is hacky, and the second solution is heavy-handed.</p>
<p>In this article, I'll cover some simpler ways to do this.</p>
<h2 id="heading-setting-the-stage">Setting the Stage</h2>
<p>First of all, let's free up all the child elements inside the container. Instead of setting a width on the container, we set it to its child elements:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Instead of doing this
article {
  max-width: 60ch;
  margin: 0 auto;
}
*/</span>

<span class="hljs-comment">/* we set width on its direct child elements */</span>
<span class="hljs-selector-tag">article</span> &gt; * {
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">60ch</span>; <span class="hljs-comment">/* childs' width can't go wider than 60ch */</span>
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto; <span class="hljs-comment">/* center them */</span>
}
</code></pre>
<p><strong>Before:</strong></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/504shots_so.png" alt="Image" width="600" height="400" loading="lazy">
<em>All children can't grow beyond the confines of their container</em></p>
<p><strong>After:</strong></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/409shots_so.png" alt="Image" width="600" height="400" loading="lazy">
<em>Now each child has plenty of space to grow horizontally if it chooses to</em></p>
<p>The  <code>article</code> container is no longer limited to a specific width. It now spans the entire width of the window. This has allowed any child element to grow sideways until the boundary of the window.</p>
<p>For example, we can choose to specifically let the image to grow to our heart's content:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/4shots_so.png" alt="Image" width="600" height="400" loading="lazy">
<em>The <code>img</code> element growing wider than the <code>p</code> elements</em></p>
<p>This is exactly what we are going to learn how to do next.</p>
<h2 id="heading-how-to-break-out-childless-elements">How to Break Out Childless Elements</h2>
<p>Breaking out at its simplest form is when the elements we want to break out are childless. For example, say we want to break out the <code>img</code> element below:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">article</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Texts<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Texts<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">article</span>&gt;</span>
</code></pre>
<p>And let's make it <em>full-bleed</em>. To do that, we will apply 3 properties on the <code>img</code>:</p>
<ol>
<li><code>**display: block**</code> – Because a) <code>img</code> is inline by default, and b) <code>margin: 0 auto</code> only works on <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/margin-left#auto">block</a> elements. </li>
<li><code>**width: 100%**</code> – To have it fully fill the width of its container which is <code>article</code> that already fills the window.</li>
<li><code>**max-width: 100%**</code> – To override the <code>max-width: 60ch</code> and also to stop it from expanding beyond the available horizontal space of its container. </li>
</ol>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">display</span>: block;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">100%</span>; 
}
</code></pre>
<p>And it works:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2024/01/914shots_so.png" alt="Image" width="600" height="400" loading="lazy">
<em>Full-bleed layout</em></p>
<p>Following up on the previous playground:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/kilgarenone/embed/QWowRME" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p>So far so good.</p>
<h2 id="heading-how-to-break-out-elements-that-have-children">How to Break Out Elements that Have Children</h2>
<p>Now, what if your image is wrapped in a <code>figure</code>, like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">figure</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"900px"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">""</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">figcaption</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">figcaption</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">figure</span>&gt;</span>
</code></pre>
<p>And the image has its width set or even just uses its intrinsic width? How will we break it out while centering it? </p>
<p>Without leveraging CSS Flexbox, to make the image full-bleed as before, first we need to set the <code>figure</code> to <code>max-width: 100%</code> so that it can fill the entire horizontal space of <code>article</code>. Next, we need to make sure our <code>img</code> is applied with <code>display: block</code> and <code>margin: 0 auto</code> in order to stay centered.</p>
<p>Compare this to if we were to use <strong>CSS Flexbox</strong>. Apply a CSS class that contains 3 Flexbox properties to <code>figure</code> alone and we are done:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.break-out</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: column;
  <span class="hljs-attribute">align-items</span>: center;
}
</code></pre>
<ol>
<li><code>**display: flex**</code> – Set <code>figure</code> as a Flex container from which we can choose to arrange its content – in this case they would be the <code>img</code> and <code>figcaption</code> – either horizontally (row) or vertically (column) along the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox#the_main_axis">main axis</a>. </li>
<li><code>**flex-direction: column**</code> – For our use case, we choose to arrange the content vertically.</li>
<li><code>**align-items: center**</code> – Lastly, we choose to always center the content along the cross axis. If main axis is going vertically in <code>flex-direction: column</code> , its cross axis would be going horizontally. So this property allows <code>img</code> and <code>figcaption</code> to always be centered horizontally relative to <code>figure</code> which is now spanning 100% of the screen. </li>
</ol>
<p>Finally, we just apply the class to the <code>figure</code> element:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">figure</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"break-out"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"900px"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">""</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">figcaption</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">figcaption</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">figure</span>&gt;</span>
</code></pre>
<p>Now whatever the width of the <code>img</code> may be, it will be centered even until it's full-bleed:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/kilgarenone/embed/eYXNyjp" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<h2 id="heading-benefits-of-these-methods">Benefits of These Methods</h2>
<p>The methods I have demonstrated here have the following benefits:</p>
<ol>
<li><a target="_blank" href="https://www.w3.org/2001/tag/doc/leastPower.html">Principle of Least Power</a>: CSS Flexbox is a less powerful solution than CSS Grid.</li>
<li>No complicated calculation: We let the browser center our image within the available space.</li>
<li>Responsive: No re-calculation needed on our part when the browser resizes.</li>
</ol>
<p>The second-order benefits are:</p>
<ol>
<li>For users: optimal performance – less code shipped and less expensive code to run. Happy browser, happy users.</li>
<li>For developers: simpler and less code to maintain, which means less valuable cognitive resources spent.</li>
</ol>
<h2 id="heading-background">Background</h2>
<p>I found this method when I was building Zuunote. It's a <a target="_blank" href="https://zuunote.com/">Markdown-based note-taking web app</a> in which images can be resized. </p>
<p>The tricky thing is, in Markdown, image syntax is parsed as an inline element. So, when users do create inline images when writing in a paragraph, this method enables them to resize between inline and full-bleed.</p>
<p>This is how I achieved it. Similar to what we just discussed, I wrapped the <code>img</code> element in a <code>span</code> to retain the inline characters:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">""</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
</code></pre>
<p>Then I applied our Flexbox properties on the <code>span</code> when the user has resized beyond the boundary of the paragraphs:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"break-out"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">""</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
</code></pre>
<p>And the browser will keep the image centered without any expensive hand-holding on my part.</p>
<p>Here is the result:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/kilgarenone/embed/bGZdEQJ" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p>One gesture covers a spectrum of resizing intentions – I think that's pretty neat :)</p>
<p>Thank you for reading! </p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ CSS Image Centering – How to Center an Image in a Div ]]>
                </title>
                <description>
                    <![CDATA[ When you're working on the front-end of a web page, you sometimes need to center an image within a div (container). This can become tricky at times. And based on certain conditions, a particular method may not work at some point, leaving you searchin... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-center-an-image-in-a-div-css/</link>
                <guid isPermaLink="false">66d45f8836c45a88f96b7cf7</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ css flex ]]>
                    </category>
                
                    <category>
                        <![CDATA[ flexbox ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Joel Olawanle ]]>
                </dc:creator>
                <pubDate>Tue, 16 Aug 2022 19:32:10 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/08/cover-template.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When you're working on the front-end of a web page, you sometimes need to center an image within a <code>div</code> (container).</p>
<p>This can become tricky at times. And based on certain conditions, a particular method may not work at some point, leaving you searching for alternatives.</p>
<p>In this article, you will learn how to center an image in a <code>div</code> with CSS.</p>
<h2 id="heading-how-to-center-a-div-using-css">How to Center a <code>div</code> using CSS</h2>
<p>You center an image in a <code>div</code> in two ways: horizontally and vertically. When you put these two methods together, you will have an entirely centered image:</p>
<p><img src="https://paper-attachments.dropbox.com/s_E4F69027FF573CB7A65859895F7B6CD8342925344584ACB8BE37F8B299430A72_1660599829888_Untitled.png" alt="s_E4F69027FF573CB7A65859895F7B6CD8342925344584ACB8BE37F8B299430A72_1660599829888_Untitled" width="600" height="400" loading="lazy"></p>
<p>By default, web content always begins from the top-left corner of the screen and moves from <code>ltr</code> (left to right) – except for certain languages like Arabic, which goes from <code>rtl</code> (right to left).</p>
<p>Let’s start by seeing how to center an image within a <code>div</code> horizontally. Then we'll see how to center vertically. Finally, we'll see how you can do both together.</p>
<p><img src="https://paper-attachments.dropbox.com/s_E4F69027FF573CB7A65859895F7B6CD8342925344584ACB8BE37F8B299430A72_1660702367688_image.png" alt="s_E4F69027FF573CB7A65859895F7B6CD8342925344584ACB8BE37F8B299430A72_1660702367688_image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-center-an-image-in-a-div-horizontally-with-text-align">How to Center an Image in a Div Horizontally with Text-align</h3>
<p>Suppose you have a <code>div</code> in which you place your image this way:</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">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./fcc-logo.png"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"FCC Logo"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>And apply basic CSS styling so your image is visible:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
}
<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;
}
</code></pre>
<p>The <code>text-align</code> method won't work in all cases, as you typically use it to center text. But when you have your images within a block level container like a <code>div</code>, then this method will work:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
    <span class="hljs-attribute">text-align</span>: center;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;
}
</code></pre>
<p>This works by adding the <code>text-align</code> property alongside its value of <code>center</code> to the container and not the image itself.</p>
<h3 id="heading-how-to-center-an-image-in-a-div-horizontally-with-margin-auto">How to Center an Image in a Div Horizontally with Margin-auto</h3>
<p>Another method that you can use to horizontally center an image within a <code>div</code> (container) is the <code>margin</code> property with the value of <code>auto</code>.</p>
<p>The element will then take up the <strong>specified</strong> <code>width</code>, and the remaining space will be split equally between the left and right margins.</p>
<p>You would usually apply this method to the image itself and not the container. But unfortunately, this property alone doesn't work. You also need to specify the <code>width</code> the image will take first. This lets the margin know the remaining width the container has so that it can be split equally.</p>
<p>Secondly, <code>img</code> is an inline element, and the <code>margin-auto</code> property set does not affect inline-level elements. This means you must first convert it to a block-level element with <code>display</code> property set as <code>block</code>.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100px</span>;
    <span class="hljs-attribute">margin</span>: auto;
    <span class="hljs-attribute">display</span>: block;
}
</code></pre>
<h3 id="heading-how-to-center-an-image-in-a-div-horizontally-with-the-position-and-transform-properties">How to Center an Image in a Div Horizontally with the Position and Transform Properties</h3>
<p>Another method you can use to position an image horizontally is the <code>position</code> property alongside the <code>transform</code> property.</p>
<p>This method can be very tricky, but it works. You must first set the container’s <code>position</code> to <code>relative</code>, then the image to <code>absolute</code>.</p>
<p>Once you do this, you can now move the image to whichever position you wish using either the <code>left</code>, <code>top</code>, <code>bottom</code>, or <code>right</code> properties on the image.</p>
<p>In this case, you only want to move the image to the center horizontally. This means you would move the image via the <code>left</code> to 50% or <code>right</code> to -50%:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
    <span class="hljs-attribute">position</span>: relative;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</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">position</span>: absolute;
    <span class="hljs-attribute">left</span>: <span class="hljs-number">50%</span>;
}
</code></pre>
<p>But when you check your image, you will notice that the image is still not perfectly placed in the center. This is because it started from the 50% mark, which is the center position.</p>
<p>In this case, you need to use the <code>transform-translateX</code> property to adjust it to get the perfect center horizontally:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
    <span class="hljs-attribute">position</span>: relative;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</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">position</span>: absolute;
    <span class="hljs-attribute">left</span>: <span class="hljs-number">50%</span>;
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateX</span>(-<span class="hljs-number">50%</span>);
}
</code></pre>
<h3 id="heading-how-to-center-an-image-in-a-div-horizontally-with-display-flex">How to Center an Image in a Div Horizontally with Display-Flex</h3>
<p>CSS flexbox makes it easier for you to design flexible, responsive layout structures without using float or positioning. We can also use this to place an image in the center horizontally of a container using the display property with flex as its value.</p>
<p>But this alone doesn't work. You also need to define the position where you want your image. This could be <code>center,</code> <code>left</code> or maybe <code>right</code>:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">justify-content</span>: center;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</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>;
}
</code></pre>
<p><strong>Note:</strong> The <code>display: flex</code> property is not supported in older versions of browsers. You can read more <a target="_blank" href="https://caniuse.com/#search=display%20flex">here</a>. You’ll also notice that the width and height of the image are defined to ensure the image doesn't shrink.</p>
<p>Let’s now learn how to center an image in a <code>div</code> vertically. Later we'll see how to center an image in a <code>div</code> horizontally and vertically together, making it a perfect center.</p>
<p><img src="https://paper-attachments.dropbox.com/s_E4F69027FF573CB7A65859895F7B6CD8342925344584ACB8BE37F8B299430A72_1660702330431_image.png" alt="s_E4F69027FF573CB7A65859895F7B6CD8342925344584ACB8BE37F8B299430A72_1660702330431_image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-center-an-image-in-a-div-vertically-with-display-flex">How to Center an Image in a Div Vertically with Display-Flex</h3>
<p>Just like how you were able to center the image horizontally with the display-flex method, you can also do the same vertically.</p>
<p>But this time around, you won’t need to use the <code>justify-content</code> property. Rather you'll use the <code>align-items</code> property:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">align-items</span>: center;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</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>;
}
</code></pre>
<p>For this method to work, the container must have a specified <code>height</code> which you will use to calculate the height and know where the center is located.</p>
<h3 id="heading-how-to-center-an-image-in-a-div-vertically-with-the-position-and-transform-properties">How to Center an Image in a Div Vertically with the Position and Transform Properties</h3>
<p>Similar to how you used the <code>position</code> and <code>transform</code> properties earlier to place your image in the center horizontally, you can also do the same vertically.</p>
<p>But this time around, you won't use <code>left</code> or <code>right,</code>. Instead you will use <code>top</code> or <code>bottom</code> alongside <code>translateY</code> rather than <code>translateX</code>:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
    <span class="hljs-attribute">position</span>: relative;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</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">position</span>: absolute;
    <span class="hljs-attribute">top</span>: <span class="hljs-number">50%</span>;
    <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateY</span>(-<span class="hljs-number">50%</span>);
}
</code></pre>
<p>You have learned how to center an image within a <code>div</code> horizontally and vertically using all possible methods. Let’s now learn how to center both horizontally and vertically.</p>
<p><img src="https://paper-attachments.dropbox.com/s_E4F69027FF573CB7A65859895F7B6CD8342925344584ACB8BE37F8B299430A72_1660702293626_image.png" alt="s_E4F69027FF573CB7A65859895F7B6CD8342925344584ACB8BE37F8B299430A72_1660702293626_image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-center-an-image-in-a-div-horizontally-and-vertically-with-display-flex">How to Center an Image in a Div Horizontally and Vertically with Display-Flex</h3>
<p>The <code>display-flex</code> property is a combination of how you'd center the image vertically and horizontally.</p>
<p>For the flex method, this means you will use both the <code>justify-content</code> and <code>align-items</code> properties set to <code>center</code>:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</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-selector-class">.container</span> <span class="hljs-selector-tag">img</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>;
}
</code></pre>
<h3 id="heading-how-to-center-an-image-in-a-div-horizontally-and-vertically-with-the-position-and-transform-properties">How to Center an Image in a Div Horizontally and Vertically with the Position and Transform Properties</h3>
<p>This is also very similar, as all you have to do is combine both ways you were able to center vertically and horizontally:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
    <span class="hljs-attribute">position</span>: relative;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</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">position</span>: absolute;
    <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">translateX</span>(-<span class="hljs-number">50%</span>) <span class="hljs-built_in">translateY</span>(-<span class="hljs-number">50%</span>);
}
</code></pre>
<p>You can also combine the <code>translateX</code> and <code>translateY</code> by using <code>translate(x,y)</code>:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">200px</span>;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#0a0a23</span>;
    <span class="hljs-attribute">position</span>: relative;
}

<span class="hljs-selector-class">.container</span> <span class="hljs-selector-tag">img</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">position</span>: absolute;
    <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>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>In this article, you have learned how to center an image in a div vertically, horizontally, or both.</p>
<p>You will often use the Flexbox method when moving an image to the center because the <code>position</code> method can distort your web page and works very trickily.</p>
<p>You can learn more about the <a target="_blank" href="https://www.freecodecamp.org/news/css-position-property-explained/">CSS position method here</a> and then more about the <a target="_blank" href="https://www.freecodecamp.org/news/css-flexbox-tutorial-with-cheatsheet/">flexbox method here.</a></p>
<p>Have fun coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ HTML Center Image – CSS Align Img Center Example ]]>
                </title>
                <description>
                    <![CDATA[ If you're making websites with HTML and CSS, you will be working with images a lot.  Developers often struggle with image alignment in CSS, especially when trying to figure out how to center an image. Centering anything in CSS is not really a straigh... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/html-center-image-css-align-img-center-example/</link>
                <guid isPermaLink="false">66adf1606f5e63db3fc43622</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ css flex ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS Grid ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Tue, 01 Feb 2022 21:16:57 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/02/arrows-2889040_1920.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you're making websites with HTML and CSS, you will be working with images a lot. </p>
<p>Developers often struggle with image alignment in CSS, especially when trying to figure out how to center an image.</p>
<p>Centering anything in CSS is not really a straightforward thing - especially for beginners. This is why people brag about being able to center a div. :)</p>
<p>Since the <code>img</code> element is an inline element, this makes it a little bit harder to center. But don't worry, you can convert the image to a block element and then center it.</p>
<p>In this article, I'm going to show you 4 different ways you can align an image to the center.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><a class="post-section-overview" href="#heading-how-to-center-an-image-with-the-text-align-property">How to Center an Image With the Text Align Property</a></li>
<li><a class="post-section-overview" href="#heading-how-to-center-an-image-with-flexbox">How to Center an Image with Flexbox</a></li>
<li><a class="post-section-overview" href="#heading-how-to-center-an-image-with-css-grid">How to Center an Image with CSS Grid</a></li>
<li><a class="post-section-overview" href="#heading-how-to-center-an-image-with-the-margin-property">How to Center an Image with the Margin Property</a></li>
</ul>
<h2 id="heading-how-to-center-an-image-with-the-text-align-property">How to Center an Image With the Text Align Property</h2>
<p>You can center an image with the <code>text-align</code> property. </p>
<p>One thing you should know is that the tag for bringing in images – <code>img</code> – is an inline element. Centering with the <code>text-align</code> property works for block-level elements only.</p>
<p>So how do you center an image with the text-align property? You wrap the image in a block-level element like a <code>div</code> and give the <code>div</code> a <code>text-align</code> of <code>center</code>.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"fcc22.png"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"freeCodeCamp"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {
      <span class="hljs-attribute">text-align</span>: center;
    }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-1.png" alt="ss-1" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-center-an-image-with-flexbox">How to Center an Image with Flexbox</h2>
<p>The introduction of CSS Flexbox made it easier to center anything.</p>
<p>Flexbox works by putting what you want to center in a container and giving the container a <code>display</code> of <code>flex</code>. Then it sets <code>justify-content</code> to <code>center</code> as shown in the code snippet below:</p>
<pre><code class="lang-css">  <span class="hljs-selector-tag">div</span> {
      <span class="hljs-attribute">display</span>: flex;
      <span class="hljs-attribute">justify-content</span>: center;
    }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-1.png" alt="ss-1" width="600" height="400" loading="lazy"></p>
<p><strong>P.S.:</strong> A <code>justify-content</code> property set to <code>center</code> centers an image horizontally. To center the image vertically too, you need to set <code>align-items</code>to <code>center</code>.</p>
<h2 id="heading-how-to-center-an-image-with-css-grid">How to Center an Image with CSS Grid</h2>
<p>CSS Grid works like Flexbox, with the added advantage that Grid is multidimensional, as opposed to Flexbox which is 2-dimensional.</p>
<p>To center an image with CSS Grid, wrap the image in a container div element and give it a display of <code>grid</code>. Then set the <code>place-items</code> property to center. </p>
<pre><code class="lang-css"> <span class="hljs-selector-tag">div</span> {
      <span class="hljs-attribute">display</span>: grid;
      <span class="hljs-attribute">place-items</span>: center;
    }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-1.png" alt="ss-1" width="600" height="400" loading="lazy"></p>
<p><strong>P.S.:</strong><code>place-items</code> with a value of <code>center</code> centers anything horizontally and vertically.</p>
<h2 id="heading-how-to-center-an-image-with-the-margin-property">How to Center an Image with the Margin Property</h2>
<p>You can also center an image by setting a left and right margin of auto for it. But just like the <code>text-align</code> property, <code>margin</code> works for block-level elements only. </p>
<p>So, what you need to do is convert the image to a block-level element first by giving it a display of block.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">img</span> {
      <span class="hljs-attribute">display</span>: block;
      <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
    }
</code></pre>
<p>Those 2 properties could be enough. But sometimes, you have to set a width for the image, so the left and right margin of auto would have spaces to take.</p>
<pre><code class="lang-css"> <span class="hljs-selector-tag">img</span> {
      <span class="hljs-attribute">display</span>: block;
      <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
      <span class="hljs-attribute">width</span>: <span class="hljs-number">40%</span>;
    }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/ss-2.png" alt="ss-2" width="600" height="400" loading="lazy"></p>
<p><strong>P.S.:</strong> You might not have to go as low as 40% for the width. The image was distorted at a 60+ percentage, that’s why I went as low as 40%.</p>
<p>I hope this article helps you choose which method works best for you in centering an image.</p>
<p>Thank you for reading.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ The CSS Display Property – Display None, Display Table, Inline Block and More ]]>
                </title>
                <description>
                    <![CDATA[ In CSS, the display property determines how an element looks. It is also a crucial part of the presentation of your HTML code as it has a significant impact on layouts. In fact, to use the modern Flexbox and Grid models, you need to use the display p... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-css-display-property-display-none-display-table-inline-block-and-more/</link>
                <guid isPermaLink="false">66adf232db5636c0b30cba95</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ css flex ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS Grid ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kolade Chris ]]>
                </dc:creator>
                <pubDate>Thu, 19 Aug 2021 20:47:14 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/08/css-display-property.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In CSS, the display property determines how an element looks. It is also a crucial part of the presentation of your HTML code as it has a significant impact on layouts.</p>
<p>In fact, to use the modern Flexbox and Grid models, you need to use the display property before you get access to their various properties and values. This is one reason why the display property is so important in CSS.</p>
<p>Let's dive in and learn how to use the <code>display</code> property and all its different values.</p>
<h2 id="heading-basic-display-property-syntax">Basic <code>display</code> Property Syntax</h2>
<pre><code class="lang-html">element {
        display: value;
     }
</code></pre>
<h2 id="heading-display-property-values-in-css">Display Property Values in CSS</h2>
<p>There are inline and block-level elements in CSS. The difference between the two is that inline elements don't take up an entire space – that is, they don't start on a new line – but block elements do.</p>
<p>The display property takes many different values such as <code>inline</code>, <code>inline-block</code>, <code>block</code>, <code>table</code>, and more, which all influence the layout and presentation of an element on the web page. Also, to implement the flex and grid layouts, you need to use the display property.</p>
<p>You can use this display property to change an <code>inline</code> element to <code>block</code>, <code>block</code> element to <code>inline</code>, <code>block</code> and <code>inline</code> elements to <code>inline-block</code>, and many more.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/display-property-values.png" alt="display-property-values" width="600" height="400" loading="lazy"></p>
<h3 id="heading-display-inline"><code>display: inline</code></h3>
<p>An element with a display property set to <code>inline</code> will not start on a new line and it will take up the remaining/available screen width. It just takes up the space such an element would normally take.</p>
<p>Because of this, you can't set the <code>width</code> and <code>height</code> of an element that has a display of <code>inline</code>, becuase it does not take up the whole screen width.</p>
<p>Some elements are inline by default, like <code>&lt;span&gt;</code>, <code>&lt;a&gt;</code>, <code>&lt;i&gt;</code>, and <code>&lt;img&gt;</code>.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      Lorem ipsum dolor sit amet consectetur adipisicing elit.
      <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>This is an inline lement<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> Modi eaque debitis eos quod labore
      maiores delectus asperiores voluptatem voluptas soluta!
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"> <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">display</span>: flex;
        <span class="hljs-attribute">align-items</span>: center;
        <span class="hljs-attribute">justify-content</span>: center;
        <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
      }
<span class="hljs-selector-tag">div</span> {
        <span class="hljs-attribute">max-width</span>: <span class="hljs-number">600px</span>;
      }
<span class="hljs-selector-tag">span</span> {
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#006100</span>;
      }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/inline-display.png" alt="inline-display" width="600" height="400" loading="lazy"></p>
<h3 id="heading-display-block"><code>display: block</code></h3>
<p>An element that has the display property set to <code>block</code> starts on a new line and takes up the available screen width.</p>
<p>You can specify the <code>width</code> and <code>height</code> properties for such elements. Examples of elements that are at block-level by default are <code>&lt;div&gt;</code>, <code>&lt;section&gt;</code>, <code>&lt;p&gt;</code>, and lots more.</p>
<p>You can set the <code>span</code> from the previous HTML code to <code>block</code> display and it will behave like a block-level element.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">span</span> {
        <span class="hljs-attribute">display</span>: block;
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#006100</span>;
      }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/block-display.png" alt="block-display" width="600" height="400" loading="lazy"></p>
<p>You can see that the <code>&lt;span&gt;</code> takes up the full width. That's because it has a display property set to block.</p>
<h3 id="heading-display-inline-block"><code>display: inline-block</code></h3>
<p>Apart from block and inline display, there's also inline-block.</p>
<p>An element you assign a display of <code>inline-block</code> is inline by presentation. But it has the added advantage of you being able to apply <code>width</code> and <code>height</code> to it, which you can't do when the element is assigned a dispaly of <code>inline</code>.</p>
<p>So, you can look at the <code>inline-block</code> display as an inline element and block element in one package.</p>
<pre><code class="lang-css"> <span class="hljs-selector-tag">span</span> {
        <span class="hljs-attribute">display</span>: inline-block;
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#006100</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>;
      }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/inline-block-display.png" alt="inline-block-display" width="600" height="400" loading="lazy"></p>
<h3 id="heading-display-none"><code>display: none</code></h3>
<p>When you set the display property of an element to <code>none</code>, the element is completely taken off the page and it doesn’t have an effect on the layout.</p>
<p>This also means that devices like screen readers, which make websites accessible to blind people, wont't have access to the element.</p>
<p>Do not confuse <code>display: none</code> with <code>visibility: hidden</code>. The latter also hides the element, but leaves the space it would normally take open or empty.</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">span</span> {
        <span class="hljs-attribute">display</span>: none;
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#006100</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>;
      }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/display-none.png" alt="display-none" width="600" height="400" loading="lazy"></p>
<p>Visibility hidden leaves the space occupied by the span element open, as you can see below:</p>
<pre><code class="lang-css"> <span class="hljs-selector-tag">span</span> {
        <span class="hljs-attribute">visibility</span>: hidden;
        <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#006100</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>;
      }
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/visibility-hidden.png" alt="visibility-hidden" width="600" height="400" loading="lazy"></p>
<h3 id="heading-display-table"><code>display: table</code></h3>
<p>You'll rarely use a display value of <code>table</code> these days, but it's still important to know. It was more useful in the past because you would use it for layouts before the advent of floats, Flex, and Grid.</p>
<p>Setting display to <code>table</code> makes the element behave like a table. So you can make a replica of an HTML table without using the table element and corresponding elements such as <code>tr</code> and <code>td</code>.</p>
<p>For example, in HTML, you can make a table with the <code>&lt;table&gt;</code> element and also a <code>&lt;div&gt;</code>, or any container of your choice.</p>
<p>You make a table with the HTML <code>&lt;table&gt;</code> element like this:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Fruits<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Lemurs<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Pets<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Cashew<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Hua hua<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Dog<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Apple<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Diadem Sifaka<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Cat<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Mango<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Rig-tailed<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Chicken<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">align-items</span>: center;
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
}

<span class="hljs-selector-tag">div</span> {
    <span class="hljs-attribute">max-width</span>: <span class="hljs-number">600px</span>;
}

<span class="hljs-selector-tag">span</span> {
    <span class="hljs-attribute">display</span>: inline-block;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#006100</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-selector-tag">tr</span>,
<span class="hljs-selector-tag">td</span> {
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
}
</code></pre>
<p>The result of the HTML and CSS code snippets above looks like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/table-with-table-element.png" alt="table-with-table-element" width="600" height="400" loading="lazy"></p>
<p>But you can make the same table with the <code>&lt;div&gt;</code> element by setting the respective displays to <code>table</code>, <code>table-row</code>, and <code>table-cell</code>. You will get the same result as you can see below:</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">"table"</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"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"cell"</span>&gt;</span>Fruits<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">"cell"</span>&gt;</span>Lemurs<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">"cell"</span>&gt;</span>Pets<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"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"cell"</span>&gt;</span>Cashew<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">"cell"</span>&gt;</span>Hua hua<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">"cell"</span>&gt;</span>Dog<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"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"cell"</span>&gt;</span>Apple<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">"cell"</span>&gt;</span>Diadem Sifaka<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">"cell"</span>&gt;</span>Cat<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"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"cell"</span>&gt;</span>Mango<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">"cell"</span>&gt;</span>Ring-tailed<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">"cell"</span>&gt;</span>Chicken<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>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">align-items</span>: center;
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
}

<span class="hljs-selector-tag">div</span> {
    <span class="hljs-attribute">max-width</span>: <span class="hljs-number">600px</span>;
}

<span class="hljs-selector-tag">span</span> {
    <span class="hljs-attribute">display</span>: inline-block;
    <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#006100</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-selector-class">.table</span> {
   <span class="hljs-attribute">display</span>: table;
}

<span class="hljs-selector-class">.row</span> {
   <span class="hljs-attribute">display</span>: table-row;
}

<span class="hljs-selector-class">.cell</span> {
   <span class="hljs-attribute">display</span>: table-cell;
}

<span class="hljs-selector-class">.row</span>,
<span class="hljs-selector-class">.cell</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
}
</code></pre>
<p>You still get your table:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/table-with-div.png" alt="table-with-div" width="600" height="400" loading="lazy"></p>
<h2 id="heading-other-values-of-the-display-property">Other values of the Display Property</h2>
<p>Apart from <code>inline</code>, <code>block</code>, <code>none</code>, and <code>table</code>, which are really important because they significantly influence how web pages look, there are other values of the <code>display</code> property worthy of your attention.</p>
<p>Some of them you'll use all the time without really realizing that they are also part of the display property. And others you won't use often at all.</p>
<p>Let's look at some of them now.</p>
<h3 id="heading-display-flex"><code>display: flex</code></h3>
<p>A display of <code>flex</code> gives you access to the Flex layout system, which simplifies how we design and layout our web pages.</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">"child"</span>&gt;</span>
        Lorem ipsum dolor sit amet consectetur adipisicing elit.
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>This is an inline element<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> Modi eaque debitis eos quod
        labore maiores delectus asperiores voluptatem voluptas soluta!
      <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">"child"</span>&gt;</span>
        Lorem ipsum dolor sit amet consectetur adipisicing elit.
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>This is an inline element<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> Modi eaque debitis eos quod
        labore maiores delectus asperiores voluptatem voluptas soluta!
      <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>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
        <span class="hljs-attribute">display</span>: flex;
        <span class="hljs-attribute">align-items</span>: center;
        <span class="hljs-attribute">justify-content</span>: center;
        <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
}

<span class="hljs-selector-tag">span</span> {
       <span class="hljs-attribute">visibility</span>: hidden;
       <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#006100</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-selector-class">.child</span> {
       <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid crimson;
       <span class="hljs-attribute">margin</span>: <span class="hljs-number">4px</span>;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/display-flex.png" alt="display-flex" width="600" height="400" loading="lazy"></p>
<h3 id="heading-display-grid"><code>display: grid</code></h3>
<p>A display set to <code>grid</code> allows you to build layouts with the grid system, which is like an advanced form of flex.</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">"child"</span>&gt;</span>
        Lorem ipsum dolor sit amet consectetur adipisicing elit.
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>This is an inline element<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> Modi eaque debitis eos quod
        labore maiores delectus asperiores voluptatem voluptas soluta!
      <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">"child"</span>&gt;</span>
        Lorem ipsum dolor sit amet consectetur adipisicing elit.
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>This is an inline element<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> Modi eaque debitis eos quod
        labore maiores delectus asperiores voluptatem voluptas soluta!
      <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>
<pre><code class="lang-css"><span class="hljs-selector-class">.container</span> {
        <span class="hljs-attribute">display</span>: grid;
        <span class="hljs-attribute">place-items</span>: center;
        <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
      }

<span class="hljs-selector-tag">span</span> {
       <span class="hljs-attribute">visibility</span>: hidden;
       <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#006100</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-selector-class">.child</span> {
       <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid crimson;
       <span class="hljs-attribute">margin</span>: <span class="hljs-number">4px</span>;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/display-grid.png" alt="display-grid" width="600" height="400" loading="lazy"></p>
<h3 id="heading-display-inherit"><code>display: inherit</code></h3>
<p>This makes the element inherit the display property of its parent. So, if you have a <code>&lt;span&gt;</code> tag inside a div and you give the span tag a display of <code>inherit</code>, it turns it from inline to block element.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      Lorem ipsum dolor sit amet consectetur
      <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Inline element<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> adipisicing elit. Cumque cupiditate harum
      consectetur a exercitationem laboriosam nobis eos pariatur expedita iure.
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
     <span class="hljs-attribute">display</span>: flex;
     <span class="hljs-attribute">align-items</span>: center;
     <span class="hljs-attribute">justify-content</span>: center;
     <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
     <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
}

<span class="hljs-selector-tag">span</span> {
     <span class="hljs-attribute">display</span>: inherit;
     <span class="hljs-attribute">background-color</span>: crimson;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/display-inherit.png" alt="display-inherit" width="600" height="400" loading="lazy"></p>
<h3 id="heading-display-initial"><code>display: initial</code></h3>
<p>This sets the display property of an element to its default value. So, if you set the display property of a span to initial, it remains inline, and if you set the same value for a div, it remains block.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      Lorem ipsum dolor sit amet consectetur
      <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Inline element<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> adipisicing elit. Cumque cupiditate harum
      consectetur a exercitationem laboriosam nobis eos pariatur expedita iure.
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-css"><span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">align-items</span>: center;
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">2rem</span>;
}

<span class="hljs-selector-tag">span</span> {
    <span class="hljs-attribute">display</span>: initial;
    <span class="hljs-attribute">background-color</span>: crimson;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/display-initial.png" alt="display-initial" width="600" height="400" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Having a good grasp of the display property will help your page layouts look great. It also gives you a lot more control over the way you present your elements while working with CSS.</p>
<p>You can continue to come back to this article for reference too as the display property is always confusing at first until you use it enough to understand it fully.</p>
<p>I hope this article has given you the background knowledge you need in order to put the display property to good use.</p>
<p>Thank you for reading, and keep coding.</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>
