1

Lets say this is my javascript code:

var bottomChild = document.createElement('div');
bottomChild.id = 'bottomChildId';
bottomChild.style.width = '100px';
bottomChild.style.position = 'relative';
bottomChild.style.overflowY = 'scroll';
bottomChild.style.background = 'red';
bottomChild.style.top = '0px';
bottomChild.style.borderLeft = '0px solid #E4E7EA';
bottomChild.style.height = '200px';
document.body.appendChild(bottomChild);

Now I want to add following css into bottomChild:

::-webkit-scrollbar {
    width: 22px;
}
::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    -webkit-border-radius: 10px;
    border-radius: 10px;
}

Please help in adding these css into that dynamic javascript div.

Shashi Bharti
  • 53
  • 2
  • 6
  • Possible duplicate of [CSS Pseudo-classes with inline styles](http://stackoverflow.com/questions/5293280/css-pseudo-classes-with-inline-styles) – Alexander O'Mara Dec 17 '16 at 05:03
  • document.querySelector('style').textContent += `::-webkit-scrollbar { width: 22px; } ::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); -webkit-border-radius: 10px; border-radius: 10px; }` – Surya Purohit Dec 17 '16 at 05:05

3 Answers3

2

You should add the css you want to a separate class, then use javascript to assign the class to the new element.

.target {
    width: 100px;
    position: relative;
    // etc.
}
.target::-webkit-scrollbar {
    width: 22px;
}
.target::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    -webkit-border-radius: 10px;
    border-radius: 10px;
}

javascript:

bottomChild.className = 'target';
jaredsk
  • 2,617
  • 2
  • 18
  • 18
2

You can add class like this- div(id).className = 'target';

in your case add :

bottomChild.className = 'target';

Js:

var bottomChild = document.createElement('div');
bottomChild.id = 'bottomChildId';
bottomChild.style.width = '100px';
bottomChild.style.position = 'relative';
bottomChild.style.overflowY = 'scroll';
bottomChild.style.background = 'red';
bottomChild.style.top = '0px';
bottomChild.style.borderLeft = '0px solid #E4E7EA';
bottomChild.style.height = '200px';
bottomChild.className = 'target';
document.body.appendChild(bottomChild);

Css:

.target {
    background:#000000 !important;
    // etc.
}
.target::-webkit-scrollbar {
    width: 22px;
}
.target::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    -webkit-border-radius: 10px;
    border-radius: 10px;
}

Working fiddle :https://jsfiddle.net/mL8g3qLf/

Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
0

Adding class rules to your CSS file and then set them using javascript is the preferable way to do this, though sometimes that isn't doable.

Here is how you can add styles dynamically.

Note, since inline styles has higher specificity than an external CSS rule, one will need to use !important to override a style, as shown in below sample

window.onload = function() {

  var bottomChild = document.createElement('div');
  bottomChild.id = 'bottomChildId';
  bottomChild.style.width = '100px';
  bottomChild.style.position = 'relative';
  bottomChild.style.overflowY = 'scroll';
  bottomChild.style.background = 'red';
  bottomChild.style.top = '0px';
  bottomChild.style.borderLeft = '0px solid #E4E7EA';
  bottomChild.style.height = '200px';
  document.body.appendChild(bottomChild);

  addRule('#bottomChildId::-webkit-scrollbar', 'width: 22px;')

  addRule('#bottomChildId::-webkit-scrollbar-track', '-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); -webkit-border-radius: 10px; border-radius: 10px;')

  // Note how this does not work, since inline styles has higher specificity
  addRule('#bottomChildId', 'background: blue')

  // !important will be necesarry to override inline style
  addRule('#bottomChildId', 'width: 200px !important')
}

function addRule(s, r) {
  if (!hasStyleSheet(document)) return;
  var ss = document.styleSheets[document.styleSheets.length - 1];
  ss.insertRule(s + ' {' + r + '}', ss.cssRules.length);

  function hasStyleSheet(d) {
    if (!d.styleSheets) return false;
    if (!d.styleSheets.length) {
      var style = document.createElement("style");
      // style.setAttribute("media", "screen")
      style.appendChild(d.createTextNode(""));  //Webkit need this
      d.head.appendChild(style);
    }
    return true;
  }
};
Asons
  • 84,923
  • 12
  • 110
  • 165