Skip to content

Support fragment references in the <link> tag's href attribute #11019

Open
@KurtCattiSchmidt

Description

@KurtCattiSchmidt

What is the issue with the HTML Standard?

The problem

There are currently several options for sharing styles with Declarative Shadow DOM, but all of them rely on external files or dataURI's:

  1. <link rel="stylesheet" href="foo.css"> this requires an external file.
  2. <link rel="stylesheet" href="data:text/css;..."> this is not technically an inline style definition, but it doesn't generate a network request, so it's as close as you can get to an inline style today. This must be re-parsed and duplicated in memory for each instance, and the dataURI syntax has poor developer ergonomics.
  3. adoptedStyleSheets via Javascript - using Javascript somewhat defeats the purpose of Declarative Shadow DOM, and this approach still only supports entire files (or a dataURI).

Use cases

  • CSS @sheet - this will enable CSS @sheet to work with inline CSS. CSS @sheet will only work in external CSS files unless there's a mechanism for referencing inline style blocks as mentioned in this proposal. For more details (including examples), see this @sheet explainer.
  • Minimizing time to First Contentful Paint (FCP) metrics - by not relying on external files, inline styles can be parsed once and reused many times
  • Lowering style computation costs with Declarative Shadow DOM - by structuring styles so that a base set of styles can be selectively applied to Declarative Shadow DOM instances, developers can optimize their site's style computation performance and reduce duplicated CSS rules.
  • Custom Elements - Custom Elements often rely on Shadow DOM for ID scoping, but they lose access to most of the light DOM's style information and need to pick a least-bad option from the current solutions listed above.

Proposed Solution

Allow the <link> tag's href attribute to support same-document references to corresponding fragment identifiers for <style> tags.

<style id="inline_styles">
  p { color: blue; }
</style>
<p>Outside Shadow DOM</p>
<template shadowrootmode="open">
  <link rel="stylesheet" href="#inline_styles">
  <p>Inside Shadow DOM</p>
</template>

Prior Art

  • SVG xlink:href syntax is very similar, although it allows cross-document references. For HTML, this might not be desirable.
  • Reference Target expands behavior of Shadow DOM via node ID's

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions