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 URL's 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 it's URL and the proper URL extention.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<!--
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>
/*
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;
}
// 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");
});
Also see: Tab Triggers