Grid vs. Flexbox
Tags: css
Published: 2021-02-20 21:52:37

##Introduction:
as we know that now a day how flex and grid become very important to design our layout before that was very messy to make our layout appear as we want by making position, float ... etc.
now grid and flex makes it too easy 
flex and grid sharing the same common features which makes it so confusing when to use flex and when to use grid and what is the difference between them.
so in this article, we will explain what is the difference between these two properties.
## Differences:
 **1- the main important difference is that: we use flex in a one-dimensional layout (row or column) while grid using in two-dimensional layout (rows and columns)**
For example :
**Flex:**
HTML:
![flex-html](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8ls5qo7z4t3cerm1w6w.png) 
CSS:
![flex-css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4v3bwtq12h4q2y2q3sq.png)
Output:
![flex-output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g128gwg7y8f7ex24q4zn.PNG) 
here in this example, we can see that flex has 2 rows but no actually its just one row but elements exceed 100% from parent width so it breaks into one another line this case only happening when we add (flex-wrap: wrap;) to the parent element 
**Grid:**
HTML:
![grid-html](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8q70qjhg3csp9obckwm.png)
CSS:
![grid-csspng](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ic9w1vkzkz2p2p3k00c.png)
Output:
![grid-outout](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ai066b8evcya6a65dhqz.PNG)
here in the grid example  what we have actually two columns and two rows so that is a two-dimensional layout 

**2-Grid is mostly defined on the parent element while flexbox most of the layout happens on the children:**
For example :
**Flex:**
HTML:
![flex-html](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbhw92gozsqw6nor5zmb.png)
CSS:
![flex-css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sim0intoyohz5nw3ua06.png)
Output:
![flex-output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aot0a8ey0zaylerr58ua.PNG)
   
**Grid:**
HTML:
![grid-html](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/czamzzta7nj75uck090o.png)
CSS:
![grid-css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8k7ffyflu8qwfn17bmft.png)
Output:
![grid-output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvcx6cd2yby9f39dkm19.PNG)

**3- Flexbox works from the content out while Grid works from the layout in:**
Flexbox works from the content out. An ideal use case for flexbox is when you have a set of items and want to space them out evenly in a container. You let the size of the content decide how much individual space each item takes up. If the items wrap onto a new line, they will work out their spacing based on their size and the available space on that line.
When you use CSS Grid Layout you create a layout and then you place items into it, or you allow the auto-placement rules to place the items into the grid cells according to that strict grid. It is possible to create tracks that respond to the size of the content, however, they will also change the entire track.

**4-Flexbox can push things away while that is not happening in Grid:**
It’s a unique feature of flexbox that you can, for example, put margin-right: auto; on an element and, if there is room, that element will push everything else as far away as it can go. while grid item if it had margin-right: auto; it can't push other items away.
For example :
HTML:
![flex-html](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2b0a7mc6oa9pn6j6xqa.png)
CSS:
![flex-css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxy9gb3gjzf2nskvpekn.png)
Output:
![flex-output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fw2zr8b8uqleqm7ik3sr.PNG)
   

### References:
* https://css-tricks.com/quick-whats-the-difference-between-flexbox-and-grid/
* https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Relationship_of_Grid_Layout
* https://www.youtube.com/watch?v=RSIclWvNTdQ
* https://www.webdesignerdepot.com/2018/09/grid-vs-flexbox-which-should-you-choose/
* https://blog.logrocket.com/flexbox-vs-css-grid/