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.
<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">
<div class="container special">
<a href="#" class="button-withvars">
This is a button style
</a>
</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;
}
Also see: Tab Triggers