DEV Community

Cover image for Closing the Gap: Simplify Your Web Layouts with the CSS Gap Property
Yoav Ganbar for Builder.io

Posted on • Originally published at builder.io on

Closing the Gap: Simplify Your Web Layouts with the CSS Gap Property

As a web developer, you're always looking for ways to improve your code and make your layouts more efficient.

One such improvement is the CSS Gap Property, which can simplify your web layouts and make them more visually appealing. Let’s explore the benefits of using the CSS Gap Property instead of margins, and how it can make your life as a developer easier.

Margin is spacing

Like a lot of things in software development, there are opposing opinions and camps for certain mehods and preferences. One of these cases is the use of CSS margin, either using only margin-top or using margin-bottom.

Nowadays, it’s better to use CSS logical properties and values, but the question of which strategy to use is only an opinion.

Go with the flow

As many of you may know, the natural flow (or normal flow) of the DOM is top to bottom. That is to say, any block element will be placed below the preceding block and any inline block will be inline.

Check out the following example to better understand this concept:

That is why a lot of front end developers might prefer using margin-bottom as it feels more natural.

The CSS gap property: an overview

The CSS Gap Property provides several benefits to web developers looking to create visually appealing and organized web layouts. It simplifies layouts, improves readability, enables responsive design, reduces code complexity, and improves browser performance. The Gap Property defines the size of the gap between the rows and columns in flexbox, grid, or multi-column layouts. It is a shorthand for the row-gap and column-gap properties.

Benefits of using gap instead of margin

Using the CSS gap property instead of margins has several advantages:

  1. Simplifies layouts : Previously, developers had to use margin or padding to create space between elements, which could be time-consuming and result in inconsistent layouts. The CSS Gap Property eliminates this problem by providing a simple and efficient way to add space between elements without affecting their dimensions.
  2. Easier responsive design : Gap makes it easier to handle spacing in responsive designs without the need for complex calculations or additional wrapper elements.
  3. Reduced code complexity : Instead of using multiple CSS properties to create space between elements, you can use the CSS gap property to achieve the same effect with a single line of code.
  4. Better browser performance : The CSS gap property can improve the performance of your website by reducing the amount of code required to create space between elements. This can result in faster load times and better overall performance.
  5. No need for negative margins or pseudo selectors : Using the gap property doesn't require thinking about negative margins or using pseudo selectors to remove first or last element margins.

Steve explains this quite well in this video:

Gap versus margin: a visual comparison

To better illustrate the differences between using the CSS gap property and margins, let's consider a minimal example with a flexbox layout:

Using Margin:

<section class="margin-example">
  <div>Box 1</div>
  <div>Box 2</div>
  <div>Box 3</div>
  <div>Box 4</div>
</section>
Enter fullscreen mode Exit fullscreen mode
.margin-example {
 display: flex;
 flex-wrap: wrap;
}

.margin-example div {
 width: 200px;
 margin: 0 10px;
 background: rebeccapurple;
 color: white;
}

.margin-example div:first-child,
.margin-example div:last-child {
 margin: 0;
}
Enter fullscreen mode Exit fullscreen mode

Here’s the result:

Depending on what device you are viewing this from, this might look broken to you, and you would be right. The corresponding margin code from above is only taking into account desktop sizes, and is adding (margin) spacing to the left.

As a result, the mobile display is broken, as can be inferred from the image below:

screenshot of bad margin spacing.

How can we fix this? We could add a media query to handle what happens on mobile sizes. The naive approach would be to just switch up the margin-left & margin-right to margin-top & margin-bottom values:

@media only screen and (max-width: 425px) {
 /* CSS styles for mobile devices */
 .margin-example div {
  margin: 10px 0;
 }
}
Enter fullscreen mode Exit fullscreen mode

The result:

That still isn’t right, is it?

It’s also clunky in different screen sizes when we resize the viewport window as can be seen below:

When we get to our media query breakpoint things go awry. Suddenly, boxes change sizes, and when we do get to more of our mobile screen size our second and third box get more space between them than we might have expected.

We’d need to do more work to make the responsiveness right.

 Same example using gap:

<section class="gap-example">
  <div>Box 1</div>
  <div>Box 2</div>
  <div>Box 3</div>
  <div>Box 4</div>
</section>
Enter fullscreen mode Exit fullscreen mode
.gap-example {
 display: flex;
 flex-wrap: wrap;
 gap: 20px;
}

.gap-example div {
 width: 200px;
 background: rebeccapurple;
 color: white;
}
Enter fullscreen mode Exit fullscreen mode

First off, using the CSS gap property results in cleaner and more concise code compared to using margins.

What about responsiveness? Have a look:

Responsive without hassle!

Embracing the Gap: A Practical Example

Let's say you have a grid layout with multiple elements:

<section>
  <div>Box 1</div>
  <div>Box 2</div>
  <div>Box 3</div>
  <div>Box 4</div>
</section>
Enter fullscreen mode Exit fullscreen mode

To create a responsive design and add space between the elements, you can use the CSS gap property:

section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

Enter fullscreen mode Exit fullscreen mode

This will create a 20px gap between the rows and columns, making the layout more visually appealing and easier to manage:

Conclusion

The CSS gap property is a powerful tool that can simplify your web layouts and make them more visually appealing. By using the gap property instead of margins, you can reduce code complexity, improve browser performance, and create more consistent and responsive designs. So, the next time you're working on a web layout, consider using the CSS Gap Property to make your life as a developer easier and your layouts more efficient.

Visually build with your components

Builder.io is a headless CMS that lets you drag and drop with your components right within your existing site.

Try it out Learn more

// Dynamically render your components
export function MyPage({ json }) {
  return <BuilderComponent content={json} />
}

registerComponents([MyHero, MyProducts])
Enter fullscreen mode Exit fullscreen mode
Read the full post on the Builder.io blog

Top comments (1)

Collapse
 
pavellaptev profile image
Pavel Laptev

Cool article! Using gap for a while, and it's a great margin replacement.