Course contentsShow
← Web Development
Lesson 433 of 2,9827. Responsive DesignFree lesson

Container Query Syntax

Writing @container rules to apply styles based on a container's dimensions using query conditions.

Container Query Syntax

You'll learn: How to write @container rules that apply styles when a container meets specific size conditions.

Writing Container Query Rules

Once you've set up a container context (using container-type or container-name), you write container queries using the @container at-rule. The syntax mirrors media queries but checks the container's dimensions instead of the viewport's.

The basic structure looks like this:

@container (min-width: 400px) {
  /* Styles applied when container is at least 400px wide */
  .card {
    display: flex;
  }
}

Query Conditions

You can use familiar query conditions:

/* Minimum width */
@container (min-width: 500px) {
  .card-title { font-size: 1.5rem; }
}

/* Maximum width */
@container (max-width: 300px) {
  .card-image { display: none; }
}

/* Exact width */
@container (width: 600px) {
  .card { border: 2px solid blue; }
}

Combining Conditions

Just like media queries, you can combine conditions with and:

@container (min-width: 400px) and (max-width: 800px) {
  .card {
    padding: 1.5rem;
  }
}

Named Container Queries

If you named your container, target it specifically:

@container sidebar (min-width: 250px) {
  .widget { font-size: 0.9rem; }
}

Think of @container as asking: "Is my container this size?" instead of "Is the viewport this size?" This lets components adapt based on their actual available space.

Key Takeaway: Container queries use @container (condition) blocks to apply styles when a container meets width, height, or combined dimension requirements—enabling truly modular, self-responsive components.