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

              
                <main>

  <article>

    <h1>theme.js</h1>

    <p><a href="https://github.com/craigbuckler/theme.js"><strong>GitHub</strong></a> | <a href="https://www.npmjs.com/package/theme.js"><strong>npm</strong></a> | <a href="https://gum.co/themejs"><strong>donate</strong></a> | <a href="https://twitter.com/craigbuckler">@craigbuckler</a> | <a href="https://craigbuckler.com/">craigbuckler.com</a></p>

    <p>Simple animated light and dark theme for any website inspired by <a href="https://dev.to/wgao19/night-mode-with-mix-blend-mode-difference-23lm">Night Mode with Mix Blend Mode: Difference</a> by <a href="https://twitter.com/wgao19">@wgao19</a>.</p>

    <p>Please use the code as you wish. <a href="https://twitter.com/craigbuckler">Tweet me @craigbuckler</a> if you find it useful and <a href="https://gum.co/themejs">donate toward development</a> if you use it commercially.</p>

    <ul>
      <li>retains theme state between page loads</li>
      <li>no external dependencies - works with any framework</li>
      <li>fast and lightweight: 1,000 bytes of JavaScript, 330 bytes of CSS</li>
      <li>easy to configure in HTML and CSS</li>
      <li>works in all modern browsers</li>
      <li>fails gracefully in browsers without support</li>
      <li>includes <a href="https://github.com/craigbuckler/toggled.js">toggled.js</a> for simple UI toggle switching</li>
    </ul>

    <p>Click to test...</p>

    <p data-toggled="themejs">theme</p>

    <p><em>(Note that some websites may have incompatible CSS or require changes to theme.css.)</em></p>

    <ul>
    </ul>

    <h2>Usage</h2>

    <p>The page must load the CSS and JavaScript. It can be placed anywhere but, typically, the CSS is loaded in the <code>&lt;head&gt;</code> and the JS is loaded just before the closing <code>&lt;/body&gt;</code> tag:</p>

    <pre><code>&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/theme.js/dist/theme.css&quot;&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/theme.js/dist/theme.js&quot;&gt;&lt;/script&gt;</code></pre>

    <p>CDN URLs are shown above but you can also <code>npm i theme.js</code> to install via npm and use a bundler.</p>

    <p>Now create a theme toggle switch on your page by adding a <code>data-toggled="themejs"</code> attribute to any element, e.g.</p>

    <pre><code>&lt;p data-toggled=&quot;themejs&quot;&gt;theme&lt;/p&gt;</code></pre>

    <p>This can be styled however you prefer but it should be hidden by default and only shown then theme.js is active and a <code>themejs-active</code> class has applied to the <code>body</code> tag, e.g.</p>

    <pre><code>[data-toggled=&quot;themejs&quot;] {&#10;  cursor: pointer;&#10;  display: none;&#10;}&#10;&#10;.themejs-active [data-toggled=&quot;themejs&quot;] {&#10;  display: block;&#10;}</code></pre>

    <p>The element can now be clicked to toggle themes.</p>

    <h2>Advanced use</h2>

    <p>The following elements are added to the page so they can be altered as necessary:</p>

    <ul>
      <li><code>body.themejs-active</code> - applied when theme.js is supported</li>
      <li><code>body.themejs</code> - the alternative theme has been applied</li>
      <li><code>div.themejs-back</code> - background element, normally white</li>
      <li><code>div.themejs-overlay</code> - background overlay, normally white but can be changed</li>
    </ul>

    <p>Any elements that should not be reversed - <em>such as images</em> - should have the CSS <code>isolation: isolate;</code> property applied, e.g.</p>

    <pre><code>img, svg { isolation: isolate; }</code></pre>

    <p>This fish will remain green (<code>#393</code>) regardless of which theme is used:</p>

    <svg width="200" fill="#393" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 .315 .196" fill-rule="evenodd" clip-rule="evenodd"><path d="M.238.074c.007.01.013.015.024.021C.212.135.153.16.088.142a.154.154 0 0 1-.045.054.1.1 0 0 1-.004-.06A.087.087 0 0 1 0 .092C.028.085.05.09.074.105.11.093.124.052.152.03c.045-.037.106-.02.155 0 .007.005.01.011.007.02a.137.137 0 0 1-.044.04.05.05 0 0 1-.032-.015zM.258.03a.01.01 0 0 1 .013.004.01.01 0 0 1-.005.013.01.01 0 0 1-.013-.004A.01.01 0 0 1 .258.03z"/><path d="M.18.01A.102.102 0 0 0 .133 0C.114.027.09.047.08.08l.02.004C.117.067.131.042.15.026A.095.095 0 0 1 .182.01zM.106.15c.001.008.005.033.013.036A.16.16 0 0 1 .154.149a.171.171 0 0 1-.048 0zM.183.142l.015.031C.205.155.218.136.227.123a.227.227 0 0 1-.044.02zM.26.033a.007.007 0 0 1 .008.003.007.007 0 0 1-.003.008.007.007 0 0 1-.01-.003A.007.007 0 0 1 .26.033z"/></svg>

    <p data-toggled="themejs">theme</p>

  </article>

</main>
              
            
!

CSS

              
                /* main styles */

*, *:before, *:after {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
  font-size: 100%;
  color: #444;
  background-color: #fff;
}

article {
  max-width: 46em;
  padding: 1em 4em 4em;
  margin: 0 auto;
}

h1, h2 {
  font-weight: normal;
  margin-top: 1em;
}

p, a {
  margin-top: 1em;
}

h2 + p {
  margin-top: 0;
}

ul, ol {
  margin: 1em 0 0 2em;
}

li {
  margin-top: 0.5em;
}

pre {
  white-space: pre;
  padding: 2px 6px;
  margin-top: 1em;
  background-color: #ddd;
  border-radius: 3px;
  overflow: auto;
}

code {
  font-family: monospace;
  font-size: 1em;
}

img {
  max-width: 100%;
  isolation: isolate;
}

svg {
  display: block;
  margin: 1em auto;
  isolation: isolate;
}

[data-toggled="themejs"] {
  font-weight: bold;
  text-align: center;
  line-height: 4.8;
  width: 5em;
  height: 5em;
  margin: 1em auto;
  color: #fff;
  background-color: #333;
  border-radius: 50%;
  cursor: pointer;
  user-select: none;
  display: none;
}

.themejs-active [data-toggled="themejs"] {
  display: block;
}

              
            
!

JS

              
                
              
            
!
999px

Console