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

推荐订阅源

D
DataBreaches.Net
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Webroot Blog
Webroot Blog
AI
AI
P
Palo Alto Networks Blog
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
Spread Privacy
Spread Privacy
T
Tor Project blog
罗磊的独立博客
小众软件
小众软件
S
Security Affairs
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
NISL@THU
NISL@THU
博客园_首页
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
N
News and Events Feed by Topic
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Forbes - Security
Forbes - Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Cloudflare Blog
A
About on SuperTechFans
爱范儿
爱范儿
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
TaoSecurity Blog
TaoSecurity Blog
宝玉的分享
宝玉的分享
G
GRAHAM CLULEY
雷峰网
雷峰网
F
Full Disclosure
I
Intezer
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)
U
Unit 42

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 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 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
Intrinsic Sizing In CSS
Ahmad Shadeed · 2020-01-26 · 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

In CSS, we have two ways of sizing, intrinsic and extrinsic. The latter is the most common one which means using fixed values for the width or height of an element. The first one means that the sizing of an element depends on its content size.

Through this article, I will explore each of the intrinsic values and see how we can get the benefit of them. Also, I will explain how to use those intrinsic values in combination with CSS grid, and other CSS properties.

Extrinsic Sizing

The extrinsic sizing simply means sizing elements (using width or height) with explicit values. Consider the following example:

.c-button {
  width: 100px;
}

The button has a width of 100px, this is called extrinsic sizing because we set it. Another example is the <div> element. By default, it’s a block element, and it means that the width of it will be 100% of its parent.

Sometimes, we want to size elements based on its content, and in that case, using extrinsic sizing doesn’t help. Let’s explore how to size with intrinsic values!

Min Content

The min-content value is the intrinsic minimum width, which means that it’s equal to the width of the element’s content longest word.

According to the CSS Working Group (CSSWG):

The inline size that would fit around its contents if all soft wrap opportunities within the box were taken.

To be honest, I didn’t understand the CSSWG’s definition well, but I think it means that if we placed a box around an element, all of its content (words) should wrap, and the width of the box will be the longest word in that element. Please correct me if I’m wrong.

Consider the following example, where we have a heading.

<h2>A title for an awesome article</h2>
h2 {
  width: min-content;
}

As you notice, the width of the element when min-content is used as a value for width is equal to the width of the longest word, which is “awesome” for that example.

See the Pen Intrinsic Sizing by Ahmad Shadeed (@shadeed) on CodePen.

Max Content

The max-content value represents the intrinsic preferred width, which means that it’s equal to the width of the content.

Consider the same example for the heading, but with max-content used this time.

h2 {
  width: max-content;
}

Notice how the element width became equal to the title. That’s width is dynamic, so when the title changes, it will change accordingly.

Fit Content

It’s a combination of using min-content and max-content. While researching, I liked this answer on Stackoverflow:

fit-content uses max-content, unless available < max-content, then it uses available. Unless available < min-content, then it uses min-content.

Let’s visualize this.

Note that available stands for the available space in the viewport. Let’s take an example to see how it works in action.

Based on the same example previously, I added the following CSS:

h2 {
  width: fit-content;
}

See the below video with captions for what is happening on viewport resize.

To recap about fit-content, it checks if the available is more than max-content, then the width is equal to max-content. If the available space is less than max-content, then the width will be equal to the available space. Finally, if the available space is less than min-content, then the width will be equal to min-content.

Now that I explained the basics of each intrinsic value, let’s move to real-life examples and use cases.

Browser Support

According to Can I Use, the support is good as all the major browsers support intrinsic values, except Microsoft Edge.

The intrinsic values are supported in Edge (Edgium), which is chromium based.

Use Cases and Examples

Figure and Caption

When we have a figure with a caption, its width will be 100% of its parent because it’s a block element.

<figure>
  <img src="blueberry-cheesecake-x.png" alt="" />
  <figcaption>..</figcaption>
</figure>

The desired behavior is that the figure should wrap to the width of the image, which will keep the text wrapped below the image. max-content to the rescue! By adding it to the figure element, it will make the figure width equal to its content.

figure {
  width: max-content;
  margin-left: auto;
  margin-right: auto;
}

However, everything is cool until the image width is larger than the viewport. In that case, the width of the figure will be as large as the image, which will cause horizontal scrolling.

To solve that, fit-content should be used instead. The reason is that it won’t make the image go beyond the viewport width, in case its width is very large.

figure {
  width: fit-content;
}

See the Pen Figure Caption - 2 by Ahmad Shadeed (@shadeed) on CodePen.

Separator With Title

In this example, there is the title “Top Stories” that needs to wrap into two lines. The width of it should be dynamic, so whatever the title is, it should wrap. To achieve that, we can use min-content.

span {
  width: min-content;
}

See the Pen Separator With Text by Ahmad Shadeed (@shadeed) on CodePen.

Title With Underline

Another interesting use case for intrinsic values is a heading, with a border that’s only the same width as its content. Consider the following figure:

Note that the heading is a block-level element. To achieve that, we can use the solution which wraps the content in a <span> element, and then the border can be applied to the <span>.

<h2><span>A title for an awesome article</span></h2>
h2 span {
  border-bottom: 2px solid #e2deed;
}

To do that better, we can use fit-content and it will make the title width as long as its content. See the below CSS:

h2 {
  width: fit-content;
  margin-left: auto; /* For centering */
  margin-right: auto; /* For centering */
  border-bottom: 2px solid #e2deed;
}

See the Pen Title With Underline by Ahmad Shadeed (@shadeed) on CodePen.

Intrinsic Navigation

You might need page navigation that has a width based on its content. By using max-content, we can achieve that easily:

.c-nav {
  width: max-content;
}

Demo

Intrinsic Todo List

One of the things that caught my eyes while trying to research and come up with practical use cases for intrinsic values, is this article.

Consider the following example.

We have a todo list with the following elements: header, list, and footer.

The middle section height should be 100% - header - footer, no matter how many todo items it has. To accomplish that, we can use CSS grid and min-content for the wrapper.

.c-todo {
  display: grid;
  grid-template-rows: min-content auto min-content;
  height: 100vh;
}

You might be wondering that what would happen in case we didn’t use min-content for the rows? Well, here you go:

The rows extend to the available space without considering its content. However, when min-content is added to the header and footer, their height won’t exceed more than the content in each one.

Intrinsic Chat

Imagine building an application that has a messaging system. In the example below, the layout structure if very similar to the todo list. When no chats are available and min-content is not used, expect the layout to break.

Demo

Hero Section

Consider having a header that is placed inside a hero section. The goal is to have a dynamic layout that makes the hero content take the full available space.

.c-section {
  display: grid;
  grid-template-rows: min-content 1fr;
}

By having two rows, the first one is taking a height based on its minimum content, and the second row can extend to fill the available space.

See the Pen Intrinsic - Hero by Ahmad Shadeed (@shadeed) on CodePen.

I always wonder why we have a sidebar with a fixed width. What if the width of it was content-based? Say, for example, that it has a minimum width based on its content and a maximum width? Let’s try that.

<div class="wrapper">
  <aside></aside>
  <main></main>
</div>
.wrapper {
  grid-template-columns: fit-content(150px) 1fr;
  grid-gap: 1rem;
}

By using CSS grid fit-content function, we can ensure that the sidebar width won’t go above 150px, and it can shrink below that in case the content is short.

And when the content of the sidebar is short, it will shrink a bit. See below:

Heading and Description

We have a heading and a description text. The description text width shouldn’t go beyond the main heading width. For me, this is an interesting use case that I didn’t think it was possible to do in CSS only.

Consider the following mockup.

To implement the above, we need to add min-content as a width for the wrapping section, and max-content as a width for the heading element.

section {
  width: min-content;
  margin: 0 auto;
  text-align: center;
}

h2 {
  width: max-content;
}

Please note the above needs some tweaking on mobile, as it will cause horizontal scrolling.

See the Pen Intrinsic - Test... by Ahmad Shadeed (@shadeed) on CodePen.

The End

And that’s a wrap. Do you have a comment or a suggestion? Please feel free to ping me on @shadeed9.

Resources