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

推荐订阅源

人人都是产品经理
人人都是产品经理
D
Docker
GbyAI
GbyAI
B
Blog RSS Feed
博客园 - 司徒正美
博客园 - Franky
美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
AI
AI
O
OpenAI News
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
S
Secure Thoughts
博客园 - 聂微东
L
LINUX DO - 最新话题
U
Unit 42
SecWiki News
SecWiki News
A
Arctic Wolf
Schneier on Security
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Visual Studio Blog
量子位
The Cloudflare Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
B
Blog
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
Last Week in AI
Last Week in AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
Latest news
Latest news

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
Use cases for CSS comparison functions
Ahmad Shadeed · 2022-09-22 · 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

CSS comparison functions have been supported since almost April 2020, and I wrote an introductory article about them back then. I like to use all of them, but my favorite one of them is clamp() and it’s the top used one for me.

In this article, I will explore a few use-cases for comparison functions, and explain each one in detail. Mostly, the use cases will be about situations other than using them for fluid sizing, as this is the most popular use case and I will keep that to the last.

If you don’t know about comparison functions, that’s totally fine. I recommend reading this introduction article first and then coming back here for the use cases.

Use cases for clamp(), max(), and min() CSS functions

Fluid sizing and positioning

In this example, we have a section with a mobile phone, along with two images that are positioned on top. Initially, it will look like the following figure:

When the width of the container becomes smaller, we want to shrink the size of the images to accommodate the available space. We can do that by using a percentage value for the width or the height (e.g: width: 20%) but this doesn’t give us much control.

We want to be able to have a fluid size, that respects a minimum and a maximum value at the same time. This is where clamp come to the rescue!

.section-image {
  width: clamp(70px, 80px + 15%, 180px);
}

By setting a minimum, preferred, and maximum width, the image will shrink or grow as per its container width. This is due to using a mix of a fixed value and percentage 80px + 15%.

Check the video below and notice how the thumbnails shrink on resize.

Demo

Decorative element

Have you ever needed to add a decorative element to a section? Most of the time, the element needs to be responsive and might need to be positioned differently based on the viewport size.

Consider the following example.

There are two decorative elements on both sides. On mobile, they will occupy too much space and so we want to show only a little of each one.

To fix that, we can use media queries to offset them on mobile.

.decorative--1 {
  left: 0;
}

.decorative--2 {
  right: 0;
}

@media (max-width: 600px) {
  .decorative--1 {
    left: -8rem;
  }

  .decorative--2 {
    right: -8rem;
  }
}

While this works, we can use a media query-less solution with CSS clamp() function.

@media (max-width: 600px) {
  .decorative--1 {
    left: clamp(-8rem, -10.909rem + 14.55vw, 0rem);
  }

  .decorative--2 {
    right: clamp(-8rem, -10.909rem + 14.55vw, 0rem);
  }
}

Let me dissect the above CSS to make it easier for you:

  • What we want is to set the minimum left offset as -8rem, and the maximum value as 0rem.
  • With that, we leave it to CSS clamp() to decide on the preferred value and respect the minimum and maximum values we set.

I used this calculator to get the above clamp() numbers.

Demo

Fluid hero height

Related to the previous example, a hero section height can be different based on the viewport size. As a result, we tend to change that via a media query or by using viewport units.

.hero {
  min-height: 250px;
}

@media (min-width: 800px) {
  .hero {
    min-height: 500px;
  }
}

We can use a mix of fixed value and viewport units, but we need to be careful to not have a huge height on larger viewports, and then we need to set a max height.

.hero {
  min-height: calc(350px + 20vh);
}

@media (min-width: 2000px) {
  .hero {
    min-height: 600px;
  }
}

With CSS clamp(), we can set a minimum, preferred, and maximum height with only one CSS declaration.

.hero {
  min-height: clamp(250px, 50vmax, 500px);
}

When resizing the screen, you will notice that the height changes gradually based on the viewport width. In the example above, 50vmax means “50% of the viewport’s largest dimension.

Demo

Loading bar

This example is inspired by this tweet from Andy Bell. I really like the use of CSS clamp() for this use case!

The bar thumb is supposed to animate from the left to right and vice versa. In CSS, the thumb can be positioned absolutely to the left.

.loading-thumb {
  left: 0%;
}

To position the thumb to the far right, we can use left: 100% but this will introduce an issue. The thumb will blow out of the loading bar container.

