Godaddy Shopping Cart

Flexbox Vs. Grid Layouts | GoDaddy Pro

Use circumstances for each

CSS Grid and Flexbox have been gaining recognition nowadays inside the internet growing world because of their ease of use and customizability. Each structure choices, Grid and Flexbox, permit customers to rearrange contents of their web sites with out having to hack round in CSS or JavaScript.

Nevertheless, the query looms: When would you utilize a CSS Grid module over Flexbox and vice versa? That’s the query we try to reply, together with telling you extra about their distinctive strengths!

The largest distinction between Flexbox and Grid is dimensionality. Flexbox is a one-dimensional structure system that may maintain content material in rows or columns and the content material shrinks or stretches based mostly on the world/part it’s in.


Alternatively, Grid is a two-dimensional structure system, the place content material may be displayed in rows and columns, however it has extra of a inflexible construction. To dive extra into the variations and similarities, let’s take a look at a number of examples with corresponding HTML and CSS.

Instance 1: Flexbox vs. Grid

On this instance, we prepare 5 parts, named First, Second, Third, Fourth, and Fifth, utilizing each Flexbox and Grid layouts. First, we’ve got Flexbox, and right here within the “wrapper.css” file we assign Flex properties utilizing “show: flex” and “flex: 1 1 150px,”  setting the scale of the containers to be 150px.

The property “flex-wrap: wrap”, permits the weather to wrap over to the following row as soon as the present row is full (keep in mind once more, Flexbox is a one-dimensional structure system).


Our ensuing structure ought to look one thing like this:

redo 1

As may be seen, First, Second, and Third replenish as a lot area as they will given the constraints within the code, and Fourth and Fifth wrapped over to the following row. In addition they crammed up the entire row due to the “flex-wrap” property.

However as a substitute, what if we wish Fourth and Fifth to line up beneath First and Second? That is the place Grid, the two-dimensional structure system, is available in.


Grid arranges containers in the identical means you’ll create a desk, with the identical measurement and construction. Beneath, within the corresponding HTML and CSS, “show: grid” defines the Grid structure, and the “grid-template-columns: repeat(3, 1fr)” property locations three containers in a single row after which goes to the following row.



Our ensuing structure now seems to be like:

redo 2

As promised, Grid mimics a table-like construction and positioned the primary three objects in a row and strikes to the following one in a table-like method. Grid permits us to align parts higher and in a extra structured means, as may be seen above, nevertheless it’s extra inflexible than Flexbox, since Flexbox would try to fill the empty area on the finish that may be seen above.

Instance 2: Flexbox vs. Grid

One other container association instance highlighting the distinction between Grid and Flexbox is proven beneath, the place the containers have different-sized textual content in them.

Within the first instance with Flexbox, there are additional margins added since there are not any default margins (utilizing “margin: 0.5rem”) for flex containers, together with additional padding between the containers utilizing “padding: 3rem.”



The ensuing structure seems to be like this on a wider show:

redo 3

And like this on a narrower show:

redo 4

Once more, as exhibited earlier than, we are able to see the pliability and one-dimensionality in Flexbox.

First, Flexbox makes an attempt to replenish as a lot info as potential in a single row then wraps over to the following one. Second, the container with the lengthy content material will not be the identical measurement as the opposite 5 containers.

Flexbox container sizes rely upon the content material they maintain fairly than their construction (which is the case for Grid), permitting higher flexibility in displaying content material.

How would Grid show this? Right here’s the code:



And the ensuing structure on a wider display screen:


And a narrower display screen:

redo 5

As seen earlier than in Instance 1, Grid’s structure mimics that of a desk with each container the identical measurement.

Within the instance above, some properties reminiscent of “grid-auto-columns: minmax(15rem, auto)” and “grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr))” had been used to make the Grid structure a bit extra dynamic, as may be seen within the wider display screen with field 5 being shorter, however we nonetheless can’t obtain the identical flexibility as Flexbox, although we do get a great inflexible construction.

Placing it into play

As you go about your individual journey in exploring and utilizing Flexbox and Grid layouts, there are particular essential parts and properties to bear in mind for each.

For Flexbox, within the examples above, the containers had been organized in rows since, by default, the property “flex-direction” is ready to “row” and goes proper to left. This will simply be modified by including “flex-direction: column.”

One other essential property to bear in mind is “flex-wrap”, which is ready to “nowrap” by default, the place objects will prepare themselves in particular person rows on the display screen.

Setting this to “wrap”, as within the examples above, has the objects prepare themselves in a single row as a lot as potential after which wrap over into the following one, and equally, setting this to “wrap-reverse” will wrap onto a number of rows from the underside to the highest.

One more essential property to be highlighted, given the examples above, is “justify-content.”

That is set to “flex-start” by default, the place objects are pushed to the beginning of the row and there’s some additional area, just like Grid, however setting this to “justify-content: space-evenly” fills up empty area and arranges containers accordingly.

These are just some of numerous essential properties that are useful to get a great begin at taking part in round with Flexbox and perceive its makes use of.

Equally, Grid has its personal suite of essential properties to know and begin with.

For any merchandise, the “grid-area: X, Y, span 1, span 2” property is extraordinarily essential because it specifies to start out an merchandise on row X, column Y, and span 1 row and a pair of columns.

The “grid-auto-columns” and “grid-auto-rows” properties set the default sizes of columns and rows within the grid, whereas, just like “grid-area”, the properties “grid-column: 1, span 2” and “grid-row: 1, span 2” can be utilized to make an merchandise begin on column/row 1, and span 2 columns/rows respectively.

Moreover, there may be gaps set in the entire grid construction utilizing “hole: Xpx Ypx” defining the row and column gaps. Copy the code above and take a look at these out to see how they’d have an effect on the layouts above!

Conclusion: Which one is greatest?

So, Grid or Flexbox? As proven above, there is no such thing as a clear winner; each have their very own advantages and disadvantages and provide distinctive strengths. Flexbox offers flexibility in displaying your content material that may be helpful in some circumstances, whereas Grid offers good and inflexible construction, that itself may be helpful in different circumstances.

If you wish to develop an internet site with extra dynamic content material, the place positioning of the content material will not be as essential, Flexbox could be the best way go. In any other case, if you wish to show a extra inflexible, table-like construction with content material, Grid could be your greatest wager.

And on the finish of the day, each Flexbox and Grid may be personalized with extra properties, so you may get a extra inflexible construction with Flexbox, or a extra versatile, dynamic construction with Grid.

Consider them as spoons and forks, each have their very own distinctive use circumstances, however you should utilize one instead of the opposite. This highlights the fantastic thing about CSS itself, giving us many unimaginable choices to type and customise our content material and structure in many alternative methods. Do you favor utilizing Grid or Flexbox, or each? Tell us!

Show More

Related Articles

Leave a Reply

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

Back to top button