English

Explore CSS Container Style Queries, a powerful approach to responsive design that allows components to adapt based on their container's styling, not just viewport size. Learn practical applications for diverse global websites.

CSS Container Style Queries: Style-Based Responsive Design for Global Applications

Traditional responsive design relies heavily on media queries, adapting a website's layout and styles based on the viewport size. While effective, this approach can lead to inconsistencies and difficulties when dealing with complex components that need to adapt to different contexts within the same viewport. CSS Container Style Queries offer a more granular and intuitive solution, allowing elements to respond to the styling applied to their containing element, offering truly component-based responsive behavior.

What are CSS Container Style Queries?

Container Style Queries extend the power of container queries beyond simple size-based conditions. Instead of checking the width or height of a container, they allow you to check for the presence of specific CSS properties and values applied to that container. This enables components to adapt their styling based on the container's context, rather than just its dimensions.

Think of it this way: instead of asking "Is the viewport wider than 768px?", you can ask "Does this container have the --theme: dark; custom property set?". This opens up a whole new world of possibilities for creating flexible and reusable components that can seamlessly adapt to different themes, layouts, or branding variations throughout your website or application.

Benefits of Container Style Queries

How to Use CSS Container Style Queries

Here's a breakdown of how to implement container style queries:

1. Setting Up the Container

First, you need to designate an element as a container. You can do this using the container-type property:

.container {
  container-type: inline-size;
}

The inline-size value is the most common and useful, as it allows the container to query its inline (horizontal) size. You can also use size which queries both inline and block size. Using only size might have performance implications if you are not careful.

Alternatively, use container-type: style to use a container only for style queries, and not size queries, or container-type: size style to use both. To control the container name, use container-name: my-container and then target it with @container my-container (...).

2. Defining Style Queries

Now, you can use the @container style() at-rule to define styles that apply when a specific condition is met:

@container style(--theme: dark) {
  .component {
    background-color: #333;
    color: #fff;
  }
}

In this example, the styles within the @container rule will only be applied to the .component element if its containing element has the --theme custom property set to dark.

3. Applying Styles to the Container

Finally, you need to apply the CSS properties that your style queries are checking for to the container element:

<div class="container" style="--theme: dark;">
  <div class="component">This is a component. </div>
</div>

In this example, the .component will have a dark background and white text because its container has the --theme: dark; style applied directly in the HTML (for simplicity). Best practice is to apply styles via CSS classes. You can also use JavaScript to dynamically change the styles on the container, triggering style query updates.

Practical Examples for Global Applications

1. Themed Components

Imagine a website that supports multiple themes. You can use container style queries to automatically adjust the styling of components based on the active theme.

/* CSS */
.app-container {
  --theme: light;
}

@container style(--theme: dark) {
  .card {
    background-color: #333;
    color: #fff;
  }
}

@container style(--theme: light) {
  .card {
    background-color: #f0f0f0;
    color: #333;
  }
}

/* HTML */
<div class="app-container" style="--theme: dark;">
  <div class="card">
    <h2>Card Title</h2>
    <p>Card content.</p>
  </div>
</div>

In this example, the .card component will automatically switch between a dark and light theme based on the --theme property of its container. This is very beneficial for sites where users can choose different themes based on their preferences.

2. Layout Variations

You can use container style queries to create different layout variations for components based on the available space or the overall layout of the page. Consider a language selection component. In the main navigation, it might be a compact dropdown. Within the footer, it could be a full list of available languages.

/* CSS */
.navigation {
  --layout: compact;
}

.footer {
  --layout: expanded;
}

@container style(--layout: compact) {
  .language-selector {
    /* Styles for compact dropdown */
  }
}

@container style(--layout: expanded) {
  .language-selector {
    /* Styles for full list of languages */
  }
}

/* HTML */
<nav class="navigation" style="--layout: compact;">
  <div class="language-selector">...

<footer class="footer" style="--layout: expanded;">
  <div class="language-selector">...

This approach is valuable for websites catering to diverse user interfaces across different devices and platforms. Consider that mobile and desktop site structures often differ greatly, and this can help components adapt.

3. Adapting to Content Type

Consider a news website with article summaries. You can use container style queries to adjust the presentation of summaries based on whether they include an image or not.

/* CSS */
.article-summary {
  --has-image: false;
}

@container style(--has-image: true) {
  .article-summary {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 1rem;
  }
}

/* HTML (with image) */
<div class="article-summary" style="--has-image: true;">
  <img src="..." alt="..." />
  <div>...
</div>

/* HTML (without image) */
<div class="article-summary" style="--has-image: false;">
  <div>...
</div>

This enables a more visually appealing and informative presentation of article summaries, improving the user experience. Note that setting the `--has-image` property in HTML directly isn't ideal. A better approach would involve using JavaScript to detect the presence of an image and dynamically add or remove a class (e.g., `.has-image`) to the `.article-summary` element, and then setting the `--has-image` custom property within the CSS rule for the `.has-image` class.

4. Localized Styling

For international websites, container style queries can be used to adapt styles based on the language or region. For instance, you might want to use different font sizes or spacing for languages with longer text.

/* CSS */
.locale-container {
  --language: en;
}

@container style(--language: ja) {
  .text {
    font-size: 1.1em;
    letter-spacing: 0.05em;
  }
}

@container style(--language: ar) {
  .text {
    direction: rtl;
  }
}

/* HTML */
<div class="locale-container" style="--language: ja;">
  <p class="text">...

</div>

This allows for creating more tailored and user-friendly experiences for different language audiences. Consider that some languages such as Arabic and Hebrew are written right-to-left, and specific styles need to be applied. For Japanese and other East Asian languages, different spacing and font size may be needed to properly render characters.

5. Accessibility Considerations

Container style queries can also enhance accessibility by adapting component styles based on user preferences or device capabilities. For example, you can increase the contrast of a component if the user has enabled high contrast mode in their operating system.

/* CSS */
body {
  --high-contrast: false;
}

@media (prefers-contrast: more) {
  body {
    --high-contrast: true;
  }
}

@container style(--high-contrast: true) {
  .component {
    background-color: black;
    color: white;
  }
}

/* HTML */
<div class="component">...

This ensures that your website is usable and accessible to everyone, regardless of their abilities. Note the use of the `@media (prefers-contrast: more)` media query to detect high contrast mode at the operating system level, and then setting the `--high-contrast` custom property. This allows you to trigger style changes using a style query based on the user's system settings.

Best Practices

Browser Support

Container style queries have excellent browser support across modern browsers including Chrome, Firefox, Safari, and Edge. However, older browsers may not fully support this feature. Be sure to use feature queries to provide fallback styles for these browsers or use a polyfill.

Conclusion

CSS Container Style Queries offer a powerful and flexible approach to responsive design, allowing you to create truly component-based and adaptable websites and applications. By leveraging the styling of container elements, you can unlock a new level of control and granularity in your designs, resulting in more maintainable, scalable, and user-friendly experiences for a global audience.

Embrace container style queries to build responsive components that seamlessly adapt to various themes, layouts, languages, and accessibility requirements, creating a truly global web experience.

Resources