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

推荐订阅源

Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LangChain Blog
月光博客
月光博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
博客园_首页
T
Tailwind CSS Blog
P
Proofpoint News Feed
雷峰网
雷峰网
D
Darknet – Hacking Tools, Hacker News & Cyber Security
IT之家
IT之家
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
S
Schneier on Security
Security Archives - TechRepublic
Security Archives - TechRepublic
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
WordPress大学
WordPress大学
The Register - Security
The Register - Security
N
Netflix TechBlog - Medium
Hugging Face - Blog
Hugging Face - Blog
Project Zero
Project Zero
博客园 - 叶小钗
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
L
LINUX DO - 最新话题
Google DeepMind News
Google DeepMind News
P
Privacy International News Feed
I
InfoQ
F
Fortinet All Blogs
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threatpost
T
Tenable Blog
B
Blog RSS Feed

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 CSS Container Query Units Less Absolute Positioning With Modern CSS 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
Conditional Border Radius In CSS
Ahmad Shadeed · 2021-10-04 · 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

A while ago, I was inspecting facebook.com home page feed to learn and see how they build things out. I’m always curious to see how people write CSS. I noticed a very, very interesting border-radius value for the card component in the main feed.

I shared the following tweet about this little discovery. Then, I received this reply from Miriam Suzanne:

is it always 8px? That math looks like a toggle to me, where at a certain point ((100vw - 4px) - 100%) could be negative, toggling 9999 to -9999? Which would flip the value to 0? Basically: if we’re within 4px of the full viewport size, remove the border radius.

After a few hours, Frank Yan from Facebook (Yay!) confirmed that this is a conditional statement to flip 8px to 0px when the card is taking the full viewport width.

Isn’t that just amazing?

At first, I thought that this is a kind of bug or something that was done by mistake. Thankfully, I was wrong. In this article, I will try to highlight the problem, and explain how the solutions work.

The problem

We have a card component with a border-radius of 8px. When the card doesn’t have margin or is taking the full viewport width, we want to flip the border-radius to 0.

This can be done by removing the border-radius with a CSS media query like this.

@media (min-width: 700px) {
  .card {
    border-radius: 8px;
  }
}

In some cases, that is limiting. If for some reason we want to activate the border-radius when the viewport size is less than 450px, we will need to create a variation CSS class and use media query again.

@media (max-width: 450px) {
  .card--rounded {
    border-radius: 8px;
  }
}

The solution

This is a clever one that was done by the team Facebook. It mimics the following logic:

if (cardWidth >= viewportWidth) {
    radius = 0;
} else {
    radius = 8px;
}

To implement that logic in CSS, we need to compare between two values by using CSS comparison functions. If you don’t know them, I recommend reading this article by yours truly.

The solution is inspired by the article The Flexbox Holy Albatross by Heydon Pickering. It was adapted by Naman Goel from Facebook to work with border-radius.

.card {
  border-radius: max(
    0px,
    min(8px, calc((100vw - 4px - 100%) * 9999))
  );
}

Let’s walk through the above CSS in detail.

  1. We have a max() function that compares between 0px and the computed value of the min(). It will pick the larger value.
  2. The min() function compares between 8px and a computed value from calc((100vw - 4px - 100%) * 9999). This will result with a very large positive or negative number.
  3. The 9999 is a large number to force the value to be either 0px or 8px.

Let’s explain the calc() magic!

The magic happens in the 100% value. It can be different based on two different scenarios:

  • It can be equal to 100% of its containing element (The parent/wrapper.. or whatever it’s called in your CSS).
  • Or, it can be equal to the 100vw, in case the card is taking the full viewport width (E.g: in mobile).

Why to use 9999?

It’s not because this exact number has superpower or something. It’s about avoiding an edge case. Thanks to Temani Afif for reminding me of that.

Let’s suppose that the viewport width is 375px, and the container is 365px. If we substitute these values in the equation, it will look like this.

.card {
  border-radius: max(0px, min(8px, calc(375px - 4px - 365px)));
  /* will result to */
  border-radius: max(0px, min(8px, 6px));
}

Based on the above, the value 6px will be picked by the browser. We don’t want that. Instead, the radius should be either 0px or 8px. To accomplish that, we can multiple the result by a large number that is less probably to be used in CSS, like 9999.

.card {
  border-radius: max(
    0px,
    min(8px, calc((375px - 4px - 365px) * 9999))
  );
  /* will result to */
  border-radius: max(0px, min(8px, 59994px));
}

Based on that, the browser will pick the 8px from the min function, and then the same value from the max() function.

Let’s take an example based on the first scenario. We have a viewport with a width of 1440px, and the card component lives within a 700px container.

Multiplying the resulted value by 9999 will result in 7359264, which is a large random number. In that case, the CSS will look like this for the browser:

.card {
  border-radius: max(0px, min(8px, 7359264px));
}

Since we have min(), it will pick the smallest value which is 8px. When compared to the max(), the 8px will win too. That’s the first use-case of this clever CSS.

.card {
  border-radius: 8px;
}

Next, is when the card is taking the full viewport width. That can be seen in a mobile viewport. Notice that the container and viewport width are the same.

Multiplying the value with 9999 will result in -39996px, which is a negative value. The browser will read it like the following:

.card {
  border-radius: max(0px, min(8px, -39996px));
}

Now to the fun! The browser has two questions to ask:

  • Which value is smaller? 8px or -39996px? The result is -39996px.
  • Which value is larger? 0px or -39996px? The result is 0px.
.card {
  border-radius: 0px;
}

Did you see how that happened? I’m still surprised by such a clever usage of CSS comparison functions.

We can also take this to the next level by using CSS clamp() as suggested by Temani Afif and Liad Yosef. I think team Facebook didn’t use it since it’s not supported in older versions of Safari (e.g: v12).

.card {
  border-radius: clamp(0px, ((100vw - 4px) - 100%) * 9999, 8px);
}

Check it out on Codepen.

I hope you enjoyed the article. Thanks for reading!