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

推荐订阅源

人人都是产品经理
人人都是产品经理
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

Chip Cullen

The need for importance, and AI: Chip Cullen An updated Colorosetta: Chip Cullen The Return of the Font Combinator!: Chip Cullen Changing the number of an item in an ordered list: Chip Cullen My pizza dough recipe as of May 2025: Chip Cullen Gonna try to be a bit more personal: Chip Cullen How I built dynamic social media images in Eleventy using Cloudinary: Chip Cullen My current approach to AI : Chip Cullen Lessons Learned Surviving a Major Product Launch: Chip Cullen How to Build a Drop Down Menu with Modern CSS: Chip Cullen How to stop page scrolling when you have an open dialog element: Chip Cullen Distraction Driven Development: Chip Cullen How I learned to code: the art of letting go: Chip Cullen In praise of the switch statement: Chip Cullen Project stuck? Think about how you’re breaking it down & question everything: Chip Cullen So how did the onboarding experiment go?: Chip Cullen Ideas for an Onboarding Checklist: Chip Cullen I really like Post Mortems: Chip Cullen Raise Red Flags Early: Chip Cullen How to mock fetch requests in React Testing Librarty tests: Chip Cullen Running a Structured Meeting: Chip Cullen Adding the View Transitions API to my personal site: Chip Cullen A Lightweight Way to Read GraphQL Data: Chip Cullen How to make a color changing favicon: Chip Cullen Using a Pros/Cons list to help navigate technical discussions: Chip Cullen How to use variable fonts from Google Fonts: Chip Cullen A new website: now on Eleventy!: Chip Cullen How to Truncate Type at More Than One Line with Just CSS: Chip Cullen Colorosetta: the VS Code Extension!: Chip Cullen Using CSS Custom Properties and Logical Properties Together: Chip Cullen Browser Dev Tools: Element Inspector Popover: Chip Cullen The Link with rel=preload is a Seperate Thing: Chip Cullen How to have Dark & Light Mode Images that also works with User Choice: Chip Cullen Don’t use Viewport Units for Font Size on their own: Chip Cullen A little known Media Query: Aspect Ratio: Chip Cullen Meta thinking: Managing Decisions: Chip Cullen Give Your To-Do's Context: Chip Cullen Say What the Impact is when Reporting Issues: Chip Cullen Firefighting 101: How to Manage Breakages: Chip Cullen How to Deal With Large Pieces of Technical Debt: Chip Cullen Make Your Request Clear: Chip Cullen Analytics events, HTML classes, and protecting against refactoring: Chip Cullen How We Removed jQuery from a large app: Chip Cullen New tool: ColoRosetta: Chip Cullen What width and height attributes should you use with responsive images?: Chip Cullen Django 3.1 gotcha: Referrer Policy has a new default, and it might break iframes and links: Chip Cullen A Javascript Component Pattern: Chip Cullen Pointer Events and Inline Elements in Chrome: Chip Cullen Resolving a github repo and a new Create React App: Chip Cullen How to POST *Data* with the Fetch API: Chip Cullen The Contrast Triangle: Chip Cullen Advice on interviewing for Junior Developers: Chip Cullen Life Lessons Learned From Running a Marathon: How to do something really hard: Chip Cullen A (Brief) intro to Search Engine Structured Data: Chip Cullen Javascript Fallback Values on Variables and Booleans - a hard lesson: Chip Cullen Alfred Tip: Quickly Access Common URLs: Chip Cullen Responsive Images in Hugo - by Laura Kalbag: Chip Cullen Making a Gatsby Site with Multiple Content Types: Chip Cullen How to Create and Use Fixtures in Cypress Tests: Chip Cullen Fixing the 'Bad Interpreter' Error from AWS and Python 3.7: Chip Cullen Creating a Canonical Tag in a Django Template: Chip Cullen Responsive spacing with viewport and ch units: Chip Cullen Welcome to my New Design - 2019: Chip Cullen Django Templates: Block and If statements don’t work like you might expect: Chip Cullen Books I Read in 2018: Chip Cullen Lifehack: 4 ways to help tame common email noise: Chip Cullen How to make better Pull Requests: Adding Steps to Test: Chip Cullen The unsung develpment tool: Spreadsheets: Chip Cullen Troubleshooting Adding and Removing EventListeners: with Arguments, Debounced, and in a React Class: Chip Cullen How to Fake the Window Object in Jest and Enzyme: Chip Cullen Migrating From Wordpress to Hugo: Chip Cullen Background Repeat and its Possibilities: Chip Cullen Getting Started With Front End Tests: a Mindset: Chip Cullen Migrating a Blog - An Opportunity for a Content Inventory: Chip Cullen Moving to Hugo: Chip Cullen JavaScript events: .target vs .currentTarget: Chip Cullen Things I wish I knew when starting with Python: Chip Cullen Leading Ampersands for modifiers in Sass: An anti-pattern: Chip Cullen How to get rid of the "You have mail" message in your terminal: Chip Cullen Why three typefaces rule the web, and what you can do about it: Chip Cullen You shouldn't worry about Section 508 - it's Section 504: Chip Cullen Looping Video Backgrounds: pointers and pitfalls: Chip Cullen How to “preview” a click event tag in the Google Tag Manager console: Chip Cullen Moving on from a technology, or: life after Drupal: Chip Cullen Don’t be a dumb developer: Chip Cullen Two level breadcrumbs with CSS :only-child: Chip Cullen Simplicity comes with experience: Chip Cullen Do the least amount possible: Chip Cullen SVGs vs. Icon Fonts: Two points in favor of Icon Fonts: Chip Cullen Accessible links without underlines: Chip Cullen The Strategic Job Hunt: Chip Cullen Surviving Getting Laid Off: Chip Cullen How to structure your typography in Sass: Chip Cullen Layer Cake: A Responsive Design Layout Pattern: Chip Cullen Creativity is yet to come in Web Design: Chip Cullen Front End Testing with Wraith: A Step by Step Recipe: Chip Cullen Where to begin? How I start a visual design for the web: Chip Cullen If you could only have five Google Fonts: Chip Cullen Why SVG is so cool (or: what happens when you're late to the party on something): Chip Cullen How to apply classes to elements with CKEditor 4, in Drupal 7: Chip Cullen
CSS min(), max() and clamp() Functions: Chip Cullen
2020-04-30 · via Chip Cullen

