Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                  <div id="container">
    <div id="box"></div>
    <div id="side">
        <input type="button" id="btn" value="add">
        <select id="sel" size="4">
            <option selected>black</option>
            <option>red</option>
            <option>green</option>
            <option>blue</option>
        </select>
    </div>
  </div>
              
            
!

CSS

              
                #container {
    margin: 0;
    padding: 0;
    display: flex;
    display: -webkit-flex;
    justify-content: center;
    -webkit-justify-content: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateY(-50%) translateX(-50%);
}
#box {
    margin: 0;
    padding: 0;
    width: 300px;
    height: 300px;
    position: relative;
    overflow-y: auto;
    overflow-x: hidden;
    border: solid 1px #ccc;
}
#box > div {
    border-bottom: solid 1px #fff;
}
#box > div:last-child {
    border-bottom: none;
}
#side {
    margin: 10px 0 0 20px;
    padding: 0;
}
#btn {
    margin: 0 0 10px 0;
    padding: 4px 8px;
    display: block
}
#sel {
    margin: 0;
    padding: 4px;
    width: 60px;
    overflow: hidden;
}

              
            
!

JS

              
                /***
 スタイルシートの直接操作
***/
var styles = {
    sheet: null,// cssRulesの初期値はnull。append後cssRules[0]に実体が入る
    create: function() {
        var elem = document.createElement("style");
        document.head.appendChild(elem);
        this.sheet = elem.sheet;
    },
    search: function(selector) {
        if (!this.sheet || !selector) return -1;
        var rules = this.sheet.cssRules;
        for (var i=0, len=rules.length, index=-1; i<len; i++) {
            if (!rules[i].selectorText) continue;   // @import などのルールを除外
            if (rules[i].selectorText === selector) {
                index = i;
                break;
            }
        }
        return index;
    },
    write: function(selector, propval) {
        if (!this.sheet || !selector || !propval) return;
        var index = this.search(selector);

        if (typeof propval !== "string") {
            // 対象とするセレクタのルールが存在しない場合、ルールを最終位置に作成
            if (index < 0) {
                index = this.sheet.cssRules.length;
                this.sheet.insertRule(selector + " {}", index);
            }
            // 対象プロパティの値のみ上書き
            propval.forEach(function(elem) {
                this.sheet.cssRules[index].style[elem[0]] = elem[1];
            }, this);
        } else {        // ルール(セレクタ単位)をまるごと書き換え
            this.sheet.insertRule(selector + " {" + propval + "}", this.sheet.cssRules.length);
            if (index >= 0) {
                // 対象とするセレクタのルールが既に存在していた場合、削除する
                // 削除しないと書き換えるたびにルールがどんどん増えていく
                // CSSルールが増えてもパフォーマンス的に問題ないかもしれないけど
                // なんとなく不要なルールは消しておきたいかなと。
                this.sheet.deleteRule(index);
            }
        }
    },
    read: function(selector) {  // 前提:本シート内には同一セレクタは存在しない
        if (!this.sheet || !selector) return "";
        var rules = this.sheet.cssRules;
        var csstext = "";
        var index = this.search(selector);
        if (index >= 0) {
            csstext = rules[index].cssText;
        }
        return csstext;
    },
    delete: function(selector) {
        if (!this.sheet || !selector) return;
        var index = this.search(selector);
        if (index >= 0) {
            this.sheet.deleteRule(index);
        }
    }
};

var gCount = 0;

styles.create();
writeStyles(0);

window.onload = function() {
	document.getElementById("btn").addEventListener('click', function(e) {
        var elem = document.createElement("div");
        elem.textContent = "element " + (gCount++);
        document.getElementById("box").appendChild(elem);
        elem.parentNode.scrollTop = elem.offsetTop;
        console.log(styles.read("#box > div"));
    });
    document.getElementById("sel").addEventListener('change', function(e) {
        writeStyles(e.target.selectedIndex);
    });
};

function createPopval(color, size, height, bcol) {
    return [["color", color],
            ["fontSize", size],      // プロパティ名はキャメルケース
            ["height", height],
            ["lineHeight", height],
            ["backgroundColor", bcol],
            ["textAlign", "center"]];
}
/*
function createPopval(color, size, height, bcol) {
    return "color:" + color + ";"
            + "font-size:" + size + ";" // insertRules()はキャメルケースじゃだめ
            + "height:" + height + ";"
            + "line-height:" + height + ";"
            + "background-color:" + bcol + ";"
            + "text-align:" + "center;";
}
*/
function writeStyles(type) {
    var selector = "#box > div";
    var propval = null;
    switch (type) {
        case 0: propval = createPopval("#000", "16px", "30px", "#eee"); break;
        case 1: propval = createPopval("#f00", "16px", "50px", "#800"); break;
        case 2: propval = createPopval("#0f0", "32px", "50px", "#080"); break;
        case 3: propval = createPopval("#0ff", "12px", "20px", "#00f"); break;
    }
    styles.write(selector, propval);
}

              
            
!
999px

Console