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

              
                <article>

    <h1>Only show <code>:focus</code> styles on <code>keyup()</code> events</h1>

    <p>Items within the <code>.container</code> will show a custom <code>:focus</code> style when using the keyboard only. Mouse interactions will prevent/remove the styles.</p>

    <div id="container" class="container">
        <p>Some text with <a href="#">a link</a>.</p>
        <button id="btn" class="btn">Button</button>
    </div>

    <p>For example, <a href="#">another link</a> which has default browser styles on <code>:focus</code>.</p>

    <aside>
        <h2>Note:</h2>
        <p>For the best usability results, it's recommended to <em>not</em> remove focus styles. Only do so if you <em>absolutely must</em>. If possible, talk with your designer and come to an agreement on focus styles.</p>
    </aside>
    
</article>
              
            
!

CSS

              
                // Don't remove focus styles if JS isn't available
.js {
    
    // Remove default focus styles for elements in .container
    .container {        
        *:focus {
            outline: 0;
        }
    }
    
    // Apply custom focus styles when elements within .container have focus
    .container--has-focus {
        *:focus {
            outline: solid CornflowerBlue 3px;
        }
    }
}

.container {
    background-color: WhiteSmoke;
    padding: .5rem 1.5rem 1.5rem;
}
              
            
!

JS

              
                (() => {
    const strings = {
            clicked: 'was clicked!'
        },
        selectors = {
            container: '#container',
            btn: '#btn'
        },
        classes = {
            containerHasFocus: 'container--has-focus'
        },
        container = document.querySelector(selectors.container),
        btn = document.querySelector(selectors.btn);

    // Add the focus class to the container if the keyboard
    // event is an element within the container
    document.addEventListener('keyup', (e) => {
        if (container.contains(e.target)) {
            container.classList.add(classes.containerHasFocus);
        } else {
            container.classList.remove(classes.containerHasFocus);
        }
    });

    // Remove the focus class on mouse click
    document.addEventListener('mousedown', (e) => {
        if (container.contains(e.target)) {
            container.classList.remove(classes.containerHasFocus);
        }
    });

    // Extra code for the sake of the example...
    btn.addEventListener('click', (e) => {
        console.log(`${e.target.id} ${strings.clicked}`);
    });
})();
              
            
!
999px

Console