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

推荐订阅源

Forbes - Security
Forbes - Security
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
美团技术团队
博客园 - 聂微东
博客园 - 叶小钗
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
Spread Privacy
Spread Privacy
J
Java Code Geeks
雷峰网
雷峰网
宝玉的分享
宝玉的分享
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
量子位
L
LINUX DO - 热门话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
D
Darknet – Hacking Tools, Hacker News & Cyber Security
月光博客
月光博客
腾讯CDC
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tor Project blog
罗磊的独立博客
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
Project Zero
Project Zero
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
MyScale Blog
MyScale Blog
T
The Exploit Database - CXSecurity.com
GbyAI
GbyAI
博客园 - 【当耐特】
O
OpenAI News
Schneier on Security
Schneier on Security
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
博客园 - 司徒正美

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 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 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
Do we need CSS flex-wrap detection?
Ahmad Shadeed · 2023-02-19 · 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

When I write CSS, I wish there is a way to have flex-wrap detection in CSS. In some cases, I design a simple component that is responsive with flex-wrap, but the point is that I want to have even more control and be able to detect when an item is wrapped. This post goes into more detail about one of the wishes I have for CSS in 2023.

In this post, I will try to explain why we need it and what the use-cases can get benefit from that.

Flex wrapping

To get you into the context, flex-wrap is a way that we can decide if flex items should wrap into a new line or not. Allowing the flex items to wrap can result in a component that works responsively regardless of its container width.

.list {
  display: flex;
  flex-wrap: wrap;
}

It’s that simple.

Flex wrapping detection examples

I tend to avoid using flex-wrap when building a site header. I prefer to use a media query and estimate the safest breakpoint to hide the navigation and show the mobile menu toggle.

Consider the following example.

.site-header {
  display: flex;
  align-items: center;
}

@media (max-width: 600px) {
  .site-header {
    /* Hide the navigation, and show the mobile menu toggle. */
  }
}

To make the site header responsive, I add a media query in the second state. The reason is I assume that the navigation might get an additional item and there is still enough space for it.

If I do the mobile stuff in the third state, the design might fail when the navigation has more items.

If I have flex-wrap state detection, this will make my job a lot easier. I can imagine something like this:

.site-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}

.site-header:has(.nav:wrap) {
  /* Hide the navigation, and show the mobile menu toggle. */
}

I used :has because I need to know if the child item is wrapped (The navigation links, in our case).

You might be wondering if both the media query and the flex-wrap detection give us the same result. Correct, but that’s not always the case.

I can break the media query-based navigation by just adding a long text.

Consider the following figure:

The media query won’t know that we have more navigation items, thus the need to kick in. It works based on the viewport width, not our content.

When working on a multilingual website, the same navigation items for English might look different in length compared to Arabic, for example.

Consider the following example.

When the viewport size gets smaller, the media query might kick in too early. This doesn’t happen all the time, but it’s possible. What I can do in such a case is to have a new media query, just for the RTL layout.

@media (max-width: 700px) {
  html[dir="rtl"] {
    .site-header {
      /* Custom CSS... */
    }
  }
}

With flex-wrap detection, the navigation will work regardless of the content length.

.site-header:has(.nav:wrap) {
  /* Hide the navigation, and show the mobile menu toggle. */
}

Tabs

We can use flexbox to lay out a tabs component. On mobile, I want the design to switch to a list instead of tabs. The trick is to know when the tab items are wrapped.

This isn’t possible in the current CSS (except with hacks I don’t prefer to use). We can use a media query to do that, but it’s not perfect.

@media (min-width: 600px) {
  .tabs {
    display: flex;
    flex-wrap: wrap;
  }
}

Again, the media query above doesn’t know anything about the content. It is just an estimation from the front-end developer.

As you see, it can fail when we have more tabs. If we can detect when the first flex item is wrapped, we can fix that in a much clearer way.

.tabs {
  display: flex;
  flex-wrap: wrap;
}

.tabs:wrap {
  flex-direction: column;
  border-bottom: 0;
}

.tabs:wrap .tab__item {
  flex: 1;
}

Auto margin and flex wrapping

Auto margins and flexbox are useful. We can use it to push a flex item to the far end of its container.

.card {
  display: flex;
  flex-wrap: wrap;
}

.actions {
  margin-left: auto;
}

The challenge here is that the card might look weird when it’s wrapped. We don’t know when that will happen.

If we have flex-wrap detection, the job will be much easier.

.card {
  display: flex;
  flex-wrap: wrap;
}

.card:wrap .actions {
  margin-left: initial;
}

.card:wrap .actions__item {
  /* show the label */
}

Conclusion

So, do we need flex wrapping detection? It’s a big yes for me. The above are a few examples of where flex wrapping detection can be helpful. I believe having this feature is the closest thing to a content query in CSS.

What about you? Do you think that this feature should be implemented in CSS?