Skip to content

calc() expressions in @media rule (FF59, CR66, SF12) #12

@WebMechanic

Description

@WebMechanic

So I like using tests for actual standards feature support and then nest simple hacks with @support or @media to target specific engines or brands - or vice versa as it seems fit.

According to its CSS spec:

calc() can be used wherever <length>, <frequency>, <angle>, <time>, <percentage>, <number>, <integer> values are allowed.

@media (min-width:calc(1 * 1px)) {
}

MDN's @media page lists "calc() expressions" support for Blink starting with Chromium 66 (2018-04) and in Safari/12 (2018-09).
https://caniuse.com/#search=calc()

Mozilla presumably implemented this when they switched to their shiny new "Quantum CSS" (Servo) in 2017 (FF57+ IIRC).
However, Firefox 59 was the first public release to allow this:

It should work with other properties than "min-width" that take numerals as their value, and there's probably some @media combination with a Firefox-only or Chromium-only (-webkit-*) filter to include/exclude supporting browsers.

Usage example:

Leads to a more readable and certainly precise value. Using var() for the values will not work here, presumably as it may cause inheritance issues.

@media (min-width:30rem) and (max-width:calc(48rem - 1px)) {
  /* small screen*/
}
@media (min-width:48rem) and (max-width:calc(64rem - 1px)) {
  /* not so small screen*/
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions