CSS gap is NOT a replacement for margins

CSS gap is NOT a replacement for margins

The Problem with Overusing Gap in CSS

In this video, the speaker discusses the trend of overusing gap in CSS and why it may not be the best approach. They explore how to bring back spacing using margins and how to use typography principles to create visual hierarchy.

Abandoning Margins for Gap

  • Many people are abandoning margins and using gap for everything.
  • This can be problematic because Gap is consistent and doesn't allow for typographic variation.
  • Using margin block start with M instead of REM can help create visual hierarchy based on font size.

Advantages of Using Gap

  • Using Gap has some advantages, such as not having to deal with adding margins to elements at the top or bottom of a page.
  • If you do decide to use Gap, it's better to use grid than flexbox.

Bringing Back Spacing with Margins

  • To bring back spacing using margins, select all children of an element and add margin block start with M instead of REM.
  • This allows for typographic variation and creates visual hierarchy based on font size.

Conclusion

The speaker suggests that while using Gap can have its advantages, it's important not to abandon margins completely. By bringing back spacing using margins and following typography principles, we can create more visually appealing designs that are easier to scan through quickly.

Gap and Grid

In this section, the speaker discusses the use of gap and grid in CSS layouts.

Using Gap for Spacing

  • Gap is a useful tool for creating equal spacing between components.
  • It works well vertically when content is shifting around.
  • Margins can be annoying to use in situations where spacing left and right is needed.
  • Use gap in situations where you need spacing between components, such as cards or layouts with different items.

Using Grid with Gap

  • Display grid can be used with gap to create equal spacing between items.
  • However, using gap can cause issues with stretching elements, such as buttons.
  • If you want to adjust the alignment of items within a grid layout, use justify-items and align-items instead of justify-content and align-content.

Alternatives to Gap

  • Instead of using gap, you could target specific elements and add margin-top to achieve consistent spacing.
  • The "lobotomized owl" technique can also be used to select all children except the first one and apply margin-top to them.

Form Layout with Grid and Gap

In this section, the speaker discusses how to use grid and gap for form layout.

Using Grid and Gap for Form Layout

  • Use form-group class to group label and input together.
  • Use display: grid property to create spacing between form groups.
  • Add a gap of desired size using gap property.
  • Use larger spacing between larger items such as subtitles, titles, etc.
  • Flexbox also works well for form layout.

Margins vs Gap

In this section, the speaker compares margins and gap for creating spacing in web design.

When to Use Margins or Gap

  • Use margin when consistent spacing is not required.
  • Larger spacing is better for flow content such as subtitles, titles, etc.
  • Gap requires more setup but can be useful when consistent spacing is needed.

Flexbox vs Gap

In this section, the speaker compares flexbox and gap for creating layouts in web design.

When to Use Flexbox or Gap

  • Use flexbox when dealing with complex layouts that require flexibility.
  • Use gap when consistent spacing is needed between elements.
Video description

I’ve seen some people completely abandoning margins and turning to using gap for everything, and to me, that’s problematic. Don’t get me wrong, gap is an absolute lifesaver, especially when it comes to spacing out components. What it’s not best for, however, is spacing out regular flow content like text. So, this isn’t to say that you should never use gap; it’s simply to say, use it for what it’s good at. 🔗 Links ✅ How I decide between using Flexbox and Grid: https://youtu.be/3elGSZSWTbM ⌚ Timestamps 00:00 - Introduction 00:31 - Using gap to replace margin 02:12 - The problem with consistent spacing and the advantage of margins 03:45 - The places where gap is a life-saver 04:45 - The problem with using gap within components 06:40 - Removing margins in specific places is often easier anyway 07:56 - More good use cases for gap #css -- Come hang out with other dev's in my Discord Community 💬 https://discord.gg/nTYCvrK Keep up to date with everything I'm up to ✉ https://www.kevinpowell.co/newsletter Come hang out with me live every Monday on Twitch! 📺 https://www.twitch.tv/kevinpowellcss --- Help support my channel 👨‍🎓 Get a course: https://www.kevinpowell.co/courses 👕 Buy a shirt: https://teespring.com/stores/making-the-internet-awesome 💖 Support me on Patreon: https://www.patreon.com/kevinpowell --- My editor: VS Code - https://code.visualstudio.com/ --- I'm on some other places on the internet too! If you'd like a behind the scenes and previews of what's coming up on my YouTube channel, make sure to follow me on Instagram and Twitter. Twitter: https://twitter.com/KevinJPowell Codepen: https://codepen.io/kevinpowell/ Github: https://github.com/kevin-powell --- And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!