Top community members

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

[Edit]
EN

JavaScript - how to create <style> tag?

7 points
Created by:
Kate_C
2739

In JavaScript it is possible to create <style> tag in following ways.

1. JavaScript Embed CSS (style tag) injection example

index.htm file:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <p>This is red web page...</p>
  <script>
  
    var element = document.createElement('style');

    element.appendChild(document.createTextNode('body { background: red; }'));
    element.appendChild(document.createTextNode('body p { background: yellow; color: blue; }'));
    // appending another rules

    document.head.appendChild(element);

  </script>
</body>
</html>

Result:

JavaScript style tag injection example.
JavaScript style tag injection example.

 

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more