12 CSS Flexbox tips & tricks which you need to know as a web developer
Tags: css, programming, webdev, beginners
Published: 2019-11-01 15:31:32

---
canonical_url: https://www.blog.duomly.com/flexbox-cheatsheet/
---

This article was originally published at: https://www.blog.duomly.com/flexbox-cheatsheet/

Probably almost everybody in the front-end industry heard something about the flexbox and benefits which it can give to us when we need to design a grid. In this article, I would like to explain what flexbox is and how we can use it to save a lot of our time.

Flexbox is a CSS layout module that allows us to setup nice-behaving and responsive grid in a speedy and friendly way. Let’s see a few most critical elements of flexbox which we can use in our daily work.

<h3>1. How to align content to left</h3>
Very often, we would like to set up our content on the one side of the page, and it’s a bit similar to the float left, but not 100% the same.  We can align our elements to the left by specifying a container’s property flex-direction: row.

{% codepen https://codepen.io/claireparker/pen/abbLgdy %}

<h3>2. How to align content to the right</h3>
As in the previous example, we align content to the one side. This time we align content to the right side of the container.

We can align our elements to the right by specifying a container’s property flex-direction: row-reverse.

{% codepen https://codepen.io/claireparker/pen/gOOGVWM %}

<h3>3. How to align content to center horizontally</h3>
What I use very often, and it’s much more challenging to set up without flex, is centering content to the container.

If we would like to do center our elements in the horizontal position, we need to add property justify-content: center to our container.

{% codepen https://codepen.io/claireparker/pen/OJJxKgb %}

<h3>4. How to justify-content</h3>
If we would like to justify our elements, we need to add a similar property like in the previous example, but instead of the center we are using method space-between, so our property should look like justify-content: space-between

{% codepen https://codepen.io/claireparker/pen/YzzrmoG %}

<h3>5. How to setup element one per line</h3>
If we would like to set up elements like in the vertical column, we can do it as well.

To set up a column, we need to specify our container with flex-direction: column.

{% codepen https://codepen.io/claireparker/pen/rNNGXXg %}

<h3>6. What is flex-grow</h3>
Flex-grow is a property that specifies how much space our elements should take.

By default, flex-grow is set up as 1and it means our element takes all space which belongs to him (for example, if we have 4 elements, one element takes 25% of space). But we can specify targeted elements with a flex-grow: xx (where xx is number), and if we will, for example, set up only one element as flex-grow: 2, that one element takes two times more space than rest of our elements.

{% codepen https://codepen.io/claireparker/pen/xxxPKKb %}

<h3>7. How to setup elements on top of the container (align-items:flex-start)</h3>
If we would like to set up all our child elements on the top of the container, we can easily do it by adding align-items:flex-start to the container.

{% codepen https://codepen.io/claireparker/pen/yLLPBNZ %}

<h3>8. How to setup elements on the bottom of the container (align-items:flex-end)</h3>
Opposite to the previous example now, we specify items on the bottom of the container, to that we need to add a similar property, but instead of start, we use end.

Our property should be align-items:flex-end then.

{% codepen https://codepen.io/claireparker/pen/wvvPwMm %}

<h3>9. How to vertically and horizontally align-center elements (align-items: center)</h3>
What is much more useful and without a grid, very often is done by position absolute, and translates, now we can do very simple. To center vertically and horizontally the elements, we need to specify two properties in the container. First is align-items: center and second is justify-content: center.

{% codepen https://codepen.io/claireparker/pen/OJJOLRQ %}

<h3>10. How to fulfill the whole container’s height with items (stretch)</h3>
Sometimes we would like to have items that have a height of 100% of our container’s height. We can do it in a few ways, but now I would like to show you one with a flexbox. To do that, you need only set up our container’s property as align-items: stretch. Voila!

{% codepen https://codepen.io/claireparker/pen/oNNpvQB %}

<h3>11. How to set up the width of an element</h3>
What if we would like to set up a few elements width related to the number of elements?

We can use flex for that and do it super quickly! To do that, we can set up all items as flex: 1, and elements which we would like to be, for example, 2 times bigger than rest as flex: 2.

{% codepen https://codepen.io/claireparker/pen/jOOYNJJ %}

<h3>12. How to set up a grid</h3>
With flexbox, we can set up a nice grid in a few moments. In this example, we do set up a standard blog grid, with menu, blog post, sidebar, and footer.

To do this, we do start from setup flex-flow: row wrap in our container. Next, we need to set up all of our elements as flex: 1 100% what set up all of the elements as 100% width. Now we can go into a body of our grid, and set up post length by setting flex: 3 0; and sidebar length by flex: 1 0; what means our sidebar length should be 3 times bigger than sidebar length. That’s all! We have our simple grid created!

{% codepen https://codepen.io/claireparker/pen/KKKZPbY %}

<h3>Conclusion</h3>
I hope now you know the basics of the flexbox, and you should be able to use them in your daily work. Flexbox is much more productive, and you can find it much more in-depth, but I wanted to tell you about a few methods which I use in my daily work and what gives me much higher productivity and saves my time a lot.

 

 Thanks for reading!

<a href="https://www.duomly.com">![Duomly - programming online courses](https://thepracticaldev.s3.amazonaws.com/i/d98wn3rjl3w5oinz3nvh.png)</a>