Skip to content

CSS Container Queries Not Working Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about CSS Container Queries Not Working Fix. We cover key concepts, practical examples, and best practices.

The Problem

CSS container queries allow styling elements based on their container's size instead of the viewport. Common failures include missing container-type declaration, using containment that prevents layout, or query expressions that do not match the container's actual dimensions.

Quick Fix

Step 1: Declare the container

/* Wrong — no container context */
.card {
    background: white;
}

/* Right — establish containment */
.card-container {
    container-type: inline-size;
    container-name: card;
}

.card {
    background: white;
}

@container card (min-width: 400px) {
    .card {
        display: grid;
        grid-template-columns: 1fr 1fr;
    }
}

Step 2: Use container-type correctly

/* Wrong — size containment prevents query */
.wrapper {
    container-type: size; /* Blocks query on inline-size */
}

/* Right — use inline-size for width-based queries */
.wrapper {
    container-type: inline-size;
}

Step 3: Match the query to the container context

/* Wrong — query does not match any container */
@container (max-width: 600px) {
    .element { font-size: 14px; }
}

/* Right — the nearest ancestor with containment is used */
.card-grid {
    container-type: inline-size;
}

@container (max-width: 600px) {
    .card { flex-direction: column; }
}

Step 4: Use container-name for specificity

.card-container {
    container-type: inline-size;
    container-name: sidebar;
}

.main-container {
    container-type: inline-size;
    container-name: main;
}

/* Wrong — query applies to any container */
@container (min-width: 500px) {
    .card { padding: 2rem; }
}

/* Right — targets only the sidebar container */
@container sidebar (min-width: 500px) {
    .card { padding: 1rem; }
}

@container main (min-width: 500px) {
    .card { padding: 2rem; }
}

Step 5: Fallback for unsupported browsers

/* Wrong — no fallback */
.card { background: lightblue; }

@container (min-width: 400px) {
    .card { background: coral; }
}

/* Right — provide viewport-based fallback */
.card { background: lightblue; }

@media (min-width: 768px) {
    .card { background: coral; }
}

@container (min-width: 400px) {
    .card { background: slateblue; }
}

Prevention

  • Always set container-type: inline-size on parent elements
  • Use container-name for explicit targeting
  • Provide @media fallbacks for browsers without container query support
  • Test with Chrome DevTools Container Queries panel
  • Avoid container-type: size unless you specifically need height queries

Common Mistakes with container queries

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What browsers support container queries?

Container queries are supported in Chrome 105+, Firefox 110+, Safari 16+, and Edge 105+. For older browsers, use @media fallbacks. CanIUse provides up-to-date support details.

Can I query height with container queries?

Yes, use container-type: size (which enables both width and height queries) or container-type: block-size for height-only queries. Note that size containment also affects layout — use it carefully.

What is the difference between container-type: inline-size and size?

inline-size only establishes a containment context for the inline axis (width in horizontal writing modes). It does not block layout. size establishes containment for both axes, which may affect how the container sizes itself.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro