CSS Container Queries Not Working Fix
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-sizeon parent elements - Use
container-namefor explicit targeting - Provide
@mediafallbacks for browsers without container query support - Test with Chrome DevTools Container Queries panel
- Avoid
container-type: sizeunless you specifically need height queries
Common Mistakes with container queries
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro