123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/0.0.1/prism.min.js"></script>

<div class="container">
    <h1>CSS Custom Properties (don't know that name? What about CSS Variables?)</h1>
    <p>In this talk, we'll walk through refactoring a standard set of vanilla CSS into code using custom properties.</p>
   
</div>

<div class="container">
    <h1>This is a standard button style</h1>
    <p>You've probably seen and written this code hundreds of time. You've probably also been fancier with it...</p>
<pre>
<code class="language-css">
.button {
    background-color: tomato;
    color: white;
    display: inline-block;
    padding: .5rem 1rem;
    border-radius: 3px;
    text-decoration: none;
}
</code>
</pre>

    <a href="#" class="button">
    This is a button style   
    </a>
    
</div>
             



<div class="container">
    <h1>Convert the styles to custom properties</h1>
    <p>Let's make the styles more flexible using custom properties</p>
    
    <h2>Set and Get your Custom Properties</h2>
    <p>We use the syntax <code>--variable-name: variable-value</code> to set a variable and <code>var(--variable-name)</code> to get the variable value. When we set values that we want to be globally accessed, we set them on the <code>:root</code> pseudo-element.</p>
    
    <h3>Set</h3>
    
    <pre>
        <code class="language-css">
        :root {
            --button-background: tomato;
            --button-foreground: white;
            --button-display: inline-block;
            --button-padding: .5rem 1rem;
            --button-corners: 3px;
            --button-decoration: none;
            --button-text-align: center;

        }
</code>
    
</pre>
    
    <h3>Get</h3>
<pre>
<code class="language-css">
.button-withvars {
    background-color: var(--button-background);
    color: var(--button-foreground);
    display: var(--button-display);
    padding: var(--button-padding);
    border-radius: var(--button-corners);
    text-decoration: var(--button-decoration);
    text-align: var(--button-text-align);

}
</code>
</pre>

    <a href="#" class="button-withvars">
    This is a button style   
    </a>
    
</div>
             
<div class="container special">
    <h1>Use Cascade and specificity for scoping</h1>
    <p>We may have set the variables on the document's root, but we can update them at various scope levels throughout our CSS. We don't even have to change button classes. We can do this by one of its parents' classes!</p>
    <h3>HTML</h3>
    <pre>
        <code class="language-html">
 &lt;div class="container special">
     &lt;a href="#" class="button-withvars">
    This is a button style   
     &lt;/a>
 &lt;/div>
        </code>
    </pre>
    
    <h3>CSS</h3>
    <pre>
    <code class="language-css">
.special {
    --button-background: lightblue;
    --button-foreground: #333;
    --button-display: block;
}
    </code>
    </pre>
    <a href="#" class="button-withvars">
    This is a button style   
    </a>
</div>


                              
<div class="container">
    <h1>Use JS to set globally</h1>
    <p>Dark mode anyone?</p>
    
    <h3>Set them by JS</h3>
    
    <pre>
        <code class="language-js">
    
    
let darkModeToggle = document.querySelectorAll('.darkMode');

darkModeToggle.forEach(toggler => toggler.addEventListener('click', e => {
    e.preventDefault();
    document.documentElement.style.setProperty('--color', '#fff');
    document.documentElement.style.setProperty('--bg-color', '#333');
    document.documentElement.style.setProperty('--button-background', '#7d483e');
    document.documentElement.style.setProperty('--button-foreground', '#eee');

}));
        </code>
    </pre>
    
    <h3>Or Toggle the class and set your colors</h3>
    <h4>JS</h4>
    <pre>
        <code class="language-js">
let darkModeToggle = document.querySelectorAll('.darkModeToggle');
let body = document.querySelector('body');

darkModeToggle.forEach(toggler => toggler.addEventListener('click', e => {
    e.preventDefault();
    body.classList.toggle('darkMode')
}));
        </code>
    </pre>
    
    <h4>CSS</h4>
        <pre>
        <code class="language-css">
.darkMode {
    --button-background: #7d483e;
    --button-foreground: #eee;    
    --color: #fff;
    --bg-color: #333;
}
        </code>
        </pre>
    <a href="#" class="button-withvars darkModeToggle">
    Toggle Dark Mode   
    </a>
    
</div>



<div class="container themed">
    <h1>Make your own theme on the fly!</h1>
    <p>Let's use JS to allow a user to adjust the site's theme on the fly! See the CodePen Panels for the code for this one. It's a lot for this little window!</p>
    
    
    <form action="" class="theme-change">
        <h4>Page options</h4>
        <label for="" >page background-color</label>
        <input type="color" id="bg-color" name="bg-color" class="text">
        <label for="">page font color</label>
        <input type="color" name="color"  id="color" class="text">
        
        <h4>Button Options</h4>
        <a href="#" class="button-withvars">Visual reference button</a> <br><br>
        <label for="button-display">Full width?</label>
        <select name="button-display" id="button-display">
            <option value="inline-block">No</option>
            <option value="block">Yes</option>
        </select>
        <br>
        <label for="button-background" >button background-color</label>
        <input type="color" id="button-background" name="button-background" class="text">
        <label for="button-foreground" >button foreground-color</label>
        <input type="color" id="button-foreground" name="button-foreground" class="text">
        
        <br>
        <label>Border Radius:</label>
        <input data-suffix="true" type="range" id="button-corners" min="0" max="25" value="10">

    </form>
</div>
            
          
!
            
              

.button {
    background-color:#ff6347;
    color: #ffffff;
    display: inline-block;
    padding: .5rem 1rem;
    border-radius: 3px;
    text-decoration: none;
}

:root {
    --button-background:#ff6347;
    --button-foreground:#ffffff;
    --button-display: inline-block;
    --button-padding: .5rem 1rem;
    --button-corners: 3px;
    --button-decoration: none;
    --button-text-align: center;
}

.button-withvars {
    background-color: var(--button-background);
    color: var(--button-foreground);
    display: var(--button-display);
    padding: var(--button-padding);
    border-radius: var(--button-corners);
    text-decoration: var(--button-decoration);
    text-align: var(--button-text-align);
}


.special {
    --button-background: lightblue;
    --button-foreground: #333;
    --button-display: block;
}
.darkModeToggle {
        display: none;
    }
@supports (--variable: 1px) {
    .darkModeToggle {
        display: var(--button-display);
    }
}

.darkMode {
    --button-background: #7d483e;
    --button-foreground: #eee;    
    --color: #fff;
    --bg-color: #333;
}


.themed {
}




.container {
    width: 95vw;
    max-width: 750px;
    margin: 20vh auto;
    padding: 2rem;
    box-shadow: 0px 0px 3px var(--color);
}


body {
    margin: 4rem;
    color: var(--color);
    background-color: var(--bg-color);
}
html {
    font-size: 125%;
    line-height: 1.4em;
}

h1, h2, h3, h4 {
    line-height: 1.25em;
}


// Cheating for dark mode

:root {
    --color:#333333;
    --bg-color:#ffffff;
}
            
          
!
            
              let darkModeToggle = document.querySelectorAll('.darkModeToggle');
let body = document.querySelector('body');
darkModeToggle.forEach(toggler => toggler.addEventListener('click', e => {
    e.preventDefault();
    body.classList.toggle('darkMode')
}));






const inputs = Array.from(document.querySelectorAll('.theme-change input, .theme-change select')); // Create an array of form fields

inputs.forEach(input => { 
    setInitialValues(input);
    input.addEventListener('change', handleUpdate);
    input.addEventListener('mousemove', handleUpdate);
});

function handleUpdate(e) {
    let newValue = this.dataset.suffix ? `${this.value}px` : this.value;
    document.documentElement.style.setProperty(`--${this.id}`, newValue);
}

function setInitialValues(input) {
    let cssProperty = getComputedStyle(document.documentElement).getPropertyValue(`--${input.id}`);
    let updatedValue = input.dataset.suffix ? cssProperty.replace("px", "") : cssProperty;
    console.log(updatedValue);
    input.value = updatedValue;
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console