visit
I was particularly intrigued that I could place an element where I needed it to be using a grid or flexbox layout. This made me begin to notice some “unwritten rules” guiding the use of either. I was motivated to make further research to learn their various use cases.
I got to notice the unending debates about the right choice of a layout. This is due to the fact that each layout comes with its pros and cons. This article attempts to make a KISS(Keep it simple stupid) analysis of each of these layouts. It also highlights the various use each can be applied to. Let's go.
1.1 Grid works well for two-dimensional layouts
Grid shines best in a two-dimensional layout. But what is a two dimensional layout? I will explain it simply this way, if you can take the components in your layout and draw a grid over them, complete with rows and columns then what you have is a two-dimensional layout.
To illustrate, say you want to do a of a web-page. is a design tear-down of a popular online magazine designed by at Microverse. So from the image below its clear that the parent container is two dimensional because it has rows and columns, i mean you can basically draw a grid over them right? Cool.Now let’s position our elements.The easiest way to align the various sections in the web-page would be to use a display grid, let’s do that. You can view the complete code snippet .
Let’s set a display grid to the main tag. This makes the sections which are direct children of the main tag to be displayed in a row..main-container {
display: grid;
grid-template-rows: 1fr 1fr;
gap: 20px;
height: 500px;
}
.latest-articles {
display: grid;
grid-template: repeat(2, 1fr)/repeat(2, 1fr);
gap: 20px;
max-width: 100%;
}
1.2 Flexbox does better in one-dimensional layouts
Flexbox looks good on a one-dimensional layout. You want to know what a one-dimensional layout is right? Alright, a one-dimensional layout is a layout in which you position items on individual rows and columns without respect to the next row or column. Continuing with our design tear-down code above, let’s go position elements on the top nav-bar.
So going ahead let’s set a display flex to the element with a class of
nav-link
..nav-link {
display: flex;
height: 90px;
text-align: center;
}
Flexbox is made for one dimensional layouts and Grid is made for two dimensional layouts. — (co-founder at )
2.1 Grid overlaps look neater
Grid easily permits the overlapping of items. Thus we have the leeway to place items across grid lines or even within the same grid cell. Here's an image below, looks quite neat right.2.2 Flex items overlaps are less neat
Overlap of flex items can be achieved but not without its cons. To overlap flex-items, one would have to use negative margins or absolute positioning. Which invariably removes items from the flex layout. Not too cool i presume.3.1 Flexbox quite complicating
Flexbox could get a tidbit complicated with the use of flex-basis, flex-grow, flex-shrink. One might also contend with setting width, min-width, max-width to flex-items. There is also the need to between flex items (this can be quite solved using ).The use of various properties to define flex items might not be an issue in some use cases. In some other cases, it could result in a lengthy piece of code that could become a debugging nightmare.3.2 Grid: A less complicating layout
Grid allows us to use grid lines to fix items right into their place. This prevents the occurrence of gutters by using space-occupying features likeGitHub -
LinkedIn -Twitter -