top of page
Writer's pictureThe Tech Platform

Difference between Grid and Flexbox



What is Grid?

Grid is a two-dimensional grid-based layout system with rows and columns, making it easier to design web pages without having to use floats and positioning. Like tables, grid layout allow us to align elements into columns and rows.


You should consider using grid layout when:

  • You have a complex design to work with and want maintainable web pages

  • You want to add gaps over the block elements

The Grid container properties are:

  1. column-gap - Specifies the gap between the columns

  2. gap - A shorthand property for the row-gap and the column-gap properties

  3. grid - A shorthand property for the grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and the grid-auto-flow properties

  4. grid-area - Either specifies a name for the grid item, or this property is a shorthand property for the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties

  5. grid-auto-columns - Specifies a default column size

  6. grid-auto-flow - Specifies how auto-placed items are inserted in the grid

  7. grid-auto-rows - Specifies a default row size

  8. grid-column - A shorthand property for the grid-column-start and the grid-column-end properties

  9. grid-column-end - Specifies where to end the grid item

  10. grid-column-gap - Specifies the size of the gap between columns

  11. grid-column-start - Specifies where to start the grid item

  12. grid-gap - A shorthand property for the grid-row-gap and grid-column-gap properties

  13. grid-row - A shorthand property for the grid-row-start and the grid-row-end properties

  14. grid-row-end - Specifies where to end the grid item

  15. grid-row-gap - Specifies the size of the gap between rows

  16. grid-row-start - Specifies where to start the grid item

  17. grid-template - A shorthand property for the grid-template-rows, grid-template-columns and grid-areas properties

  18. grid-template-areas - Specifies how to display columns and rows, using named grid items

  19. grid-template-columns - Specifies the size of the columns, and how many columns in a grid layout

  20. grid-template-rows - Specifies the size of the rows in a grid layout

  21. row-gap - Specifies the gap between the grid rows


Example:

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
  display: grid;
  grid-template-columns: auto auto auto;
  background-color: blue;
  padding: 10px;
}
.grid-item {
  background-color: white;
  border:2px solid;
  padding: 20px;
  font-size: 30px;
  text-align: center;
}
</style>
</head>
<body>

<h1>Grid Elements</h1>

<div class="grid-container">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>  
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>  
  <div class="grid-item">7</div>
  <div class="grid-item">8</div>
  <div class="grid-item">9</div>  
</div>

</body>
</html>

Output:


Advantages:
  • Grid tracks are created within your stylesheet.

  • Reduced file sizes.

  • Prototyping with CSS Grid is fast and efficient.

Disadvantages:
  • Not supported by every browser


What is Flexbox?

Flexbox is one-dimensional layout. It is helpful in allocating and aligning the space among items in a container (made of grids). It works with all kinds of display devices and screen sizes.


You should consider using flexbox when:

  • You have a small design to work with a few rows and columns

  • You need to align the element

  • You don’t know how your content will look on the page, and you want everything to fit in.

The flex container properties are:

  1. flex-direction - Defines the direction of the container to stack the flex items.

  2. flex-wrap - Whether the flex should wrap or not?

  3. flex-flow - flex-direction + flex-wrap

  4. justify-content: used to align the flex items

  5. align-items : used to align the flex items.

  6. align-content: used to align the flex lines.


Example:

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  background-color: Blue;
}

.flex-container > div {
  background-color: white;
  margin: 10px;
  padding: 30px;
  font-size: 30px;
}
</style>
</head>
<body>

<h1>Flex Box</h1>

<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>


</body>
</html>

Output:


Advantages:
  • Flex can be arranged in any direction

  • Flex can have its visual order reversed or rearranged.

  • Items can be aligned in your container or between them.

  • Support all browsers.

Disadvantages:
  • Performance issues


Difference Between Grid and Flexbox

GRID

FLEXBOX

Dimensionality and Flexibility

Grid is 2-Dimension layout which allow flexible widths as a unit of length.

Flexbox give great control over alignment and space distribution between the items. Flexbox is 1-Dimension and delas with only columns or rows.

Alignment

It allow developers to automatically adjust the columns or rows.

Flex allow developers to align elements vertically or horizontally.

Item management

Grid support implicit and explicit content placement. The inbuild automation allows it to automatically extend the lines items and copy values into new creation from the preceding item.

Flex Container is the parent element while Flex Item represents the children. The Flex Container can ensure balanced representation by adjusting item dimensions. This allows developers to design for fluctuating screen sizes.

Dimension

2 - Dimension

1 - Dimension

Support Type

Layout First

Content First

Feature

It can flex combination of items through space-occupying features

It can push content element to extreme alignment




The Tech Platform

0 comments

Recent Posts

See All

Comments


bottom of page