<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ruta Tang</title>
    <description>The latest articles on DEV Community by Ruta Tang (@rutatang).</description>
    <link>https://dev.to/rutatang</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F789705%2Fb00a0021-648d-48f6-9ce8-df53c1096d63.jpeg</url>
      <title>DEV Community: Ruta Tang</title>
      <link>https://dev.to/rutatang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rutatang"/>
    <language>en</language>
    <item>
      <title>A Complete Guide to Flexbox Layout</title>
      <dc:creator>Ruta Tang</dc:creator>
      <pubDate>Wed, 08 Mar 2023 06:53:36 +0000</pubDate>
      <link>https://dev.to/rutatang/a-complete-guide-to-flexbox-layout-4jh2</link>
      <guid>https://dev.to/rutatang/a-complete-guide-to-flexbox-layout-4jh2</guid>
      <description>&lt;p&gt;The Flexbox Layout, also referred to as the flexible box layout, is an incredibly versatile approach to positioning, sizing, and directing items within a container. As the name suggests, it offers a flexible way to arrange elements in a layout that can easily adapt to different screen sizes and resolutions.&lt;/p&gt;

&lt;p&gt;It is important to note that, unlike the Grid Layout, which is a two-dimensional layout, the Flexbox Layout focuses on one-dimensional layouts. This means that it can control the layout either in a row or column at a time, but not both simultaneously. However, with its ease of use and powerful capabilities, it is a highly valuable tool in web design.&lt;/p&gt;

&lt;p&gt;Rest assured, we will delve further into this topic and explore the many benefits and usages of Flexbox Layout in the upcoming discussion.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Concept
&lt;/h2&gt;

&lt;p&gt;Here is the graph describing the terms of Flexbox layout from &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox#the_flex_model"&gt;MDN&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y3HtYCca--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70mn001xq2a5w2lysvhy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y3HtYCca--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70mn001xq2a5w2lysvhy.png" alt="Flexbox Concept" width="563" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's explain the terms one by one:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Flex Container&lt;/strong&gt;: You should use &lt;code&gt;display: flex&lt;/code&gt; on the container element to declare that the container element will use Flexbox layout.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flex Items&lt;/strong&gt;: Any &lt;strong&gt;direct children&lt;/strong&gt; of the Flex container will become the flex items.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Main Axis&lt;/strong&gt;: Main axis is dependent on the direction of flex, by default it is horizontal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross Axis&lt;/strong&gt;: Cross axis is dependent on the direction of flex, by default it is vertical.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Main Start/End, Cross Start/End&lt;/strong&gt;: They are simply the start or end of the main or cross axis.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Related Properties
&lt;/h2&gt;

&lt;p&gt;Here are almost all the related properties to the flexbox layout, we will explain how they work later.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Flex container related properties&lt;/strong&gt;: &lt;code&gt;flex-direction&lt;/code&gt;, &lt;code&gt;flex-wrap&lt;/code&gt;, &lt;code&gt;flex-flow&lt;/code&gt;, &lt;code&gt;justify-content&lt;/code&gt;, &lt;code&gt;align-content&lt;/code&gt;, &lt;code&gt;align-items&lt;/code&gt;, &lt;code&gt;place-content&lt;/code&gt;, &lt;code&gt;place-items&lt;/code&gt;, &lt;code&gt;column-gap&lt;/code&gt;, &lt;code&gt;row-gap&lt;/code&gt;, &lt;code&gt;gap&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flex items related properties&lt;/strong&gt;: &lt;code&gt;flex&lt;/code&gt;, &lt;code&gt;flex-basis&lt;/code&gt;, &lt;code&gt;flex-grow&lt;/code&gt;, &lt;code&gt;flex-shrink&lt;/code&gt;, &lt;code&gt;order&lt;/code&gt;, &lt;code&gt;align-self&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  A Quick Example
&lt;/h2&gt;

&lt;p&gt;Here is a quick example of how we may use some of the properties. You can click the &lt;em&gt;index.css&lt;/em&gt; to say how we use those properties.&lt;/p&gt;

&lt;p&gt;Hope the example may give a brief idea about how flexbox works. And now let's talk about how each property works and may affect the layout in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Declare Flexbox Layout
&lt;/h2&gt;

&lt;p&gt;There are two options to declare an element as the flexbox layout. The one we always use is &lt;code&gt;display: flex&lt;/code&gt; which asks an element (container) to use the flexbox layout in which the element (container) itself is considered a block element. Another one is &lt;code&gt;display: inline-flex&lt;/code&gt; which is just similar to the first one but the element (container) itself is the inline block.&lt;/p&gt;

&lt;p&gt;Thus, only two ways to declare flexbox layout you should remember: &lt;code&gt;display: flex&lt;/code&gt; and &lt;code&gt;display: inline-flex&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flexbox Direction
&lt;/h2&gt;

&lt;p&gt;By default, &lt;code&gt;display: flex&lt;/code&gt; will make all the container's direct children lay along the horizontal line. But, you can also make children lay vertically by explicitly applying &lt;code&gt;flex-direction: column&lt;/code&gt;. Barring aligning children horizontally or vertically, you can also reverse the children's order by applying &lt;code&gt;flex-direction: row-reverse&lt;/code&gt; or &lt;code&gt;flex-direction: column-reverse&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap
&lt;/h2&gt;

&lt;p&gt;By default, the children of flex container will condense as more children are pushed. When the width of all children exceed the width of the container, the content will overflow. Usually, at this time, you will want to break the lines and put overflowed children to the next line. Thus, &lt;code&gt;flex-wrap: wrap&lt;/code&gt; which will be putted on container can help you break the overflowed children to the next line. And one thing worthy to be noted is that you can almost consider each line as a flex container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alignment
&lt;/h2&gt;

&lt;p&gt;Within Flexbox layout, you can align children along main axis or cross axis, align a certain specific item, or align multiple axis lines. Let's dicusse them one by one in detail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Main Axis Items Alignment
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;justify-content&lt;/code&gt; property defines how the browser distributes space between and around content items along the &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Main_Axis"&gt;main-axis&lt;/a&gt; of a flex container.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can use &lt;code&gt;justify-content&lt;/code&gt; property to align direct children of the container along &lt;strong&gt;main axis&lt;/strong&gt;. There are several values we can use for this property to align the content:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;center&lt;/code&gt;: children will be packed and aligned in the &lt;strong&gt;center&lt;/strong&gt; along the main axis.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;flex-start&lt;/code&gt; and &lt;code&gt;flex-end&lt;/code&gt;:  children will be packed and aligned starting from the start or end of the main axis.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;space-betweeen&lt;/code&gt;: spaces between children will be distributed evenly while the first item is at the edge of the start of the main axis and the last item at the edge of the end of the main axis.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;space-around&lt;/code&gt;: similar to &lt;code&gt;space-between&lt;/code&gt; but the space between the edge of the start and the first item and between the edge of the end and the last item is half space of the space between each item.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;space-evenly&lt;/code&gt;: similar to &lt;code&gt;space-around&lt;/code&gt; but all the space including the space between items and edges and between items and items are the same.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Cross Axis Items Alignment
&lt;/h3&gt;

&lt;p&gt;We can use &lt;code&gt;align-items&lt;/code&gt; property to align the direct children of the container along the &lt;strong&gt;cross-axis&lt;/strong&gt;. There are several values we can use for this property to align the content:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;code&gt;stretch&lt;/code&gt;: This is the default value. Direct children of the container will be stretched to fit the container’s cross-axis size.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;center&lt;/code&gt;: Direct children of the container will be aligned at the center along the cross-axis.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;flex-start&lt;/code&gt;: Direct children of the container will be aligned at the start of the cross-axis.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;flex-end&lt;/code&gt;: Direct children of the container will be aligned at the end of the cross-axis.&lt;/li&gt;
&lt;li&gt; &lt;code&gt;baseline&lt;/code&gt;: Direct children of the container will be aligned at the baseline of the cross-axis, in which baseline means the first text line position.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Item's Self Alignment
&lt;/h3&gt;

