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

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

              
                <form action="#" id="demoForm">
    <p>
        <label for="name">Name</label>
        <input type="text" name="name" id="name"/>
    </p>
    <p>
        <label for="email">E-mail address</label>
        <input type="text" name="email" id="email"/>
    </p>
    <p>
        <span>
            <label for="something1">Something</label>
            <input type="text" name="something1" id="something1" value="Pre-filled"/>
        </span>
        <span>
            <label for="something2">Something else</label>
            <input type="text" name="something2" id="something2"/>
        </span>
    </p>
</form>

              
            
!

CSS

              
                * {
    margin: 0;
    padding: 0;
}

html {
    font-family: sans-serif;
    padding: 20px;
    font-weight: 100;
    -webkit-font-smoothing: antialiased;
}

p {
    width: 400px;
    position: relative;
    margin-bottom: 10px;
}

span {
    width: 50%;
    box-sizing: border-box;
    display: inline-block;
    float: left;
    position: relative;
}

span:first-of-type {
    padding-right: 5px;
}

span:last-of-type {
    padding-left: 5px;
}

span:last-of-type label {
    left: 11px;
}

input {
    width: 100%;
    box-sizing: border-box;
    padding: 5px 10px;
    border: 1px solid #ccc;
    border-radius: 3px;
    height: 40px;
    outline: none;
    font-size: 16px;
    transition: border-color 125ms;
}

input:focus {
    border-color: #000;
}

label {
    position: absolute;
    left: 6px;
    top: 12px;
    pointer-events: none;
    transition: all 125ms;
    color: #666;
    padding: 0 5px;
}

.active label {
    font-size: 10px;
    text-transform: uppercase;
    top: -5px;
    background: #fff;
    color: #000;
}
              
            
!

JS

              
                (function(namespace){
    "use strict";

    /**
     * This method adds a focus-class to the parent wrapper of an input element.
     * Example HTML Markup:
     *  <span>
     *      <label for="name">Name</label>
     *      <input type="text" name="name" id="name" />
     *  </span>
     * When the input element is not empty or receives focus, the wrapping <span>-
     * element receives an 'active'-class
     * @param {HTMLFormElement} parentForm
     * @constructor
     */
    function FocusLabel(parentForm) {
        var labels = parentForm.querySelectorAll('label');
        for (var i in labels) {
            if (labels.hasOwnProperty(i)) {
                this.bindFunctionality(labels[i]);
            }
        }
    }

    /**
     * Bind the functionality to a single label
     * @param {HTMLLabelElement} label
     */
    FocusLabel.prototype.bindFunctionality = function(label)
    {
        var parentNode = label.parentNode;
        var inputElement = parentNode.querySelector('input');
        if (inputElement) {
            inputElement.addEventListener('focus', this.checkLabel.bind(this, parentNode));
            inputElement.addEventListener('blur', this.checkLabel.bind(this, parentNode));
            this.checkLabel(parentNode);
        }
    };

    /**
     * Bind label/input functionality
     * @param {HTMLElement} parentNode
     * @param {FocusEvent} event
     */
    FocusLabel.prototype.checkLabel = function(parentNode, event)
    {
        var inputElement = parentNode.querySelector('input');
        parentNode.classList.remove('active');
        if (inputElement.value !== '' || (event && event.type === 'focus')) {
            parentNode.classList.add('active');
        }
    };

    namespace.FocusLabel = FocusLabel;
})(window);

new window.FocusLabel(document.getElementById('demoForm'));
              
            
!
999px

Console