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

              
                .wrapper
  .lede
    code width: 6.2rem; height: 6.2rem; border-width: .3rem; margin: 0.3rem 0.2rem;
  .container
    .border-box
      | box1
    .border-box
      | box2
    .border-box
      | box3
  .result
.console
    
    
              
            
!

CSS

              
                html, body
  margin: 0
  padding: 0

html
  font-size: 16px;

.wrapper
  position: relative
  display: flex
  flex-direction: column 
  margin: auto
  max-width: 30rem
  //height: 75vh

.container
  display: flex
  align-items: center
  justify-content: center
  padding: 1.5rem 0
  height: 100%
  
.border-box
  margin: 0.3rem 0.2rem
  background: #f8f5f0
  border: .3rem solid #2ba4bb
  width: 6.2rem
  height: 6.2rem
  display: flex
  align-items: center
  justify-content: center

.lede
  margin-top: 1rem

code
  color: #e83e8c
  background: #e8ebf1
  border-radius: .2rem
  padding: .2rem .25rem

.console
  font-family: monospace
  color: #f8f5f0
  padding: .5rem 1.5rem
  background: #202122
  width: 100%
  //height: 25vh
  box-sizing: border-box
  overflow: auto

.console-line
  font-size: 80%
  &:before
    content: '> '
    
  + .console-line
    margin-top: .25rem
 
.result
  font-size: .8rem
  margin-bottom: .5rem
              
            
!

JS

              
                const area = document.querySelector('.console');

const appendLine = (text) => {
  const line = document.createElement('div');
  line.classList.add('console-line');
  line.innerHTML = text;
  area.appendChild(line);
}

const boxElm = document.querySelectorAll('.border-box')[0];

(function() {
const styles = window.getComputedStyle(boxElm);
const {marginTop, marginRight, marginBottom, marginLeft} = styles;
  console.log(marginTop, marginRight, marginBottom, marginLeft);
})();

// clinetHeight / clientWidth 
const clientHeight = boxElm.clientHeight;

appendLine(`clientHeight: ${clientHeight}(px)`);

// offsetHeight / offsetWidth
const offsetHeight = boxElm.offsetHeight;
appendLine(`offsetHeight: ${offsetHeight}(px)`);

// getBoundingClientRect().height / getBoundingClientRect().width
const boundingClientRectHeight = boxElm.getBoundingClientRect().height;
appendLine(`getBoundingClientRect().height: ${boundingClientRectHeight} (px)`);

// get mergen
const boxElmStyle = window.getComputedStyle(boxElm);
const marginTop = boxElmStyle.marginTop;

appendLine(`document.defaultView.getComputedStyle(boxElm).marginTop ${marginTop}: (string)`);

//---

appendLine('')
appendLine(`Get Box size with mergen = Box height + border width * 2 + margin-top + margin-bottom`);
appendLine(`=> Element.getBoundingClientRect().height + document.defaultView.getComputedStyle(Element).marginTop + document.defaultView.getComputedStyle(Element).marginBottom`)
appendLine(boundingClientRectHeight + parseFloat(boxElmStyle.marginTop.replace('px', '')) + parseFloat(boxElmStyle.marginBottom));

const getBoundingSize = (element) => {
  const boundingClientRect = element.getBoundingClientRect();
  return {
    width: boundingClientRect.width,
    height: boundingClientRect.height,
  }
}

const getElementsMargin = (element) => {
  const style = window.getComputedStyle(element);
  return {
    top: parseFloat(style.marginTop) || 0,
    right: parseFloat(style.marginRight) || 0,
    bottom: parseFloat(style.marginBottom) || 0,
    left: parseFloat(style.marginLeft) || 0,
  }
}

const getOuterSize = (element) => {
  const margin = getElementsMargin(element);
  const size = getBoundingSize(element);
  
  return {
    width: size.width + margin.left + margin.right,
    height: size.height + margin.top + margin.bottom,
  };
}

function setResult() {
  const resElm = document.querySelector('.result');
  resElm.innerHTML = '';
  
  const boxSize = getBoundingSize(boxElm);
  const outerBoxSize = getOuterSize(boxElm);
  
  const boxRect = boxElm.getClientRects()[0];
  
  const texts = [
    `clientWith / clientHeight: ${boxElm.clientWidth} / ${boxElm.clientHeight}`,
    `offsetWidth / offsetHeight: ${boxElm.offsetWidth} / ${boxElm.offsetHeight}`,
    `getClientRects: ${boxRect.width} / ${boxRect.height}`,
    `getBoundingClientRect: ${boxSize.width} / ${boxSize.height}`,
    `boxMargin: ${Object.entries(getElementsMargin(boxElm)).map(([key, val]) => `${key}: ${val}`).join(', ')}`,
    `outerWidth / outerHeight: ${outerBoxSize.width} / ${outerBoxSize.height}`,
  ];
  
  const flagment = document.createDocumentFragment();
  texts.forEach((str) => {
    const div = document.createElement('div');
    div.textContent = str;
    flagment.appendChild(div);
  });
  
  resElm.appendChild(flagment);
}

let timer;

window.addEventListener('resize', function() {
  timer && window.clearTimeout(timer);
  timer = window.setTimeout(() => {
    setResult();
  }, 100);
});

setResult();
              
            
!
999px

Console