Color scheme

Containers, Flex, and Sticky

This reading covers three features that every advanced CSS developer encounters regularly: container queries, which let components respond to their own context rather than the viewport; the flex sizing triangle, which controls how flex items grow, shrink, and establish their starting size; and sticky positioning, which is deceptively simple until an invisible ancestor breaks it. Each topic stands on its own but together they represent the kind of nuanced layout knowledge that separates polished work from brittle work.

Container Queries

Media queries answer the question "how big is the viewport?" Container queries answer a more useful question: "how big is the element's containing box?" That distinction matters enormously for reusable components. A card that appears in a narrow sidebar and again in a wide main column has no way to respond to its local context using media queries alone. With container queries, the card can adapt to both placements without any outside help.

Setting Up a Container

Before any @container rule can fire, you must declare a containment context on the element that will act as the reference box. The container-type property does this. The two values you will use in practice are inline-size, which establishes a containment context on the inline axis only, and size, which includes both axes. Nearly every real-world use case needs only inline-size, since components typically respond to their available width, not their available height.


        .card-wrapper {
            container-type: inline-size;
        }
    

You can also assign a name to the container with container-name, or use the shorthand container. Naming is optional when a component has only one ancestor to query, but becomes necessary when you want to query a specific ancestor rather than the nearest one.


        /* Shorthand: name / type */
        .sidebar {
            container: sidebar / inline-size;
        }

        .main-content {
            container: main / inline-size;
        }
    

Writing Container Query Rules

Once a container is established, descendants can query it with @container. The syntax mirrors @media almost exactly. The key difference is that the condition tests the container's size, not the viewport's.


        /* Targets the nearest container ancestor */
        @container (min-width: 400px) {
            .card {
                display: grid;
                grid-template-columns: auto 1fr;
            }
        }

        /* Targets a named container specifically */
        @container sidebar (max-width: 300px) {
            .card {
                flex-direction: column;
            }
        }
    

Container queries can be nested, combined with media queries, and used alongside every other CSS feature. They do not replace media queries; they complement them. A good rule of thumb is to use media queries for page-level layout decisions and container queries for component-level decisions.

280px
◆

The Sermon on the Mount

Christ's foundational teachings on discipleship, covering the Beatitudes, the higher law, and how to live in God's kingdom.

Drag the slider to narrow or widen the container. Below 420px the card stacks vertically; at 420px and above it switches to a horizontal layout. The viewport width never changes, only the container width does.

Container queries have excellent browser support as of 2026. The Container Query Units and Style Queries choose topic goes deeper into container query length units like cqi and cqb, which let you size elements relative to the container rather than the viewport.

Flex Grow, Shrink, and Basis

The flex shorthand packs three distinct concepts into one declaration. Most developers learn flex: 1 and move on, which works until it does not. Understanding what each value actually controls lets you build flex layouts that behave predictably at any size.

The Three Values

flex-basis sets the starting size of a flex item before any growing or shrinking occurs. Think of it as the item's preferred size. When set to auto, the item uses its own width or height as its basis. When set to 0, the item starts from nothing and grows entirely from the available free space.

flex-grow is a unitless ratio that controls how much of the positive free space an item receives. If the container has leftover space after all items reach their basis size, that space is divided among items proportionally by their flex-grow values. An item with flex-grow: 2 receives twice as much extra space as one with flex-grow: 1. An item with flex-grow: 0 receives none.

flex-shrink is the mirror image: a unitless ratio that controls how much an item gives up when the container runs out of space. Items with a higher shrink factor compress more than their siblings. Setting flex-shrink: 0 prevents an item from compressing at all, which is useful for fixed-width sidebars or icons that should never collapse.


        /* The shorthand: grow / shrink / basis */
        .item { flex: 1 1 auto; }  /* grow, shrink, auto basis */
        .item { flex: 1; }         /* same as flex: 1 1 0        */
        .item { flex: auto; }      /* same as flex: 1 1 auto     */
        .item { flex: none; }      /* same as flex: 0 0 auto     */
    

Notice that flex: 1 sets basis to 0, not auto. This means items distribute all available space proportionally from zero, ignoring their content size. flex: auto uses the item's content width as the starting point and then distributes leftover space. The difference becomes visible when items have unequal content lengths.

