September 19, 2020

CSS flex vs grid: which is better?

One of the most important things while designing a web-page is to make sure that its design pulls the visitors or customers towards it. The design should be dynamic, catchy and attractive. This whole science of designing and formatting designing web pages requires the use of markup languages.

Markup languages are used in the process to add additional information to the document which is also known as the “process of annotation”. 

One can say that HTML defines content and structure of the page whereas CSS specifies looks of the page i.e. its color, style, formatting and layout.

History of CSS

The first version of CSS was first released in 1996 after the endorsement of the World Wide Web Consortium. Different CSS variants came forward with time and they include CSS1, which was the very first version, CSS 2, CSS 2.1, CSS 3 and CSS 4.

CSS flexbox vs CSS grid

CSS Flexible Box Layout, which is simply known as CSS Flexbox is the version of CSS 3 variant of CSS. CSS flexbox is designed from providing layouts in only one dimension, and they can be either row or column. 

CSS Grid Layout or CSS grid allows the users to design webpages in more asymmetrical and complex ways. Compared to the flexbox model, grid provides a two-dimensional model, both in rows and columns at the same time.

One dimensional layout vs two dimensional layout

As discussed above, CSS flexbox is created for one-dimensional layout whereas CSS grid provides a two-dimensional layout. 

In order to consider the difference, let’s say you are using Flexbox for designing your webpage and you want to keep things simple. Using flexbox will allow you to layout your items in only one direction which can be horizontal or vertical directions (or in other words, either in rows or in columns).

Now consider a situation where you have to develop a web page that needs to be versatile and highly customizable. That is where Grid comes to help. Using CSS Grid in these situations will allow you to place your items both in horizontal and vertical directions (i.e. in both rows and in columns).

Content flow: flexbox vs grid

The thing about flex is that its main focus is on content’s flow rather than its correct placement. As its name indicates, it can easily flex itself according to the content of the item. If the content is more, it will expand and if the content is less, it will contract. In simpler terms. It allows you to make use of the available space flexibly.This ease does not come up with CSS grid  

Browser Support

As discussed earlier, it is important for the web page to be designed in such a way that it attracts its users. And to achieve this milestone, it is really necessary that the program must get efficient support from the browser. CSS Flexbox is generally well-supported by all browsers almost 96% of global scope whereas CSS Grid is not supported by all browsers such as Internet Explorer 11 and Microsoft Edge 15 and CSS grid have coverage of 87% of browsers

Content vs layout

That’s where another major difference shows up. If you want a simple layout, or if you want to make your content in-charge of all the places it takes on a row-by-row or column-by-column format, you go for flexbox. But if you want a grid to control the placement of your items or if you want to control the positioning of your items by using line-based positioning or grid template areas, you go for grid.

Gap property

Gap property defines the size of gap between rows and columns. This gap between adjacent rows and columns can be easily made in CSS Grid but doing so in CSS Flexbox requires multiple actions to be taken, such as use of flex items or padding and nested containers.

Wrapping Method

Wrapping method is also different between CSS Flexbox and CSS Grid. As Flexbox is one-dimensional, wrapping items may result in losing the context of the previous row or column. In CSS Grid however, items don’t lose their context as it is two-dimensional and they remain part of the defined grid as usual.

Content based vs Container based

Flexbox works on the basis of content where size of the cell is defined within the flex item itself. For example, look at this row-element styled flexbox:

.parent {
  margin: 20px auto;
  display: flex;
  flex-flow: row wrap;
}

.child {
  min-height: 100px;
  background: green;
  border: 5px solid white;
  flex: 1 1 auto;
  line-height: 100px;
  text-align: center;
}

Looking at these examples, you may notice that the size of items is not defined.

But working with the CSS Grid, you have to define the layout first as it is container based. In Grid, Calculating of layout is calculated regardless of the content put inside the containers. Look at the following for example

parent {  
  margin: 20px auto;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
}

.child {
  min-height: 100px
background: orange;
  border: 5px solid white;
  line-height: 100px;
  text-align: center;
}

Conclusion

From the above text, it can be concluded that both CSS formats have their pros as well as cons. One cannot simply argue about which platform is “better” than the other. Instead, what you should look for is what suits you and your website the best. Also, you cannot limit yourself to use of just one layout. Instead, you can easily use both of them to make the interface of your webpage catchier. Hence, no one should argue over which interface is better than the other instead he or she should go for what serves better his or her interests. 

Leave a Reply

Your email address will not be published. Required fields are marked *