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="example">
    <p class="withClientRectsOverlay" style="width: 100px; height: 100px;">
        this is p
        <span>this is span this is span this is span this is span </span>
    </p>
</div>

<div class="example">
   <span class="withClientRectsOverlay">this is a span this is a span this is a span this is a span this is a span </span>
</div>

<div class="example">
   <span class="withClientRectsOverlay">this is a span 
       <span>this is a span</span>
       this is a span this is a span this is a span 
   </span>
</div>

<div class="example">
   <span class="withClientRectsOverlay">this is a span 
       <span style="display: inline-block; margin: 10px; height: 50px; line-height: 50px;">this 
            <span>is</span>
        a span</span>
       this is a span this is a span this is a span 
   </span>
</div>

<div class="example">
   <span class="withClientRectsOverlay">this is a span 
       <span style="display: inline-block; margin: 10px; height: 50px; line-height: 50px;">this 
            <span style="margin: 10px;">is</span>
        a span</span>
       this is a span this is a span this is a span 
   </span>
</div>

<div class="example">
   <span class="withClientRectsOverlay">this is a span 
       <span style="display: inline-block; margin: 10px; height: 50px; line-height: 50px;">this 
            <span style="display: inline-block;">is</span>
        a span</span>
       this is a span this is a span this is a span 
   </span>
</div>

<div class="example">
   <span class="withClientRectsOverlay">this is a span 
       <span style="display: block; margin: 10px; height: 50px; line-height: 50px;">this <span>is</span> a span</span>
       this is a span this is a span this is a span 
   </span>
</div>

<div class="example">
   <span class="withClientRectsOverlay">this is a span 
       <span style="display: block; margin: 10px; height: 50px; line-height: 50px;">this <span style="margin: 10px">is</span> a span</span>
       this is a span this is a span this is a span 
   </span>
</div>
              
            
!

CSS

              
                .example {
    display: inline-block;
    vertical-align: top;
    width: 150px;
}
              
            
!

JS

              
                function addClientRectsOverlay(elt) {
    // 为了使边框宽度与矩形宽度一致,这里给每个客户矩形上方绝对定位一个 div。
    // 注意:如果用户改变大小或者缩放,绘图将会重绘。

    var rects = elt.getClientRects();
    for (var i = 0; i != rects.length; i++) {
        var rect = rects[i];
        var tableRectDiv = document.createElement('div');
        tableRectDiv.style.position = 'absolute';
        tableRectDiv.style.border = '1px solid red';
        tableRectDiv.style.backgroundColor = 'rgba(255,0,0,.25)';
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
        tableRectDiv.style.margin = tableRectDiv.style.padding = '0';
        tableRectDiv.style.top = (rect.top + scrollTop) + 'px';
        tableRectDiv.style.left = (rect.left + scrollLeft) + 'px';
        // 我们希望 rect.width 作为边框宽度,所以内容宽度减少2px

        tableRectDiv.style.width = (rect.width - 2) + 'px';
        tableRectDiv.style.height = (rect.height - 2) + 'px';
        document.body.appendChild(tableRectDiv);
    }
}

(function() {
  /* 给所有具有"widthClientRectsOverlay"样式的元素调用函数addClientRectsOverlay(elt) */
  var elt = document.getElementsByClassName('withClientRectsOverlay');
  for (var i = 0; i < elt.length; i++) {
    addClientRectsOverlay(elt[i]);
  }
})();
              
            
!
999px

Console