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 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

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

              
                <p class="toggle">Toggle size <input type="checkbox" class="js-toggle"></p>

<div class="sizes">
  <p>Text width: <span class="js-text-width">0px</span></p>
  <p>Text height: <span class="js-text-height">0px</span></p>
  <p>Image width: <span class="js-image-width">0px</span></p>
  <p>Image height: <span class="js-image-height">0px</span></p>
</div>

<div class="container is-small">
  <div class="box content-box">
    <h2>Resize Observer</h2>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Rem impedit necessitatibus dolore dolorem voluptates et quo illum, nihil officia perferendis illo quisquam totam? Reiciendis nisi voluptatibus velit numquam doloribus sit.</p>
  <img src="https://source.unsplash.com/random/800x420" alt="">
</div>
</div>
              
            
!

CSS

              
                .container {
  max-width: 800px;
  transition: max-width 2s;
  
  &.is-small {
    max-width: 250px;
  }
}

.box {
  max-width: 600px;
  border: 2px solid #000;
  padding: 10px;
  
  p {
    max-width: 350px;
  }
  
  img {
    display: block;
    max-width: 550px;
    width: 100%;
    height: auto;
  }
}

.toggle {
  margin: 0 0 20px;
}

.sizes {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 200px;
  background: orange;
  padding: 10px 15px;
  z-index: 10;
}
              
            
!

JS

              
                const c = u('.container');

u('.js-toggle').on('click', () => {
  if (c.hasClass('is-small')) {
    c.removeClass('is-small');
  } else {
    c.addClass('is-small');
  }
});

const tWidth = document.querySelector('.js-text-width');
const tHeight = document.querySelector('.js-text-height');
const iWidth = document.querySelector('.js-image-width');
const iHeight = document.querySelector('.js-image-height');

const ro = new ResizeObserver((entries, observer) => {
   for (let entry of entries) {
     const { target } = entry;
     const width = entry.contentRect.width.toFixed(2);
     const height = entry.contentRect.height.toFixed(2);
     
     if (target.tagName === 'IMG') {
       iWidth.textContent = `${width}px`;
       iHeight.textContent = `${height}px`;
     } else if (target.tagName === 'P') {
       tWidth.textContent = `${width}px`;
       tHeight.textContent = `${height}px`;
     }
   }
});

ro.observe(document.querySelector('.box p'));
ro.observe(document.querySelector('.box img'));

              
            
!
999px

Console