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

              
                <p class="greedy">
  <button class="button" type="button">
    <span class="visuallyHidden">
      Accessible button description (only "seen" by AT/screen-reader users)
    </span>
    <span class="icon"><!-- <svg> --></span>
    <span class="icon"><!-- <svg> --></span>
    <span hidden>non-visible content</span>
  </button>
  
  (greedy)
</p>

<p class="better">
  <button class="button" type="button">
    <span class="visuallyHidden">
      Accessible button description (only "seen" by AT/screen-reader users)
    </span>
    <span class="icon"><!-- <svg> --></span>
    <span class="icon"><!-- <svg> --></span>
    <span hidden>non-visible content</span>
  </button>
  
  <ui-button class="button">
    <!-- unslotted element -->
    <span class="visuallyHidden">
      Accessible button description (only "seen" by AT/screen-reader users)
    </span>
    
    <span class="icon" slot="icon-left"><!-- <svg> --></span>
    <span class="icon" slot="icon-right"><!-- <svg> --></span>
    
    <!-- unslotted content -->
    <span hidden>non-visible content</span>
  </ui-button>
  
  (better)
</p>

<p class="best">
  <button class="button" type="button">
    <span class="visuallyHidden">
      Accessible button description (only "seen" by AT/screen-reader users)
    </span>
    <span class="icon"><!-- <svg> --></span>
    <span class="icon"><!-- <svg> --></span>
    <span hidden>non-visible content</span>
  </button>
  
  (best, if it worked)
</p>




<template id="ui-button.html">
  <style>
    :host { display: inline-block; }
    * { box-sizing: border-box; }
  </style>
  <slot name="icon-left"></slot>
  <slot name="text"></slot>
  <slot name="icon-right"></slot>
</template>
              
            
!

CSS

              
                .button {
  display: inline-flex;
}

/* 
  Too greedy!

  Unfortunately, this still targets the <span class="visuallyHidden"> element.
*/
.greedy .button > * + * {
  margin-inline-start: 0.5rem;
}




/* 
  Better, but doesn't apply if children are hidden via other CSS classes, or HTML attributes.

  What if a child is hidden via the [hidden] attribute?
    - UA doesn't render it, so margin won't apply.
  What if a child is invisible because a custom element didn't provide a default <slot>?
    - Similar to [hidden], the element isn't rendered, so margin doesn't get applied.
  What if a child is invisible by means of externally defined CSS?
    - There's no way to know every CSS class that does this, so we can't use explicit
      :not(.className) selectors in this scenario.  We need a way to programmatically 
      check the conditions in JS rather than explicitly overriding edge cases in CSS.
*/
.better .button > *:not(.visuallyHidden) + *:not(.visuallyHidden) {
  margin-inline-start: 0.5rem;
}




/* 
  Just right!

  The definition of "visible" can be fine tuned (maybe even configurable) via JS to address 
  different scenarios, but the CSS remains short and sweet.
*/
.best .button > :visible + :visible {
  margin-inline-start: 0.5rem;
}































/* PEN ONLY */
html { box-sizing: border-box; }
*, *::before, *::after { box-sizing: inherit; }
.icon {
  background-color: currentColor;
  display: inline-block;
  height: 1em;
  width: 1em;
}
.button {
  align-items: center;
  background-color: #009aff;
  border-radius: 2px;
  border: 1px solid navy;
  color: #006;
  height: 2rem;
  padding: 0.25rem 0.75rem;
  margin: 0;
  font: inherit;
}
.visuallyHidden {
  /* remove borders and shadows */
  border: none;
  box-shadow: none;
  outline: none;

  /* remove from normal document flow */
  position: absolute;
  z-index: -9999;

  /* render fg and bg invisible */
  background-color: transparent;
  color: transparent;
  text-shadow: transparent;

  /* shrink as small as possible and avoid interference with sibling element layout */
  height: 1px;
  margin: -1px;
  overflow: hidden;
  width: 1px;
}
              
            
!

JS

              
                window.customElements.define('ui-button', class extends HTMLElement {
  constructor () {
    super();
    let _template = document.getElementById('ui-button.html')
    
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.appendChild(_template.content.cloneNode(true));
  }
})
              
            
!
999px

Console