w3c / csswg-drafts

CSS Working Group Editor Drafts
https://drafts.csswg.org/
Other
4.36k stars 641 forks source link

[mediaqueries-5] Introduce `@size` rule for defining reusable media query size values #10487

Open brandonmcconnell opened 2 weeks ago

brandonmcconnell commented 2 weeks ago

Abstract

This proposal introduces the @size rule, a new CSS at-rule, that allows authors to define variable sizes which can be referenced in @media, @custom-media, and @container queries alike. This feature offers an easier and more maintainable method to manage responsive design breakpoints across CSS files.

**Related proposal:** [[mediaqueries-5] [css-env] Replace @custom-media with custom environment variables? #6698](https://github.com/w3c/csswg-drafts/issues/6698) ☝🏼 this proposal shares a similar goal but uses a different approach

Motivation

Responsive design often involves repeatedly using the same viewport breakpoints across multiple media queries. This can lead to cumbersome, error-prone code and make global updates to breakpoints tedious. The @size rule aims to solve this by allowing developers to define and reuse named sizes that can be referenced consistently across all media queries.

The introduction of @custom-media aids in this effort as well, but it stores an entire query, where even in the definition of a @custom-media query, the use of a reusable variable value would prove useful, as pointed out by @adamwathan on X here:

It's a shame to me that the @​custom-media spec offers no way of removing the duplicate 500px value in this sort of situation like you'd expect to be able to do with a plain old variable 😕 Adam Wathan

Syntax / Example

Defining Sizes

The @size at-rule allows developers to declare named sizes using standard CSS units:

@size {
  --small: 480px;
  --medium: 768px;
  --large: 1024px;
  --extra-large: 1440px;
}

Usage in @media and @container Queries

The named sizes can be used in @media, @custom-media, @container queries:

@media (min-width: size(--small)) {
  /* Styles for small and above */
}

@media (width > size(--medium)) and (width <= size(--large)) {
  /* Styles for medium to large */
}

@custom-media --custom-medium (width > size(--medium));
@media (--custom-medium) {
  /* Styles using custom media query */
}

@container some-container (min-width: size(--small)) {
  /* Styles for small and above */
}

Specification

Grammar

The @size rule must contain a block of rules, each defining a single named size. Each named size must be a valid CSS identifier followed by a colon, a valid CSS length (e.g., px, em, rem, %) or viewport width/height (e.g., vw, vh), and a semicolon.

Semantics

Benefits

Specification Considerations

Implementation Considerations

Conclusion

The @size rule introduces a method for defining reusable responsive design breakpoints. By enabling the reuse of named sizes across @media, @custom-media, and @container queries, this proposal aims to simplify the process of maintaining consistent, flexible, and readable responsive styles.

brandonmcconnell commented 2 weeks ago

Another approach could be to add support for any @property-defined values inside queries. That would require next to no new actual language changes, but it comes with the caveat that queries would not inherit any changes to those values and could only use their initial values, which may be more confusing to users.

In that case, such @property values would need to somehow be definable constant to be meaningful and able to be used in different places meaning the same thing.

Because of this point, an alternative to @property like @size may be best after all.

tabatkins commented 2 weeks ago

The problem you're solving is just getting a variable that's useful in a "global" sense, yeah? There doesn't appear to be anything size-specific in this proposal; it's just doing substitution, and you happen to only be using that substitution in places that expect a size, but the same functionality could be used for any value.

So this is a dupe of #2627 I think.

brandonmcconnell commented 2 weeks ago

@tabatkins The idea here was to make these variables size-specific and limited to lengths, but custom environment variables would take it one step further. I'm definitely a fan of #2627 if that passes. 💯

Crissov commented 2 weeks ago

While size usually consists of width and height, the proposal currently only considers the former.

There is plenty of preexisting content that would allow to identify commonly used terms for breakpoints (as in the example above), so you would not need custom idents but could introduce keywords that authors could set to individual values then, without a need for size():

@size {
  small: 480px 640px;
  medium: 768px 576px;
  large: 1024px 720px;
  extra-large: 1440px 1080px;
}

@media (min-width: small) {
  /* Styles for small and above */
}

@media (width > medium) and (height < large) {
  /* Styles for medium to large */
}