&lt;p&gt;We can use &lt;code&gt;align-self&lt;/code&gt; property on a certain direct child of the container to align that specific child’s position along cross-axis. The values of this property are almost the same as the values of &lt;code&gt;align-items&lt;/code&gt; property with the difference that the &lt;code&gt;align-self&lt;/code&gt; only applies to a certain direct child.&lt;/p&gt;

&lt;h3&gt;
  
  
  Align Multiple Main Axis
&lt;/h3&gt;

&lt;p&gt;When you apply &lt;code&gt;flex-wrap: wrap&lt;/code&gt; on the container, there might be multiple lines. As we mentioned that you can consider each line as a flexbox container, when we want to align how these lines are aligned along the cross-axis, we can use &lt;code&gt;align-content&lt;/code&gt;. The values of &lt;code&gt;align-content&lt;/code&gt; are almost the same as &lt;code&gt;justify-content&lt;/code&gt; but &lt;code&gt;align-content&lt;/code&gt; apply to how lines are aligned along the cross-axis.&lt;/p&gt;

&lt;h2&gt;
  
  
  Item Size
&lt;/h2&gt;

&lt;p&gt;There are three properties associated with direct child size:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;flex-basis&lt;/code&gt;: initial item size
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;flex-grow&lt;/code&gt;: how to handle the growth of the direct child size
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;flex-shrink&lt;/code&gt;: opposite to &lt;code&gt;flex-grow&lt;/code&gt;, how to handle the shrinkness of the direct child size&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Initial Size
&lt;/h3&gt;

&lt;p&gt;Apply &lt;code&gt;flex-basis&lt;/code&gt; on a certain direct child to set its initial size and there are several values we can use:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;max-content&lt;/code&gt;, &lt;code&gt;min-content&lt;/code&gt;, &lt;code&gt;content&lt;/code&gt;: size will be determined by the content of the item&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;'width'&amp;gt;&lt;/code&gt;: set specific size, like &lt;code&gt;10em&lt;/code&gt;, &lt;code&gt;100px&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;auto&lt;/code&gt;: if item has width or height property, the size is determined by its width or height, or it will fallback to &lt;code&gt;content&lt;/code&gt; value&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Growness
&lt;/h3&gt;

&lt;p&gt;Apply &lt;code&gt;flex-grow&lt;/code&gt; on a certain direct child to control how the item is grown. 0 is not to grow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shrinkness
&lt;/h3&gt;

&lt;p&gt;Apply &lt;code&gt;flex-shrink&lt;/code&gt; on a certain direct child to control how the item is shrunk. 0 is not to shrink.&lt;/p&gt;

&lt;h2&gt;
  
  
  Item Order
&lt;/h2&gt;

&lt;p&gt;We can use &lt;code&gt;order&lt;/code&gt; property to change the order of a certain direct child of the container. The value of the &lt;code&gt;order&lt;/code&gt; property is basically a number, the smaller the more forward, and the bigger the more latter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gap
&lt;/h2&gt;

&lt;p&gt;For the gap, we have three associated properties, &lt;code&gt;gap&lt;/code&gt;, &lt;code&gt;row-gap&lt;/code&gt;, and &lt;code&gt;column-gap&lt;/code&gt;. The &lt;code&gt;gap&lt;/code&gt; is the shorthand for &lt;code&gt;row-gap&lt;/code&gt; and &lt;code&gt;column-gap&lt;/code&gt;. &lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox"&gt;Flexbox&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout"&gt;CSS Flexible Box Layout&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
  </channel>
</rss>
