Closed Bug 1958511 Opened 1 year ago Closed 1 month ago

CSS custom properties aren't inherited from :root into @page rules (but they are in Chromium)

Categories

(Core :: Layout, defect)

Firefox 137
defect

Tracking

()

RESOLVED FIXED
161 Branch
Tracking Status
firefox161 --- fixed

People

(Reporter: christian+bugzilla, Assigned: emilio)

References

Details

Attachments

(1 file, 1 external file)

User Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:137.0) Gecko/20100101 Firefox/137.0

Steps to reproduce:

I have the following CSS, with the intention of making the margin on printed pages variable with a CSS property:

:root {
-margin: 5cm;
}

@page {
margin: var(--margin);
}

I've got a live demo at https://firefox-print-page-margin.think.somethingorotherwhatever.com.

Actual results:

It seems that inside the @page query, properties aren't inherited from :root, so the margin rule isn't valid and Firefox ends up rendering the page with zero margin when printed.

Expected results:

The --margin property should be defined within the @page query, and the printed page should have a margin of 5cm on each side.

Chrome does this correctly.

The Bugbug bot thinks this bug should belong to the 'Core::Layout' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: Untriaged → Layout
Product: Firefox → Core

The severity field is not set for this bug.
:jwatt, could you have a look please?

For more information, please visit BugBot documentation.

Flags: needinfo?(jwatt)

The --margin property should be defined within the @page query, and the printed page should have a margin of 5cm on each side.

Seems like a chrome bug? Why should a page inherit from the root element? The @page things are descriptors, not properties.

(In reply to Emilio Cobos Álvarez (:emilio) from comment #3)

Seems like a chrome bug? Why should a page inherit from the root element? The @page things are descriptors, not properties.

Agreed, I think Firefox is correct on this (from an inheritance perspective) -- @page rules aren't targeting any particular element where inheritance would be a thing that could happen (whether from :root or anywhere else).

Having said that: the screenshot does show another weird thing, which is that the page box is squished into the top left corner on the sheet. I filed bug 1963868 on that.

Severity: -- → S4
Flags: needinfo?(jwatt)
Summary: CSS custom properties aren't inherited from :root in @page blocks → CSS custom properties aren't inherited from :root into @page rules (but they are in Chromium)

The reasoning for not inheriting from the root element makes sense.

So is there a way of setting a custom CSS property from JavaScript which can be used in @page, or is that just impossible?

I think custom properties right now "work" on @page, so inserting a @page { --foo: bar; } should work?

Duplicate of this bug: 2085662

Hey there,

I reported the duplicated bug 2085662, so I'll take the opportunity that this one is still open to argue for supporting global :root custom properties in @page, similarly to Chrome-based browsers.

First, as I mentioned in the other bug report, global custom properties are available in other at-rule sections, such as @media print. See the example I provided with bug 2085662. In this regard, not supporting them in the @page at-rule seems like an exception.

But the main argument is about re-usability. According to MDN:

Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that represent specific values to be reused throughout a document.

Defining a global variable that can be used both when rendering the page in the browser and when printing it is the very definition of re-usability.

I'm going to take a simple example from a website I manage, One-Pager Chords, where the goal is to display guitar chords that fit on one page.

When rendering the one-pager in the browser (let's say What's Up? - 4 Non Blondes), a simple div with border, shadow and padding reproduces the visual effect of a paper page. When printing the page, the div border, shadow and padding are not needed anymore, but the @page itself requires a margin.

I might want to use a global variable that specifies the length (e.g. --page-margin: 0.5in) for both:

  • the div padding when browsing the page
  • the @page margin when printing the page

Hopefully that helps fueling the debate. At the end of the day, I guess it would be best if the W3C specification would offer a way to settle it.

First, as I mentioned in the other bug report, global custom properties are available in other at-rule sections, such as @media print. See the example I provided with bug 2085662. In this regard, not supporting them in the @page at-rule seems like an exception.

It's not an exception. It works in style rules inside @media, not in @media itself, fwiw.

But I just looked and the spec does specify this. It's a bit unclear but given we expose already custom properties in @page{} itself we might as well I guess...

Matches the spec and in some way we already have this dependency, since
we allow e.g. rem units inside @page.

This fixes a few pre-existing issues with rem units, because we were not
waiting for the root element to be styled. We query the page style
before the root is styled for the initial frame constructor but then
re-resolve it properly. It'd be nice to make that more explicit but not
today...

Assignee: nobody → emilio
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Pushed by ealvarez@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/2093e3a470ca https://hg.mozilla.org/integration/autoland/rev/1303a1977629 Make @page inherit from the root element. r=layout-reviewers,firefox-style-system-reviewers,dshin
Status: ASSIGNED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 161 Branch

Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/64286 for changes under testing/web-platform/tests

Upstream PR merged by moz-wptsync-bot

QA Whiteboard: [qa-triage-done-c162/b161]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: