Text emphasized alt text example
A cartoon skull with a hotpink hat on.2 min read

A use case for CSS overflow-clip-margin, nested border radii

css
5,381 views

With a mega viral tweet on nesting border radii and Andy mentioning it as well, I felt I should blog about my thoughts real quick! Here's one of the things I learned after requesting CSS get a nice way to draw nested radii:

.the-trick-tldr {
  overflow: clip;
  overflow-clip-margin: content-box;
}

If you're like me, you saw that and said…

The prob #

Nested border curves.

When you've got a rounded card with a rounded button inside, or a rounded footer inside a rounded section, you may not have noticed, but there's a wobble in there.

See the nested radii on the left here 🙈,
vs the radii on the right ✅:

Corners compared, one is clearly concentric

Click those tweets or read the CSSWG proposal for more about the problem space and calculations for solutions.

Using overflow-clip-margin to solve it #

In the comments of the CSSWG issue, Oriol Brufau politely mentions using overflow on the content-box with overflow-clip-margin. And it works great, but Caniuse shows you how it's not really viable yet:

Alt

tldr;

  • No Safari
  • Firefox doesn't support the content-box value
  • … Chrome only.

Try yourself #

Here's a Codepen where I show the problem space and offer a custom property and overflow-clip-margin solution, for testing and comparing.

It's totally worth opening in a tab in Chrome and inspecting that card for it's funky coo content-box overflow: clip solution, and kickin the tires 🤓

10 comments #

123likes

NirTamir, TheRastrian, rolandfranke and 96 more

22reposts
5 pingbacks

Join the conversation on

Any way you can highlight the differences a bit more? Looking at that first graphic and they all look the same to me
Evan JacobsEvan Jacobs
here's a closeup from the post, left is the undesirable (current) radius post also has links to some tweets that outline the issue as well
Adam ArgyleAdam Argyle
Ohhhhh got it! I was looking at the skull 😂
Evan JacobsEvan Jacobs
Is that the magnificent Dank Mono 👀
BerkayBerkay
Oh my 👌 Lots of good things happening in the #css world lately 😏
LukyVj - A$AP LukyLukyVj - A$AP Luky
neat 🤩
GregGreg
Glad someone else did. I had them zoomed in, going between all three, thinking, "man, I must have no design sense 'cause these all look identical."
Rowan MerewoodRowan Merewood
“tl;dr no Safari” At this point I’m not even surprised or fazed anymore.
TerryTerry
Not addressing the same problem but also houdini might help css-houdini.rocks/smooth-corners/
Onur GümüşOnur Gümüş