Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
CSS

CSS: Fix Container Queries Never Firing Inside a Flex Item

- 26.11.26 - ErcanOPAK

🎨 CSS: Fix Container Queries Never Firing Inside a Flex Item

A container query only evaluates against an ancestor explicitly established as a query container via container-type, and container-type: size specifically needs both the element’s width AND height to be fully resolved before the browser can query against them. A flex item with its default flex-grow/flex-shrink behavior actively negotiating its size against its siblings can go through flex layout without that size ever settling into a value container-type: size can commit to early enough – so a container query that works perfectly on a plain block-level container can simply never match any breakpoint the moment that same element becomes a flex item instead.

🔎 The Problem

.sidebar-item {
  container-type: size;
  container-name: sidebar-item;
}
@container sidebar-item (min-width: 300px) {
  .title { font-size: 1.4rem; }
}
/* Works fine as a plain block-level div.
   The moment .sidebar-item becomes a flex child of a flex row,
   the @container rule above never matches - its size never
   settles into something container-type: size can query */

✅ The Fix

  • Use container-type: inline-size instead of size on a flex item specifically – it only needs the element’s width to resolve, which flex layout settles earlier, avoiding the circular sizing dependency that size introduces inside flex layout.
  • Give the flex item an explicit flex-basis instead of leaving it at the auto default, so its inline size has a concrete starting value to query against rather than one still being negotiated between flex-grow and flex-shrink.
  • If the container genuinely needs to query against both dimensions, wrap it in a separate non-flex container element and make THAT wrapper the flex child instead – containment and flex-item sizing stop fighting each other once they’re on different elements.

🚨 Why This Is Dangerous

This fails completely silently – no console warning, no error, the container-query rules simply never match, so a responsive component renders at its default or fallback styling in every flex-based layout it’s used in, with nothing in the DevTools to explain why.

A container query needs a size it can trust – a flex item still being negotiated with its siblings doesn’t have one yet.

— UI Engineer

Related posts:

CSS: Fix a Grid Layout Where Columns Ignore the Widths You Set

CSS: Fix a Grid Layout Where Columns Ignore the Widths You Set

CSS: Fix a :nth-child() Selector That Silently Counts Every Sibling Instead of Just the Ones Matchin...

CSS: Fix a :nth-child() Selector That Silently Counts Every Sibling Instead of Just the Ones Matchin...

CSS: Master Z-Index for Stack Order

CSS: Master Z-Index for Stack Order

Views: 7
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

The AI Prompt That Checks a Contractor Estimate for Hidden Fees
SQL Server: Fix a Recursive CTE Hitting the Default 100-Level Cap

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com