A
Short
B
Medium length
C
Considerably longer content here

The min-width: 0 Problem

Flex items have a hidden default: min-width: auto. This means a flex item will never shrink smaller than its content's minimum size, even when flex-shrink says it should. The result is an item that overflows its container instead of compressing.

The fix is a single declaration: min-width: 0. This overrides the default and allows the item to shrink as far as the flex algorithm needs. It is most commonly needed on items containing long words, code blocks, images, or other content with a significant minimum size. The min-width: 0 on Flex Items choose topic explores this behavior in detail with additional examples.


        /* Without this, a flex item with long content may overflow */
        .flex-item {
            flex: 1;
            min-width: 0; /* allow the item to shrink past its content */
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
    

Sticky Positioning Gotchas

position: sticky is one of those CSS features that looks simple and then silently fails in production. The element sticks during scroll exactly as expected in isolation, then stops working the moment you put it inside a real layout. Two issues cause nearly every sticky failure: a missing offset value and an ancestor with the wrong overflow setting.

Sticky Requires an Offset

position: sticky without a threshold value does nothing. The browser needs to know where to stick the element: how far from the top, bottom, left, or right edge of the scrolling container before the element locks into place. Without at least one of top, bottom, left, or right, the element participates in normal flow and never sticks.


        /* Does nothing - no threshold defined */
        .sticky-header {
            position: sticky;
        }

        /* Sticks 0px from the top of the scroll container */
        .sticky-header {
            position: sticky;
            top: 0;
        }

        /* Sticks with a gap - useful when a fixed site nav sits above */
        .sticky-header {
            position: sticky;
            top: 4rem;
        }
    

Overflow Kills Sticky

This is the more insidious problem. If any ancestor of your sticky element has overflow set to anything other than visible (including hidden, auto, scroll, or clip), that ancestor becomes the sticky element's scroll container. If that ancestor does not scroll, the sticky element has nowhere to move and appears completely static.

The difficulty is that the culprit is often several levels up the DOM and was added for unrelated reasons: a clearfix hack, an attempt to prevent margin collapse, or a layout wrapper that needed to clip an overflowing decoration. Finding it requires inspecting every ancestor between the sticky element and the document root.

Wrapper: overflow: visible (working)

Scroll down inside this box. The header above should stay pinned to the top as long as the wrapper has no overflow constraint.

The sticky element needs a scrolling ancestor to work against. That ancestor is the nearest parent with a defined scroll context, which here is the dashed border box above.

Toggle the switch above to add overflow: hidden to the wrapper div that sits between the sticky element and the scrolling container.

When that happens, the wrapper becomes the sticky element's scroll container. But the wrapper does not scroll, so the sticky element has nowhere to move and stops working entirely.

This is the overflow gotcha. The fix is to remove overflow: hidden from that intermediate ancestor, or find another way to achieve whatever the overflow was doing.

Common culprits in real projects: a clearfix wrapper, a card with overflow: hidden for border radius clipping, or a layout container that hides a decorative background bleed.

Scroll inside the demo box, then toggle the switch and try again. The sticky header locks to the top in the working state; adding overflow: hidden to the intermediate wrapper makes it scroll away with the content as if position: sticky were not there at all.

Diagnosing a Broken Sticky Element

Open DevTools, select the sticky element, and walk up the DOM tree inspecting each ancestor. Look for any overflow value other than visible. The first ancestor you find with such a value is your scroll container, and if it does not scroll, your sticky element will not work. Remove or change that overflow declaration to fix it.

Key Concepts Summary

Container queries decouple component layout from viewport size by establishing a containment context with container-type and querying it with @container. This makes components genuinely portable across different layout contexts without requiring external coordination.

The flex sizing model works in three stages: flex-basis sets the starting size, flex-grow distributes positive free space, and flex-shrink absorbs negative free space. The default min-width: auto on flex items prevents shrinking past content size, which causes overflow when content is long; min-width: 0 is the fix.

Sticky positioning requires both position: sticky and a threshold value (top, bottom, left, or right). Any ancestor with a non-visible overflow value becomes the sticky element's scroll container; if that ancestor does not scroll, the sticky behavior silently fails.