Intro

I've seen a lot of chatter lately about how some new CSS functions - min(), max() and clamp() are actually well supported. Even Safari supports clamp() as of 13.1.

Intended audience

This post assumes that you're an advanced and experienced writer of CSS.

Wait, CSS Functions?

A CSS function is a value that we can pass in as part of a rule that actually performs some logic. We've had one function for a while now - calc().

 /* whatever 100% is minus 32 pixels */
  width: calc(100% - 32px);

We just have three more functions that we can use.

min()

min() is pretty straightforward - you pass it a list of comma separated values, and the smallest option is used.

/* will be 100px or 10vw wide, whichever is smaller */
  width: min(100px, 10vw);

The order of the values doesn't matter:

/* does the same thing */
  width: min(10vw, 100px);

You can even have more than two if that helps:

/* will be 100px, 10vw or 50% wide, */
/* whichever is smallest */
  width: min(10vw, 100px, 50%);

Technically you can have only one value for min() and max() but ... that would be rather silly, wouldn't it?

Use cases for min()

Rather unintuitively, the thing that min() is really good at is creating a maximum value for something.

/* will use a relative font size of 10vw, up to 5rem */
  font-size: min(10vw, 5rem);
/* the third column will be 10vw wide up to 200px
  this is an alternative to minmax(),
  which is grid-specific */
  grid-template-columns: 1fr 1fr min(10vw, 200px);
/* the image will be responsive
  and take up it's container width, up to 400px */
  img {
    width: min(100%, 400px);
  }
/* will create relative padding using vh and vw units,
  but cap it at 20px */
  padding: min(5vh, 20px) min(5vw, 20px);

The above example builds on ideas I started with here.

max()

max() works just like min() but it will choose the largest value in the list provided.

/* will be 100px or 10vw wide, whichever is larger */
  width: max(100px, 10vw);

As with min(), you can one or more values, and the order doesn't matter

/* will be 100px, 10vw or 50% wide, whichever is largest */
  width: max(10vw, 100px, 50%);

Use cases for max()

As the inverse of min() - the use case for max() is to establish a minimum value that a thing can be. I tend to write my CSS mobile first, that is, smallest use case first and then work my way up. Consequently, I don't think I'll be reaching for max() that often, if I'm honest.

/* will use a relative font size of 10vw,
  but at least 5rem */
  font-size: max(10vw, 5rem);
/* the third column will be at least 200px wide,
 but 10vw above when that is larger
 this is an alternative to minmax(),
 which is grid-specific */
  grid-template-columns: 1fr 1fr max(10vw, 200px);
/* the image will be a minumum of 200px wide,
  but 100% wide beyond that */
  img {
    width: max(100%, 200px);
  }

clamp()

clamp() is reallllly interesting, and I think will be what CSS authors really should reach for in a lot of responsive implementations.

While it's a related idea to min() and max() it is different in specific ways:

  • Order matters
  • It only takes 3 parameters

Those 3 parameters are

  1. The minimum
  2. The target value (ideally a relative unit)
  3. The maximum

You use it like:

/* will be 10vw wide, with a minimum of 100px
  and a maximum of 200px */
  width: clamp(100px, 10vw, 200px);

If you want to think of it another way, as noted by MDN, it evaluates like this (using the previous example):

 /* don't actually do this */
 width: max(100px, min(10vw, 200px));

Use cases for clamp()

clamp() makes a lot of intrinsic web design ideas possible - that is, implementations that are responsive but not needing media queries.

/* will be 10vw, with a minimum of 2rem
  and a maximum of 5rem
  to me, this is the responsive typography dream */
  font-size: clamp(2rem, 10vw, 5rem);
/* the image will be responsive and
  take up it's container width,
  with a minimum of 200px and a maximum of 800px */
  img {
    width: clamp(200px, 100%, 800px);
  }
/* will create relative padding using vh and vw units,
  with minimums of 8px and maximums of 20px */
  padding: clamp(8px, 5vh, 20px) clamp(8px, 5vw, 20px);

clamp() and grid

From my experimentation, clamp() is not a valid value for use in CSS Grid. So, this is not a thing:

/* not a thing. don't do this. */
  grid-template-columns: 1fr 1fr clamp(100px, 10vw, 200px);

A note on using Sass / SCSS

Sass had min() and max() functions way before they were implemented in CSS. In order to not break existing Sass code, it works like this - if you pass in a Sass feature (like a variable), it will use the Sass function

  width: min($variable1, $variable2); // will use Sass's min()

If you are using Dart Sass, and if the function uses straight values, it will use CSS's function

  width: min(100px, 10vw); // works as before in Dart Sass

However, if you're using LibSass or Ruby Sass, it will always use the Sass min or max functions, and consequently you may see this error:

Internal Error: Incompatible units: 'px' and 'vw'.

In order to use it with with those versions of Sass, you need to use the following syntax:

  width: unquote('min(100vw, 100px)');

or, with variables:

  width: unquote('min(#{$variable1}, #{$variable2})');

Conclusion

I hope this post gives you an idea of how min(), max() and clamp() work, and more importantly, gives you some ideas of how you might use them.