HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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><head></code> and the JS is loaded just before the closing <code></body></code> tag:</p>
<pre><code><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/theme.js/dist/theme.css">
<script src="https://cdn.jsdelivr.net/npm/theme.js/dist/theme.js"></script></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><p data-toggled="themejs">theme</p></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="themejs"] { cursor: pointer; display: none; } .themejs-active [data-toggled="themejs"] { display: block; }</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>
/* 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;
}
Also see: Tab Triggers