{"componentChunkName":"component---src-components-blog-template-js","path":"/blog/2021-10-12-css-flexbox-grid/","result":{"data":{"markdownRemark":{"frontmatter":{"title":"CSS Flexbox and Grid","date":"2021-10-12"},"html":"<p>Positioning and laying out elements on a page used to be tricky. We had to use floats, inline-blocks, or even tables to get things in the right place. Now with Flexbox and Grid, CSS layout has become much more straightforward.</p>\n<h3>Flexbox</h3>\n<p>Flexbox is designed for one-dimensional layout, meaning it works in a single direction at a time, either in a row or in a column. To start using Flexbox, we set <code class=\"language-text\">display: flex</code> on the parent container.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.container {\n  display: flex;\n  justify-content: center;\n  align-items: center;\n}</code></pre></div>\n<p><code class=\"language-text\">justify-content</code> controls the alignment along the main axis (horizontal if the direction is row), and <code class=\"language-text\">align-items</code> controls the alignment along the cross axis (vertical if the direction is row).</p>\n<p>Some useful values for <code class=\"language-text\">justify-content</code> are <code class=\"language-text\">flex-start</code>, <code class=\"language-text\">flex-end</code>, <code class=\"language-text\">center</code>, <code class=\"language-text\">space-between</code>, and <code class=\"language-text\">space-around</code>. The <code class=\"language-text\">space-between</code> distributes items evenly with the first item at the start and the last item at the end. The <code class=\"language-text\">space-around</code> gives equal spacing around each item.</p>\n<p>For the children elements, we can use <code class=\"language-text\">flex-grow</code> to control how much space each child should take. For example, if we have three children and we set <code class=\"language-text\">flex-grow: 1</code> on all of them, they will share the available space equally. If we set <code class=\"language-text\">flex-grow: 2</code> on the second child, it will take twice as much space as the others.</p>\n<h3>Grid</h3>\n<p>Grid is designed for two-dimensional layout. It works with both rows and columns at the same time. To start using Grid, we set <code class=\"language-text\">display: grid</code> on the parent container.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.container {\n  display: grid;\n  grid-template-columns: 1fr 1fr 1fr;\n  grid-gap: 16px;\n}</code></pre></div>\n<p>The <code class=\"language-text\">grid-template-columns</code> defines how many columns we want and how wide each column should be. The <code class=\"language-text\">fr</code> unit stands for fraction, so <code class=\"language-text\">1fr 1fr 1fr</code> means three columns with equal width. We can also mix units like <code class=\"language-text\">200px 1fr 1fr</code> where the first column is fixed at 200px and the remaining two share the rest of the space equally.</p>\n<p>For responsive layouts, we can use <code class=\"language-text\">repeat</code> and <code class=\"language-text\">minmax</code> together:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.container {\n  display: grid;\n  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));\n  grid-gap: 16px;\n}</code></pre></div>\n<p>This will automatically create as many columns as can fit, with each column being at least 250px wide. When the screen gets smaller, the number of columns will reduce automatically.</p>\n<h3>When to Use Which</h3>\n<p>I usually use Flexbox for smaller components like navigation bars, card layouts in a single row, or centering content inside a container. Grid works better for the overall page layout or when I need to control both rows and columns at the same time, like a dashboard with multiple sections.</p>\n<p>Both of them can be combined. For example, we can use Grid for the page layout and then use Flexbox inside each grid cell to arrange the content within that cell.</p>"}},"pageContext":{"slug":"/2021-10-12-css-flexbox-grid/"}},"staticQueryHashes":[]}