Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <div class="test" data-font='16'></div>
<input type="range" id='change-font' />
<div class="change-color">改变文字颜色</div>


              
            
!

CSS

              
                
.test{
  display: block;
}
.test::before{
  content:'我是' attr(class) "的::before伪元素,当前字号为" attr(data-font) 'px' ;
  width: 100%;
  word-break:break-all;
  display: block;
  border:2px solid #ABC;
  font-size:16px;
  margin-bottom:10px;
  white-space:pre;
}
.test-red::before{/*改变伪元素内文字颜色的class*/
  color:red;
}
.change-color{
  display: block;
  width: 150px;
  height: 50px;
  line-height: 50px;
  text-align:center;
  background: #EEE;
  cursor:pointer;
  -webkit-user-select:none;
}


              
            
!

JS

              
                
console.log(window.location.href);
var btnFontSize=document.querySelector('input');
var btnColor=document.querySelector('.change-color');
var currentPseudo=document.querySelector('.test');
var stylesheets=document.styleSheets;
var sheet;
if(stylesheets){
    sheet=stylesheets[stylesheets.length-1];
  // console.log(sheet);
  // console.log('sheet.cssRulesLength='+sheet.cssRules.length);
}else{
    var style=document.createElement('style');
    document.head.appendChild(style);
}
btnColor.addEventListener('click',function(){
  //粗略实现的改变颜色,未做切换。
  currentPseudo.classList.add('test-red');
});
var currentFontSize=window.getComputedStyle(currentPseudo,'::before').getPropertyValue('font-size');//获取当前伪元素字体大小。
var cssRulesLength=(sheet.cssRules.length);//不‘-1’是因为这样才是在最后一位,否则加入的样式会变成倒数第二
console.log('cssrule='+cssRulesLength+'currentFont='+currentFontSize);
sheet.insertRule('.test::before{font-size:'+currentFontSize+'}',cssRulesLength);//在最后一个style标签的最后位置添加当前字体大小样式。这么做是为了后面删除添加上去的cssRule,否则最后css表会变得巨大无比
btnFontSize.addEventListener('input',function(){
//拖动滑块改变文字大小
  var changeFontSize=btnFontSize.value;
  currentPseudo.setAttribute('data-font',changeFontSize);
  var index=sheet.cssRules.length-1;
  console.log('index='+index);
 sheet.deleteRule(index);//删除最后一行样式(这里是前面添加的字体大小样式,不直接删除是为了防止误删除影响其他属性
  sheet.insertRule('.test::before{font-size:'+changeFontSize+'px}',index);
  // sheet.insertRule('.test::before{font-size:16px}',index);
});

              
            
!
999px

Console