惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
GRAHAM CLULEY
S
Security @ Cisco Blogs
P
Proofpoint News Feed
Cisco Talos Blog
Cisco Talos Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
WordPress大学
WordPress大学
Project Zero
Project Zero
S
Schneier on Security
P
Proofpoint News Feed
小众软件
小众软件
P
Privacy International News Feed
美团技术团队
L
LangChain Blog
Know Your Adversary
Know Your Adversary
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
Engineering at Meta
Engineering at Meta
I
InfoQ
量子位
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
Spread Privacy
Spread Privacy
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
U
Unit 42
P
Privacy & Cybersecurity Law Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
Latest news
Latest news
K
Kaspersky official blog
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
Simon Willison's Weblog
Simon Willison's Weblog
云风的 BLOG
云风的 BLOG
S
Securelist
AWS News Blog
AWS News Blog
F
Fortinet All Blogs
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
Scott Helme
Scott Helme
Help Net Security
Help Net Security
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Tenable Blog

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
Self Gap
Ahmad Shadeed · 2025-03-18 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

The problem

Have you ever removed a flexbox from a container or wrapped some elements in a new container just to have more control over the spacing between the items? You are not alone.

When I use flexbox or grid gap, I sometimes wonder if I can control the gap between some elements.

Consider the following example:

<div class="card">
  <img src="thumb.jpg" alt="" />
  <div class="cardContent">
    <h3>Title</h3>
    <p>Description</p>
    <p><a href="#">Link</a></p>
  </div>
</div>
.card {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

This will add vertical spacing between the flex items.

Spacer opacity:

Homemade cookies

The best in class cookies at the comfort of your home.

View the recipe

If I need to reduce the space between the title and description, what are the options that I have?

  • Adjust the vertical margin of the title or description.
  • Wrap title and description in a new container and re-apply flexbox with a smaller spacing.
  • Use the default flow layout with margins.

Current options to change the gap between items

Adjust the vertical margin

I tried adjusting the margin first.

.card-description {
  margin-top: calc(var(--space) / 2 * -1);
}

Here is a working demo with the result. Play with the spacer opacity to get a feel of the spacing.

Spacer opacity:

Homemade cookies

The best in class cookies at the comfort of your home.

View the recipe

While that works, it comes with a few issues.

Removing an element from the card

Let’s assume that we want to remove the title, maybe we need to use this card in a place where a title is not needed.

The best in class cookies at the comfort of your home.

View the recipe

Since the space is added to the description, it will still be there. In that case, we need to remove it.

.card--without-title .card-description {
  margin-top: 0;
}

It works, but it feels hacky.

Wrap in a new container

First, we need to wrap the title and description in a new container.

<div class="card">
  <img src="thumb.jpg" alt="" />
  <div class="card-content">
    <div class="card-item">
      <h3>Title</h3>
      <p>Description</p>
    </div>
    <p><a href="#">Link</a></p>
  </div>
</div>

Then, we need to apply flexbox on the new container with flex-direction: column and set the space we need with gap.

.card-item {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

Here is the result:

Spacer opacity:

Homemade cookies

The best in class cookies at the comfort of your home.

View the recipe

While that works and it sounds less hacky than the first option, the biggest drawback is that we need to change the HTML.

Can we do better?

Using the flow layout

We can use the flow layout (default one) and use margins with CSS variables. A few years ago, I learned about the flow utility from Andy Bell. I use it in almost all of my projects.

The idea is to use the universal CSS selector to select sibling elements and add margins to each.

.u-flow {
  > * + * {
    margin-top: var(--space);
  }
}

The above CSS will add a margin-top to all elements between the first and last child. In our example, we can do this:

.card {
  --space: 12px;
}

.card-content {
  > * + * {
    margin-top: var(--space);
  }
}

Then, we can override the space variable on per element basis.

.card-description {
  --space: 4px;
}

Here is the result:

Spacer opacity:

Homemade cookies

The best in class cookies at the comfort of your home.

View the recipe

Homemade cookies

The best in class cookies at the comfort of your home.

View the recipe

To me, this is the best option for now:

  • It works without weird hacks
  • We can remove an item and the spacing will work just fine
  • No need to edit the HTML

My proposal

This got me to think about having a native way to do this with CSS gap. Since the gap property is added to the container, we need a way to allow the flex or grid items to have a custom gap.

<div class="card">
  <img src="thumb.jpg" alt="" />
  <div class="card-content">
    <h3>Title</h3>
    <p>Description</p>
    <p><a href="#">Link</a></p>
  </div>
</div>

I didn’t think deeply about the naming, but I reused a similar naming convention in CSS from align-self.

Here, I used CSS scope to scope the selection between two HTML elements. This isn’t valid in the browser as per my testing.

.card-content {
  @scope (h3) to (p) {
    gap-self: 4px;
  }
}

Or maybe something like this:

.card-content {
  p {
    gap-above: 4px;
  }
}

What do you think?