.loading-thumb {
  left: 100%;
}

That is expected, because 100% in this context starts from the end of the thumb, thus pushing it out.

We can use CSS calc() to subtract the thumb width and it will work, but this isn’t 100% flexible.

.loading-thumb {
  /* 40px represents the thumb width. */
  left: calc(100% - 40px);
}

Let’s explore how to better CSS for that using CSS variables and comparison functions.

.loading-thumb {
  --loading: 0%;
  --loading-thumb-width: 40px;
  position: absolute;
  top: 4px;
  left: clamp(
    0%,
    var(--loading),
    var(--loading) - var(--loading-thumb-width)
  );
  width: var(--loading-thumb-width);
  height: 16px;
}

Here is how the above CSS works:

  • First, we set a minimum value of 0%.
  • The preferred value is the current value of the --loading CSS variable.
  • The maximum value represents the current loading minus the thumb width.

CSS clamp() here provide us with three different stats for this component. I personally like this solution!

Not only that, we can extend the same concept for a different design. Consider the following figure:

The current progress value has a little handle on top of it. When the value is 100%, we need the width to respect that.

As you see in the figure below, the circle must end at the far right side. If we don’t take care of that, it will end up blowing out by half of the handle width (See the second row with the red sign).

In such a case, we can use CSS clamp() function.

.loading-progress {
  width: clamp(10px, var(--loading), var(--loading) - 10px);
}

The minimum value is equal to half the circle width, the preferred value is the current loading percentage, and the maximum value is the subtraction result of the current percentage from half of the circle.

Demo

Dynamic Line separator

Earlier this year, I published an article about an interesting CSS solution for a UI I was working on.

Consider the following figure where we have a line separator between two sections.

On mobile, that separator should become horizontal as below.

My solution was to use a border and flexbox. The idea is that a pseudo-element with a border can expand to fill the available space for both the vertical and horizontal states.

.section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.section:before {
  content: "";
  border: 1px solid #d3d3d3;
  align-self: stretch;
}

@media (min-width: 700px) {
  .section {
    align-items: center;
    flex-direction: row;
  }
}

We can even do better by using CSS clamp. Temani Afif suggested a solution that doesn’t need a media query at all.

.section {
  --breakpoint: 400px;
  display: flex;
  flex-wrap: wrap;
}

.section:before {
  content: "";
  border: 2px solid lightgrey;
  width: clamp(0px, (var(--breakpoint) - 100%) * 999, 100%);
}

Let’s dissect the above CSS:

  • 0px: the minimum value, used for the vertical separator. It’s zero because we’re using a CSS border instead.
  • (var(--breakpoint) - 100%) * 999 a toggle that switch between 0px or 100% based on the viewport width.

Here is a video:

Conditional border radius

Almost a year ago, I spotted a neat CSS trick in the Facebook feed CSS. It’s about using CSS max() comparison function to switch the radius of a card from 0px to 8px depending on the viewport width.

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

Let’s walk through the above CSS in detail.

Let’s dissect the above CSS:

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

With the above, the card will have a zero radius when it’s taking the full viewport width, or 8px on larger screens. Neat, right?

You can read more here about the full details of the technique.

Defensive CSS article header

While building the article header for [Defensive CSS][https://defensivecss.dev/], I needed a way to add dynamic padding to the content while maintaining a minimum value on smaller viewports.

The idea is that the article header isn’t contained with a wrapper element, so we need a way to mimic that the content is actually wrapped and aligned with the content underneath.

To do that, we need a way to use the following formula in CSS:

dynamic padding = (viewport width - wrapper width) / 2

Thanks to the CSS max() function, we can add minimum padding, and a way to switch to the dynamic padding when needed.

:root {
  --wrapper-width: 1100px;
  --wrapper-padding: 16px;
  --space: max(
    1rem,
    calc(
      (
          100vw - calc(var(--wrapper-width) - var(--wrapper-padding) *
                2)
        ) / 2
    )
  );
}

.article-header {
  padding-left: var(--space);
}

The idea is that we need the minimum padding to be 1rem, and then it will be dynamic based on the viewport width.

For more details, you can read the full article on this technique.

Spacing

Sometimes, we might need to change the spacing for a component or a grid based on the viewport width. Not with CSS comparison functions! We only need to set it once.

.wrapper {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: min(2vmax, 32px);
}

To learn more about spacing in CSS, I wrote a deep-dive article on that.