Deepanshu Bisht

Frontend Developer Software Developer || Fascinated by tech trends || Building usable systems that work on web and mobile.

Flexbox and Grid

The manner in which you show your content can say a ton regarding you or your organization. Having pleasant content and realizing how to show your content appropriately in a perfect manner that your client can comprehend is truly significant. 

So how would we pick how we will show our content? The obligation of showing our content in a pretty and good manner is through our CSS code. 

CSS has consistently been perhaps the main piece of web advancement, we’ve been assembling and working on better approaches to work with it since the start of programs. A couple of years prior, making site page formats was exceptionally difficult to work. We were utilizing some CSS properties like a buoy, situating a great deal of components with position, and inline-block styling. 

Presently, we don’t rely upon those properties any longer to make our website pages, it’s getting simpler now to make various encounters in our site pages. The advancement of the web has gone to a point that now we have two CSS design frameworks that we can work with; Flexbox and CSS network. 

Contemplating which format framework is best for your venture early can truly assist you with accomplishing a superior outcome and elegantly composed CSS code. In this post, we will see when to utilize Flexbox and when to utilize CSS lattice. It’s anything but an exceptionally hard inquiry to reply yet it can save you eventually with upkeep and refactoring. 

To begin, we need to initially comprehend their motivations and how they work in the engine. How about we start with Flexbox. 

Flexbox:

Flexbox was presented in 2009 as another design framework, with the objective to assist us with building responsive website pages and put together our components effectively, and from that point forward, it’s acquired more consideration. It turns out it’s currently utilized as the principal format framework for present-day site pages. 

Flexbox is a one-dimensional design framework that we can use to make a line or a section pivot format. It makes our life simpler to plan and assemble responsive pages without utilizing interesting hacks and a great deal of buoy and position properties in our CSS code. 

To begin to utilize Flexbox, you should simply make a flex holder utilizing the display: flex property. From that point forward, each component that you have inside that flex compartment transforms into a flex thing.

The fundamental heading that our flex things take in our flex holder is a row.

main direction that our flex items take in our flex container is a row

We can adjust the bearing of our flex things in our flex compartment effectively to a segment, bypassing a flex-direction: column property to our flex holder.

flex container in a column

Flexbox has a great deal of different properties that we can use to make magnificent things. We can arrange the components in the manner in which we need, we can switch the request for components, we can decide whether our components ought to develop or shrivel, and so forth.

For instance, how about we envision that we have a div component and inside that div, we have three components, with a similar width and height:

<div id="container">
  <div id="one">1</div>
  <div id="two">2</div>
  <div id="three">3</div>
</div>

In our CSS, we’re using Flexbox to order and align our elements:

#container {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-evenly;
  padding: 10px;
}

#one,
#two, 
#three {
  width: 200px;
  height: 100%;
  background: red;
}

We can undoubtedly invert the request for a component utilizing the order property. Assuming we needed to change the request for the component with an id of two, we would do this:

#two {
  order: 3;
}

Presently our component is the final remaining one in the flex holder. These are a portion of the provisions that Flexbox is acquainted with us and they’re extremely valuable for the style and arrangement of components. 

Since we realize that Flexbox is a one-dimensional design framework, how about we see momentarily how CSS lattice functions and contrasts between these format frameworks.

CSS grid:

In case Flexbox is an incredible design framework since it’s a one-dimensional framework (implying that we can work with lines or sections) CSS Grid is viewed as presenting the most remarkable format framework accessible. 

CSS network is a two-dimensional format framework, we can work with lines and sections together, which implies that it opens many prospects to construct more intricate and coordinated plan frameworks, without falling back to a few “hacky ways” that we were utilizing before. 

To characterize a framework compartment, you should simply pass a display: grid property to your square component. Presently you have a network, so you ought to characterize the number of lines and segments you need. 

To make columns you utilize the grid-template-rows property, and pass the number of you need, similar to this: 

grid-template-rows: 200px 200px;

To make segments it’s practically the equivalent, we utilize the lattice layout segments property: 

grid-template-columns: 200px 200px;

Yet, on the off chance that you definitely realize how to utilize both of these format frameworks, you may have been pondering ‘which one should I use?’, we will investigate this next. 

Since we have perceived how they work, we’ll accentuate the distinctions and best use cases for everyone. 

CSS grid is for layout, flexbox is for alignment 

Back when Flexbox was delivered, we believed that it very well may be the best format framework to use to assemble our pages, however, it wasn’t. 

Flexbox assisted engineers with beginning to make more responsive and viable web applications, however, the fundamental thought of a one-dimensional format framework doesn’t bode well when you need to construct a more mind-boggling format plan. 

