Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
How to apply CSS to newly loaded Document

I would like to load a document using fetch() and then apply CSS styles to it.

However, it doesn't seem like I can inspect the styles unless I append the document to the main one, as seen here:

const doc = fetch(); // get some document

doc.documentElement.computedStyleMap()  // returns an empty map

document.appendChild(doc.documentElement)  // attach it to the main document object

doc.documentElement.computedStyleMap()  // now populated

Is there any way I can call some method to get the CSS cascade to happen, populating the computedStyleMap without attaching it as done in step 3?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would like to load a document using fetch() and then apply CSS styles to it.

I've tested this locally and it seems computedStyleMap only works for documents / elements that are actually attached in the DOM.

An alternative is using the styleSheets API.

    var styleSheet = document.styleSheets[0];
    styleSheet.insertRule('.myclass{ color: 'red'; }', styleSheet.cssRules.length);

Keep in mind that if you load a stylesheet from another domain, like a CDN, you may need to select a different index of the .styleSheets to use one on your domain. Otherwise you may encounter security errors.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!