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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 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.

HTML

              
                <!--
This div defines the theme color variable in an inline style.

This inline style is then changed in the JS to change the theme color.
-->
<div class="container" style="--theme-color: #e00">
  <nav>
    <h1>Site Title</h1>
    <ul class="links">
      <li>Link 1</li>
      <li>Link 2</li>
      <li>Link 3</li>
    </ul>
  </nav>
  <div class="change_color">
    <h1>Change Theme Color:</h1>
    <p>Clicking each of the buttons changes the "--theme-color" CSS variable through JS.</p>
    <button class="active" id="redBtn"></button>
    <button id="greenBtn"></button>
    <button id="blueBtn"></button>
  </div>
  <div class="content">
    <h1>Some Content</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur</p>
  </div>
</div>
              
            
!

CSS

              
                /*
Demo from an article on my personal website about the CSS Variables: https://xtrp.io/blog/2020/01/27/css-variables-explained-with-demo/.

Give this CodePen a ❤️ if you liked it and check out the article :)
*/

/* global styles */
* {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

/* style container */
div.container {
  width: 100%;
  padding: 0 calc((100% - 750px) / 2);
  box-sizing: border-box;
}

@media only screen and (max-width: 775px) {
  div.container {
    padding: 1.5rem;
  }
}

/* style nav with CSS variable as color */
nav {
  height: 3.5rem;
  width: 100%;
  display: block;
  border-bottom: 1px solid #cccccc;
  
  /* set color to theme color */
  color: var(--theme-color);
}

nav h1 {
  font-size: 1.55rem;
  line-height: calc(3.5rem + 1px);
  letter-spacing: -.03rem;
  float: left;
}

nav ul.links {
  float: right;
  height: 3.5rem;
  overflow: hidden;
}
nav ul.links li {
  display: inline-block;
  font-weight: 600;
  line-height: calc(3.5rem + 1px);
  cursor: pointer;
}
nav ul.links li:not(:last-child) {
  margin-right: 1rem;
}

/* content divs */
div.container > div {
  margin: 2rem 0;
  padding: 1.5rem;
  
  /* background is theme color CSS variable */
  background-color: var(--theme-color);
  
  color: #fff;
  border-radius: 4px;
  box-shadow: 0 .25rem 1rem rgba(0, 0, 0, .2);
}
div.container > div h1 {
  font-size: 2rem;
  line-height: 1.35;
  margin-bottom: .75rem;
}
div.container > div p {
  line-height: 1.5rem;
  font-size: .9rem;
}

/* change color div */
div.container > div.change_color {
  background-color: #ffffff;
  
  /* border color is theme color CSS variable */
  border: 3px solid var(--theme-color);
  
  /* color is theme color CSS variable */
  color: var(--theme-color);
  
  text-align: center;
}
div.container > div.change_color h1,
div.container > div.change_color p {
  text-align: left;
  margin-bottom: .5rem;
}
div.container > div.change_color p {
  margin-bottom: 1rem;
}
div.change_color button {
  width: 3.25rem;
  height: 3.25rem;
  border: none;
  margin: .5rem;
  box-shadow: 0 .1rem .5rem rgba(0, 0, 0, .1);
  border-radius: 4px;
  cursor: pointer;
  box-sizing: border-box;
  border: 3px solid transparent;
  outline: none;
}
div.change_color button.active {
  border-color: #000;
}
div.change_color button#redBtn {
  background-color: #e00;
}
div.change_color button#blueBtn {
  background-color: #00e;
}
div.change_color button#greenBtn {
  background-color: #0e0;
}
              
            
!

JS

              
                // remove active class from active btn
const removeActive = () => {
  document.querySelector("button.active").classList.remove("active");
}

// button click events
const redBtn = document.querySelector("button#redBtn");
const greenBtn = document.querySelector("button#greenBtn");
const blueBtn = document.querySelector("button#blueBtn");
const container = document.querySelector("div.container");


redBtn.addEventListener("click", () => {
  removeActive();
  redBtn.classList.add("active");
  
  // change CSS variable
  document.querySelector("div.container").setAttribute("style", "--theme-color: #e00");
});

greenBtn.addEventListener("click", () => {
  removeActive();
  greenBtn.classList.add("active");
  
  // change CSS variable
  document.querySelector("div.container").setAttribute("style", "--theme-color: #0e0");
});

blueBtn.addEventListener("click", () => {
  removeActive();
  blueBtn.classList.add("active");
  
  // change CSS variable
  document.querySelector("div.container").setAttribute("style", "--theme-color: #00e");
});
              
            
!
999px

Console