Container queries let you style elements based on their container size, not just the screen size. Preparing your Sass code helps you write these queries clearly and reuse styles easily.
Container query preparation in SASS
Introduction
Syntax
SASS
@container (min-width: 30rem) { // styles here }
Use
@container followed by a condition like (min-width: 30rem) to start a container query.Inside the block, write styles that apply when the container matches the condition.
Examples
.box when its container is at least 40rem wide.SASS
@container (min-width: 40rem) { .box { background-color: lightblue; } }
SASS
@container (max-width: 20rem) { .text { font-size: 0.8rem; } }
SASS
$container-breakpoint: 50rem; @container (min-width: #{$container-breakpoint}) { .card { padding: 2rem; } }
Sample Program
This example shows a container with a fixed width of 40rem. The .box inside changes its background color to light green when the container is at least 30rem wide.
SASS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Container Query Example</title> <style> .container { container-type: inline-size; border: 2px solid #333; padding: 1rem; width: 40rem; margin: 1rem auto; } .box { background-color: lightgray; padding: 1rem; transition: background-color 0.3s ease; } @container (min-width: 30rem) { .box { background-color: lightgreen; } } </style> </head> <body> <section class="container"> <div class="box">This box changes color based on container width.</div> </section> </body> </html>
Important Notes
Remember to set container-type on the container element to enable container queries.
Use rem units for breakpoints to keep sizes consistent with user settings.
Container queries work only in browsers that support them; check browser compatibility.
Summary
Container queries let you style elements based on their container size.
Use @container in Sass to write container queries clearly.
Set container-type on containers to activate container queries.
Practice
1. What is the main purpose of using
@container in Sass?easy
Solution
Step 1: Understand container queries
Container queries allow styling elements depending on their container's size, not the viewport.Step 2: Role of
The@containerin Sass@containerrule 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 DQuick 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?
easy
Solution
Step 1: Identify container query activation property
To use container queries, the container must declarecontainer-typeto enable size-based queries.Step 2: Verify other options
container-size,container-query, andcontainer-styleare not valid CSS properties for this purpose.Final Answer:
container-type -> Option AQuick Check:
Enable container queries = container-type [OK]
Hint: Set container-type to activate container queries on an element [OK]
Common Mistakes:
- Using non-existent properties like container-query
- Confusing container-type with container-size
- Forgetting to set container-type on the container
3. Given the Sass code below, what background color will the
.box have if its container width is 500px?
@container (min-width: 600px) {
.box {
background-color: blue;
}
}
.box {
background-color: red;
}medium
Solution
Step 1: Understand the container query condition
The container query applies styles only if the container's width is at least 600px.Step 2: Check container width against condition
The container width is 500px, which is less than 600px, so the container query block does not apply.Step 3: Determine fallback style
The.boxoutside the container query hasbackground-color: red;, so this style applies.Final Answer:
Red -> Option BQuick Check:
Container width 500px < 600px = red background [OK]
Hint: Check container width against min-width condition to pick color [OK]
Common Mistakes:
- Assuming container query applies regardless of container size
- Confusing container width with viewport width
- Ignoring fallback styles outside @container
4. Identify the error in the following Sass container query code:
@container (max-width 400px) {
.card {
padding: 1rem;
}
}medium
Solution
Step 1: Check container query syntax
The condition inside @container must have a colon after the property name, e.g.,max-width: 400px.Step 2: Verify other options
The selector.cardis valid, padding in rem is allowed, and max-width is a valid container query condition.Final Answer:
Missing colon after max-width -> Option CQuick 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?hard
Solution
Step 1: Understand container query syntax for multiple conditions
Multiple conditions separated by commas mean OR logic. Use colons after properties.Step 2: Analyze each option
@container (max-width: 300px), (min-width: 700px) { .card { font-size: 1.2rem; } } correctly uses commas and colons. @container (max-width: 300px) and (min-width: 700px) { .card { font-size: 1.2rem; } } uses AND which requires both conditions simultaneously (impossible here). @container (max-width 300px), (min-width 700px) { .card { font-size: 1.2rem; } } misses colons. @container (width < 300px), (width > 700px) { .card { font-size: 1.2rem; } } uses invalid syntax with < and >.Final Answer:
@container (max-width: 300px), (min-width: 700px) { .card { font-size: 1.2rem; } } -> Option AQuick Check:
Use commas for OR, colons after properties [OK]
Hint: Use commas for OR conditions and colons after properties [OK]
Common Mistakes:
- Using 'and' instead of commas for OR conditions
- Omitting colons in conditions
- Using invalid operators like < or > in container queries
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
