WICG / construct-stylesheets

API for constructing CSS stylesheet objects
Other
138 stars 25 forks source link

construct stylesheet priority #127

Open mantou132 opened 4 years ago

mantou132 commented 4 years ago
const sheet = new CSSStyleSheet();
sheet.replaceSync(`
  h1 {
    color: blue;
  }
`);

document.adoptedStyleSheets = [sheet];

document.body.innerHTML = `
  <style>
    h1 {
      color: red;
    }
  </style>
  <h1>title</h1>
`;

construct stylesheet overrides <style>.

but sometimes I don't want to override the style of <style> <link> in the code, I just want to construct stylesheet to work like a browser stylesheet. // case: <gem-frame>

is there a way to determine the priority of constructing stylesheet?

nordzilla commented 4 years ago

This was discussed and resolved here: https://github.com/WICG/construct-stylesheets/issues/93#issuecomment-577713131

As of right now, constructed always go after other author sheets.

mantou132 commented 1 year ago

use @layer can solve the problem:

const sheet = new CSSStyleSheet();
sheet.replaceSync(`
  @layer { 
    h1 {
      color: blue;
    }
  }
`);

document.adoptedStyleSheets = [sheet];

document.body.innerHTML = `
  <style>
    h1 {
      color: red;
    }
  </style>
  <h1>title</h1>
`;