Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
CSS

CSS: Fix Container Queries Never Triggering Because the Parent Element Was Never Given an Explicit container-type

- 01.11.26 - ErcanOPAK

🎨 Fix Container Queries Never Triggering Because the Parent Element Was Never Given an Explicit container-type

A container query lets a component adapt its own layout based on the size of its nearest ancestor rather than the viewport, which is exactly what makes it useful for a component that gets reused in a wide sidebar in one layout and a narrow card in another, but the browser only establishes size containment, and therefore only evaluates any @container rule against it, for an ancestor that was explicitly declared a query container with container-type. Writing an @container rule against a parent that looks like it should obviously qualify, because it visually wraps the component and clearly has a size, does nothing at all if that parent’s container-type was never set, and the browser gives no warning or console error – the rule is simply never evaluated, so the component renders using whatever styles apply outside the query.

🔎 The Problem

/* Component meant to adapt based on its wrapping card's width */
.card {
  /* looks like an obvious container - it visually wraps everything -
     but nothing here actually declares it as a query container */
  padding: 16px;
  border: 1px solid #ddd;
}

.card-title {
  font-size: 1rem;
}

@container (min-width: 400px) {
  .card-title {
    font-size: 1.5rem;   /* this rule NEVER applies - .card was never
                             given container-type, so no containment
                             context exists for @container to query */
  }
}

✅ Fix: Declare container-type on the Element You’re Actually Querying Against

  • Add container-type: inline-size (or size, if block-axis containment is also needed) to the exact ancestor the @container rule is meant to query, and give it a container-name if more than one ancestor in the chain needs to act as a container, so each @container rule can target the right one explicitly.
  • Remember that container-type establishes a new containment context, which changes how that element’s own size can be computed – an element sized purely by its content in the axis being contained may need an explicit width or a fallback, since containment can prevent it from growing to fit content in that axis.
  • Check actual browser support for the specific container-type value and query features being used before shipping container queries as the only layout mechanism, and provide a reasonable default style outside any @container block for browsers where containment silently doesn’t apply.

🚨 Why This Is Dangerous

  • A component that depends entirely on container queries for its responsive behavior, with no sensible default styling outside any @container block, can render identically broken in every context if the containment setup is wrong, and because there’s no console warning, the first sign of the problem is simply that the responsive behavior never happens.

@container doesn’t search upward for whichever ancestor looks big enough to qualify – it only ever sees the nearest ancestor someone explicitly turned into a container, and an un-declared parent is invisible to it no matter how perfectly sized it looks.

— UI Engineer

Related posts:

CSS: Use Aspect Ratio for Perfectly Sized Boxes

CSS: Use Aspect Ratio for Perfectly Sized Boxes

Hide element in CSS with Display and Visibility

Hide element in CSS with Display and Visibility

CSS: Fix Elements That Ignore max-width Because of a Missing box-sizing Reset

CSS: Fix Elements That Ignore max-width Because of a Missing box-sizing Reset

Views: 11

Post navigation

Ajax: Fix an AbortController Signal Silently Not Canceling a Fetch Request Already Served From the Browser’s Disk Cache
Ajax: Fix a fetch() Upload Silently Stalling at 99% Because the Request Body Is a Stream the Server Never Fully Drains

Leave a Reply Cancel reply

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

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (608)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

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