CSS network truly came to assist us with building more perplexing format plans utilizing a two-dimensional way, utilizing the two lines and segments. We should plan to utilize the two of them together, however, for various purposes. For your format, use CSS network, for the arrangement of your components, use Flexbox. 

To dominate and know precisely when you will need and how to utilize a CSS network, you should initially become familiar with the fundamentals and how Flexbox functions since when you need an arrangement of components in your application, it’s Flexbox that you will utilize. 

You ought to consider utilizing Flexbox when: 

  • You have a little plan to execute ?—? Flexbox is ideal when you have a little format configuration to carry out, with a couple of lines or a couple of segments 
  • You need to adjust components ?—? Flexbox is ideally suited for that, the solitary thing we ought to do is make a flex holder utilizing show: flex and afterward characterize the flex-bearing that we need 
  • You need a substance first design? — Flexbox is the best format framework to make site pages on the off chance that you don’t realize precisely how your substance will look, so assuming you need all that just to fit in, Flexbox is ideally suited for that. 

Obviously, you can construct your entire application utilizing just Flexbox and get a similar outcome as though you were working with a CSS network, that is absolutely fine. Yet, for a superior CSS approach, to have a more succinct, elegantly composed, and viable application in the long haul, to make and accommodate your design consummately, the ideal technique is to utilize CSS lattice. 

CSS grid is better when: 

  • You have an unpredictable plan to carry out ?—? in some utilization cases, we have complex plans to execute, and that is the point at which the enchantment of CSS network shows itself. The two-dimensional format framework here is wonderful to make a perplexing plan, we can utilize it in support of ourselves to make more intricate and viable website pages 
  • You need to have a hole between block components ?—? something else that is extremely useful in CSS matrix, that we don’t have in Flexbox, is the hole property. We can characterize the hole between our lines or segments effectively, without utilizing the edge property, which can cause some incidental effects particularly in case we’re working with numerous breakpoints 
  • You need to cover components ?—? crossover components utilizing CSS matrix is exceptionally simple, you simply need to utilize the lattice section and framework line properties and you can have covering components without any problem. Then again, with Flexbox we actually need to utilize a few hacks like edges, changes, or supreme situating 
  • You need a format first design? — when you as of now have your design configuration structure, it’s simpler to work with CSS network, and the two-dimensional design framework helps us a ton when we’re ready to utilize lines and segments together, and position the components the manner in which we need 

Before you choose which one you should utilize, remember:

CSS grid is for layout, Flexbox is for alignment

Here is an illustration of the right utilization of the CSS grid, we should envision that we will assemble a straightforward application, and the barebones of our applications will resemble this:

correct use of css including header,main,aside, and footer

We have a header, an aside menu, a primary square substance, and a footer. To make this design utilizing CSS framework, we simply need to make our components:

<div id="container">
  <header>Header</header>
  <aside>Aside</aside>
  <main>Main</main>
  <footer>Footer</footer>
</div>

And now create our grid container using display: grid and then create some rows and columns, like this:

#container {
  width: 100%;
  height: 100vh;
  background: yellow;
  display: grid;
  grid-template-rows: 80px 600px 80px;
  grid-template-columns: 0.5fr 1fr;
}

header {
  grid-column: 1 / 3;
  grid-row: 1 / 2;
  background: blue;
}

aside {
  grid-column: 1 / 2;

That is it. Utilizing just 30 lines of CSS we make our plan essentially, without utilizing hacks like float or situating our components, and without making many flex compartments. 

The best choice that you can take for your application to make an extremely nice and all-around constructed web design for your application is to utilize both together. 

How about we take our model and utilize both CSS network and Flexbox to show the force of both format frameworks together. Inside our header, we will make three div components, and we will adjust them in succession. 

To do that, we should simply pronounce our header a flex holder utilizing the display: flex property, make the flex-bearing a column utilizing flex-direction: row and adjust the things.

header {
  grid-column: 1 / 3;
  grid-row: 1 / 2;
  background: blue;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-evenly;
  padding: 10px;
}

An incredible page, utilizing CSS grid for the format, and Flexbox for arrangement. 

For a significant design style, you could utilize the CSS matrix, since it’s a two-dimensional format framework, you can work with the two lines and segments without any problem. Furthermore, for a more straightforward format style, you can utilize Flexbox, a one-dimensional framework, it’s extremely useful to work with lines. 

Conclusion:

In this article, we found out about the contrasts between Flexbox and CSS lattice, how they work in current programs, and how we can utilize every last one of them to achieve various outcomes in our CSS. 

Reference:

https://blog.logrocket.com/flexbox-vs-css-grid/
Share