📖 Scenario: You are building a responsive card component that changes its style based on the size of its container, not just the viewport. This is useful for flexible layouts where cards can appear in different sized containers.
🎯 Goal: Create a Sass stylesheet that sets up a container query for a card component. The card should have a base style and a different background color when its container is at least 30rem wide.
📋 What You'll Learn
Use a CSS container class .card-container with the name card-container
Create a card class .card with base styles
Add a container query inside Sass that changes the card background color when the container width is at least 30rem
Use Sass nesting to organize the styles
💡 Why This Matters
🌍 Real World
Container queries let you build components that adapt to their container size, not just the screen size. This is useful in complex layouts like dashboards, widgets, or modular UI where components appear in different sized spaces.
💼 Career
Understanding container queries and Sass nesting is valuable for front-end developers working on modern responsive designs, improving user experience and maintainability.
Progress0 / 4 steps
1
Set up the base card styles
Create a Sass class called .card with these styles: padding: 1rem, border-radius: 0.5rem, and background-color: #f0f0f0.
SASS
Hint
Use the .card selector and add the styles inside curly braces.
2
Add container declaration
Create a .card-container class with container-type: inline-size and container-name: card-container.
SASS
Hint
Create the .card-container selector and add container-type: inline-size; and container-name: card-container; inside it.
3
Write the container query
Inside the .card class, add a container query using @container card-container (min-width: 30rem) that changes the background-color to #d0e6f7.
SASS
Hint
Use @container card-container (min-width: 30rem) inside .card and nest the style change.
4
Complete with responsive font size
Add inside the container query a style to set font-size: 1.25rem for the .card.
SASS
Hint
Inside the container query block, add font-size: 1.25rem; to increase text size.
Practice
(1/5)
1. What is the main purpose of using @container in Sass?
easy
A. To reset all container styles
B. To create animations for containers
C. To define variables inside containers
D. To apply styles based on the size of an element's container
Solution
Step 1: Understand container queries
Container queries allow styling elements depending on their container's size, not the viewport.
Step 2: Role of @container in Sass
The @container rule in Sass is used to write container queries clearly and apply styles conditionally based on container size.
Final Answer:
To apply styles based on the size of an element's container -> Option D
Quick Check:
Container queries = style by container size [OK]
Hint: Remember: @container styles depend on container size, not viewport [OK]
Common Mistakes:
Confusing container queries with media queries
Thinking @container creates animations
Using @container to define variables
2. Which CSS property must be set on a container element to enable container queries in Sass?
The condition inside @container must have a colon after the property name, e.g., max-width: 400px.
Step 2: Verify other options
The selector .card is valid, padding in rem is allowed, and max-width is a valid container query condition.
Final Answer:
Missing colon after max-width -> Option C
Quick Check:
Container query conditions need colons [OK]
Hint: Always use colon after property in container query conditions [OK]
Common Mistakes:
Omitting colon in container query conditions
Thinking max-width is invalid in container queries
Confusing units for padding
5. You want to style a .card component differently when its container is narrower than 300px or wider than 700px using Sass container queries. Which of the following is the correct way to write this?