How do you add CSS with Javascript?

前端 未结 14 2631
旧时难觅i
旧时难觅i 2020-11-22 17:18

How do you add CSS rules (eg strong { color: red }) by use of Javascript?

14条回答
  •  失恋的感觉
    2020-11-22 17:49

    The simple-and-direct approach is to create and add a new style node to the document.

    // Your CSS as text
    var styles = `
        .qwebirc-qui .ircwindow div { 
            font-family: Georgia,Cambria,"Times New Roman",Times,serif;
            margin: 26px auto 0 auto;
            max-width: 650px;
        }
        .qwebirc-qui .lines {
            font-size: 18px;
            line-height: 1.58;
            letter-spacing: -.004em;
        }
    
        .qwebirc-qui .nicklist a {
            margin: 6px;
        }
    `
    
    var styleSheet = document.createElement("style")
    styleSheet.type = "text/css"
    styleSheet.innerText = styles
    document.head.appendChild(styleSheet)
    

提交回复
热议问题