Skip to content

CssConfig extension: scoped CSS + CSS inlining as one config surface #36

Open

Description

@JuroOravec

Merges two roadmap entries into one config surface: Scoped CSS
(docs/design/extensions_roadmap.md section 2, upstream
django-components #1230)
and Inline CSS (roadmap section 3, upstream
django-components #1147).
The two features interact semantically (what "inline my CSS" means depends
on whether the CSS is scoped), so they should present one mental model to
the user even though they can ship in stages.

What each feature is

  • Scoped: isolate a component's CSS to the elements it renders (the Vue
    model): stamp a per-component data attribute on the component's own
    elements (stopping at nested component boundaries, with the Vue rule for
    in-template slot fills), rewrite each top-level selector to require that
    attribute. Changes the reach of the CSS.
  • Inline: apply the collected CSS to the rendered HTML as style="..."
    attributes on matching elements (the
    css_inline sense: walk the
    HTML, match every rule, write the declarations onto matching tags).
    Changes the delivery of the CSS, not its meaning. Primary use cases:
    HTML emails (clients strip <style>), embeddable fragments/widgets
    injected into pages citry does not control, CSP contexts that forbid
    style tags.

Note: the roadmap's Inline CSS row currently describes it as inlining "into
the page (a <style> block)", which is not what upstream
#1147
tracks; correct the row when the roadmap is next touched.

The semantics, settled by one principle

Inlining is a delivery-mechanism change and must not change which
elements the CSS affects.
From that, the combination matrix follows:

scoped inline Behavior
off off Today's default: the component's CSS is a page-global stylesheet
on off Selectors rewritten + attributes stamped; still a <style> tag, but rules only match the component's own elements
off on The CSS is page-global, so faithful inlining applies it page-wide: every matching element in the rendered tree gets the declarations, including elements deeper in child components and outside the component entirely
on on Rules only match the component's scope-marked elements, so inlining lands exactly on the component's own rendered HTML. The email/widget sweet spot

Two clarifications this matrix encodes:

  • Unscoped component CSS does not merely "propagate to children": a
    <style> block is page-global. It also matches sibling components,
    other instances, and surrounding page HTML. So the answer to "should
    inline go deeper than the component's own HTML?" is: with unscoped CSS,
    yes, all the way (anything else would silently change styling);
    containment to the component's own elements is exactly what scoped
    expresses. No third semantics is needed.
  • Because scoping rewrites the CSS and stamps the HTML before inlining
    runs, the inliner needs no knowledge of scoping at all: it faithfully
    applies whatever CSS exists to whatever HTML matches, and scope-rewritten
    selectors are self-limiting. The mechanical dependency between the two
    features is only ordering (scope first, inline last); the reason to merge
    them is the product surface, not the machinery.

Where each runs (the seams exist today)

  • Scoped, two halves:
    • CSS half: rewrite selectors once per class. Rides the existing
      on_css_loaded map hook, which under the asset compiler design
      (docs/design/asset_compiler.md section 6) fires on already-compiled
      CSS, so SCSS-authored styles scope correctly too. Deterministic
      (scope id derives from class_id), so it composes with the content
      caches.
    • HTML half: stamp the scope attribute on the component's rendered
      elements per render. The root-marker machinery,
      on_template_compiled, and on_slot_rendered seams are built
      (roadmap section 2 row). The selector-rewrite primitive remains a
      candidate for the Rust html_transform layer so other bindings can
      reuse it.
  • Inline runs at serialization, on the final page HTML. This is the
    conclusion upstream
    #1147
    reached the hard way (django-components lacked a whole-output hook and
    sketched root-detection via Component.parent); citry does not need the
    workaround because on_serialize already exists (final HTML in, final
    HTML out). The styles to inline are obtainable via the on_dependencies
    emit hook (mutable scripts/styles lists at emission): pull the
    styles marked for inlining out of normal emission, apply them to the
    HTML, and emit the non-inlinable residue (media queries, pseudo-classes,
    keyframes, which cannot live in style= attributes) as a <style>
    block, which is standard css_inline behavior.
  • Consequence for the roadmap's section 4 hook list: like the asset
    compiler before it, the inline-CSS feature does not need the
    on_template_postprocess post-render hook
    (serialize covers it),
    which leaves the reactive extension's whole-HTML wrap as that hook's
    only remaining candidate consumer.

Proposed config surface

One nested config (name open: Css / CssConfig), staged delivery:

class EmailCard(Component):
    class Css:
        scoped = True    # per component: reach of this component's CSS
        inline = True    # honored when this component is the render root
    css = """
        .card { color: #333; }
    """
  • scoped is naturally per component.
  • inline is naturally per rendered tree (the pass runs on the final
    HTML). Upstream #1147 says "should be set on the root component";
    the citry equivalent: the flag takes effect when the component is the
    render root (which covers the email and fragment cases, where one
    component is the document), plus an extensions_defaults global for
    site-wide inlining. Setting it on a non-root component does nothing
    except when that component is rendered standalone; document this rather
    than warn, since "renders as an email body AND as a page fragment" is a
    legitimate dual use.
  • Staging: the scoped half is the roadmap's build-now item and can ship
    first; inline arrives later on the same config class (the per-extension
    config schema grows field by field). Merging the surface does not
    couple the schedules.

Implementation notes and caveats

  • Do not own the inliner: css_inline
    (Rust core, maintained, official Python bindings) is the engine upstream
    #1147 already points at; it handles cascade resolution among the inlined
    rules and keeps non-inlinable rules in a style block. Optional extra per
    the citry[ext-...]/lazy-import conventions.
  • Cascade caveat to document: style= attributes outrank stylesheets,
    so inlining can change the effective styling when other page CSS was
    intended to override component CSS (and !important interplay is its own
    corner). This is inherent to CSS inlining, not a citry bug; the docs
    should say "inline changes delivery, and delivery has cascade
    consequences at the page boundary".
  • css_data() variables: CSS custom properties set via the
    [data-ccss-<hash>] variables stylesheets are legal in style=
    attributes and inherit; whether the inliner should fold them or leave
    the variables stylesheet as residue needs a decision during design
    (emails likely want them folded, since some clients ignore custom
    properties entirely).
  • Base URL: url(...) references in inlined declarations may need
    absolutizing for emails; upstream sketches a get_request_domain
    override, citry's equivalent seam is the mount contract /
    Citry.build_url. Design during build.
  • Prior art warning carried over from the roadmap: the old
    django-components working tree contains a css_scope.py draft and a
    scope_css() regex-based rewriter; both are broken drafts
    ("build properly, do not port"), useful only as intent records (the Vue
    slot-fill rule, the :root opt-out idea, the tinycss2 pointer).

Relationship to other work

  • Asset compiler (#10):
    upstream of both halves; scoping sees compiled CSS because the compiler
    runs before on_css_loaded.
  • Minifier extension (#35):
    a minifier riding the future on_css_postprocess (pre-cache) would see
    scoped-rewritten CSS if scoping stays on on_css_loaded (load-time);
    ordering between the two CSS transforms is a design point for whichever
    lands second.
  • Upstream: django-components #1230
    (scoped CSS),
    django-components #1147
    (inline CSS via css_inline).

Activity

  1. added
    next-sprint
    on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions