Bird
Raised Fist0
SASSmarkup~10 mins

Container query preparation in SASS - Browser Rendering Trace

Choose your learning style10 modes available
Render Flow - Container query preparation
[Parse CSS selectors] -> [Identify container query rules] -> [Check container size] -> [Apply styles if conditions met] -> [Recalculate layout] -> [Paint changes]
The browser reads CSS, finds container queries, measures container sizes, applies styles if conditions match, then updates layout and paints the result.
Render Steps - 3 Steps
Code Added:<section class="card-container"> and <article class="card"> elements
Before

[ ] (empty page)
→
After

[card-container]
  [card]
    Title
    Some content here.
Adding HTML elements creates the basic structure visible on the page with text inside the card.
🔧 Browser Action:Builds DOM tree and renders default styles
Code Sample
A container query changes the card's background and padding when the container is at least 30rem wide.
SASS
<section class="card-container">
  <article class="card">
    <h2>Title</h2>
    <p>Some content here.</p>
  </article>
</section>
SASS
@container card-container (min-width: 30rem) {
  .card {
    background-color: lightblue;
    padding: 1rem;
  }
}

.card-container {
  container-type: inline-size;
  container-name: card-container;
  border: 1px solid #ccc;
  padding: 0.5rem;
}
Render Quiz - 3 Questions
Test your understanding
After applying step 2, what new behavior does the container have?
AIt changes background color automatically
BIt becomes measurable for container queries based on width
CIt hides its children elements
DIt becomes a flex container
Common Confusions - 2 Topics
Why doesn't my container query styles apply even though I wrote @container?
You must set container-type on the container element first. Without container-type, the browser won't measure the container size and won't apply container query styles (see render_step 2).
💡 Always add container-type on the container before using @container queries.
Why does changing container width not update styles immediately?
Container queries react to container size changes, but if the container-type is missing or incorrect, the browser won't detect size changes to trigger style updates (see render_step 2 and 3).
💡 Ensure container-type matches the dimension you want to query (inline-size for width).
Property Reference
PropertyValue AppliedEffectCommon Use
container-typeinline-sizeEnables container queries based on container widthSet on container element
@container(min-width: 30rem)Applies styles when container width is at least 30remWraps styles for responsive design
background-colorlightblueChanges background color visuallyStyle change inside container query
padding1remAdds space inside element edgesImproves spacing when condition met
Concept Snapshot
container-type enables container queries by making an element measurable. @container rules apply styles based on container size conditions. Common container-type values: inline-size (width), size (width & height). Container queries update styles responsively inside containers, not viewport. Remember to set container-type before using @container queries.

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

  1. Step 1: Understand container queries

    Container queries allow styling elements depending on their container's size, not the viewport.
  2. 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.
  3. Final Answer:

    To apply styles based on the size of an element's container -> Option D
  4. 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?
easy
A. container-type
B. container-size
C. container-query
D. container-style

Solution

  1. Step 1: Identify container query activation property

    To use container queries, the container must declare container-type to enable size-based queries.
  2. Step 2: Verify other options

    container-size, container-query, and container-style are not valid CSS properties for this purpose.
  3. Final Answer:

    container-type -> Option A
  4. Quick 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
A. Blue
B. Red
C. No background color
D. Green

Solution

  1. Step 1: Understand the container query condition

    The container query applies styles only if the container's width is at least 600px.
  2. 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.
  3. Step 3: Determine fallback style

    The .box outside the container query has background-color: red;, so this style applies.
  4. Final Answer:

    Red -> Option B
  5. Quick 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
A. Container queries cannot use max-width
B. Incorrect selector inside @container
C. Missing colon after max-width
D. Padding value should be in px, not rem

Solution

  1. Step 1: Check container query syntax

    The condition inside @container must have a colon after the property name, e.g., max-width: 400px.
  2. Step 2: Verify other options

    The selector .card is valid, padding in rem is allowed, and max-width is a valid container query condition.
  3. Final Answer:

    Missing colon after max-width -> Option C
  4. 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?
hard
A. @container (max-width: 300px), (min-width: 700px) { .card { font-size: 1.2rem; } }
B. @container (max-width: 300px) and (min-width: 700px) { .card { font-size: 1.2rem; } }
C. @container (max-width 300px), (min-width 700px) { .card { font-size: 1.2rem; } }
D. @container (width < 300px), (width > 700px) { .card { font-size: 1.2rem; } }

Solution

  1. Step 1: Understand container query syntax for multiple conditions

    Multiple conditions separated by commas mean OR logic. Use colons after properties.
  2. 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 >.
  3. Final Answer:

    @container (max-width: 300px), (min-width: 700px) { .card { font-size: 1.2rem; } } -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard