123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

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.

            
              <h2>Understanding the CSS box-sizing property values</h2>
<div class="grid-container">
   <div id="div1" class="container div-1">
      <div><strong>box-sizing: content-box</strong>      
      </div>
      <div class="info"></div>
   </div>

   <div>
      By default <code>box-sizing: content-box</code> is active. This means that an element's assigned  width applies only to its content. Any padding and border widths added increase the overall element width. The width of blue box's to the left is set to 400px, but its padding and border widths make the total width of the element 426px. This can be a frustrating "feature" when desigining UIs. 
   </div>   
</div>   

<hr>

<div class="grid-container">
   <div class="container div-2">
      <div><strong>box-sizing: border-box</strong>
      </div>   
      <div class="info"></div>
   </div>
   <div>
      CSS3 introduced the <code>box-sizing: border-box</code> property. When this property is active an element's specified width is guaranteed to include any padding and border width assigned.    The width of blue box's to the left is set to 400px and that width includes its padding and border widths--making its total width be exactly as specified, 400px. Most CSS resets and CSS frameworks assign this property. <code>box-sizing: border-box</code> is available <a href="https://caniuse.com/#search=border-box">on virtually every browser.</a> 
   </div>   
</div>   

            
          
!
            
              body {
  font-family: Sans-Serif;
}

.grid-container {
   display: grid;
   grid-template-columns: 450px auto;
   justify-items: start;
}

.container {
   border: 8px solid red;
   padding: 5px;
   background-color: blue;
   width: 400px;
   color: #fff;
}

.div-1 {
   /* This is the default  */
   box-sizing: content-box;      
}

.div-2 {
   box-sizing: border-box;      
}
            
          
!
            
              const removeNonNumericChars = function(value) {
   return parseInt(value.replace(/\D/g,''), 10);
}

const getWidthHeight = function(element) {
   let outerWidth = element.offsetWidth;   
   let height = element.offsetHeight;   
      
   let info = element.querySelector('.info');   
   let innerWidth = parseInt(element.clientWidth,10);
   let paddingLeft = window.getComputedStyle(element, null).getPropertyValue('padding-left');
   let paddingRight = window.getComputedStyle(element, null).getPropertyValue('padding-right');
   let borderLeft = window.getComputedStyle(element, null).getPropertyValue('border-left-width');
   let borderRight = window.getComputedStyle(element, null).getPropertyValue('border-right-width');
   let contentWidth = outerWidth - 
                      removeNonNumericChars(paddingLeft) - 
                      removeNonNumericChars(paddingRight) - 
                      removeNonNumericChars(borderLeft) -
                      removeNonNumericChars(borderRight);
   
   info.innerHTML = `outer width: ${outerWidth} <br>\
                     inner width ${innerWidth} <br>\ 
                     content-only width: ${contentWidth}<br>\
                     padding width left: ${paddingLeft}<br>\
                     padding width right: ${paddingRight}<br>\
                     border width left: ${borderLeft}<br>\
                     border width right: ${borderRight}<br>`;        
}

getWidthHeight(document.querySelector('.div-1'));
getWidthHeight(document.querySelector('.div-2'));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console