Answers for "how to apply styles in javascript"

28

add style javascript

// Bad:
element.setAttribute("style", "background-color: red;");
// Good:
element.style.backgroundColor = "red";
Posted by: Guest on September-08-2020
0

Set CSS styles with javascript

var style = document.createElement('style');
  document.head.appendChild(style);
  style.sheet.insertRule('#target {color: darkseagreen}');
Posted by: Guest on February-28-2022
0

Set CSS styles with javascript

// Create our shared stylesheet:
const sheet = new CSSStyleSheet();
sheet.replaceSync('#target {color: darkseagreen}');

// Apply the stylesheet to a document:
document.adoptedStyleSheets = [sheet];
Posted by: Guest on February-28-2022
0

Set CSS styles with javascript

var style = document.createElement('style');
  style.innerHTML = `
  #target {
  color: blueviolet;
  }
  `;
  document.head.appendChild(style);
Posted by: Guest on February-28-2022

Code answers related to "how to apply styles in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language