container-type and container-name
Define query containers using container-type and give them names with container-name.
container-type and container-name is a free CSS Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are Container Queries?
Container queries allow CSS to apply styles based on the size of a parent container rather than the viewport. This enables truly component-based responsive design where a component adapts to wherever it is placed.
The Problem Container Queries Solve
With media queries, a card component in a 300px sidebar cannot know it is small — it only knows the viewport is wide. Container queries let the component react to its own container's size, not the global viewport.
container-type
To query a container, first declare it as a query container using container-type:
.card-wrapper {
container-type: inline-size;
/* enables width-based container queries */
}container-type Values
Available values:
inline-size— enables queries on the container's inline dimension (width in horizontal writing)size— enables queries on both inline and block dimensions (width and height)normal— default, no size queries but style queries still possible
container-name
Give a container a name to reference it in queries. This is necessary when nested containers exist and you want to query a specific ancestor:
.sidebar {
container-type: inline-size;
container-name: sidebar;
}container Shorthand
The container shorthand sets both name and type:
.card-grid {
container: card-grid / inline-size;
/* name: card-grid, type: inline-size */
}Why Unnamed Containers
An unnamed container can still be queried — the @container rule will target the nearest container ancestor by default. Names are only needed when you want to skip to a specific ancestor.
Performance Consideration
Every container-type element gets its own layout containment. This means size changes inside the container do not affect ancestors — a performance benefit. The browser does not need to recalculate the entire page when the container changes.
Contain Property Relationship
Setting container-type: inline-size implicitly sets contain: inline-size layout style on the element. This prevents container size from affecting its own layout — required for size queries to work.
Practical Example Setup
A complete container query setup:
/* Define container */
.article-grid {
container: article-grid / inline-size;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 24px;
}
/* Query the container */
@container article-grid (min-width: 600px) {
.card { flex-direction: row; }
}Container Queries vs Media Queries
Use containers for: component-level adaptation, reusable components in different layout contexts, cards/widgets in unknown placements. Use media queries for: global layout changes, typography scaling, show/hide large sections.
Quick Check
Which property value makes an element queryable for its width using @container rules?
Recap
container-type: inline-size defines an element as a query container for its width. Use container-name to name containers for selective targeting. The container shorthand sets both. Container queries solve the component-in-unknown-context problem that viewport media queries cannot handle.
Frequently asked questions
Is the “container-type and container-name” lesson free?
Yes — the full text of “container-type and container-name” is free to read here on the web, and the CSS Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO.
What will I learn in “container-type and container-name”?
Define query containers using container-type and give them names with container-name. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CSS Academy?
No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “container-type and container-name” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CSS Academy lesson?
Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- container-type and container-name
- @container Rules and Breakpoints
- Container Query Units: cqw cqh
- Container Queries vs Media Queries