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.
<fieldset class="radio-switch">
<legend>
Settings
</legend>
<input type="radio" name="lol" id="public">
<label for="public">
On
</label>
<input type="radio" name="lol" id="private">
<label for="private">
Off
</label>
</fieldset>
<!--
Note:
The problem with this implementation, specifically the use of pseudo elements to make the Switch UI, is that the UI itself is split down the middle in where a user can click to toggle the current selection.
Ideally, the text "on" or "off" could be clicked to update the selected option...but it'd be nicer if the full toggle UI could also be clicked to toggle the options back and forth. I'm sure this could still be done with this implemetnation, but as it stands right now, it doesn't fully allow that.
-->
/*
get rid of the fieldset styling and keep
this all on a single line
*/
.radio-switch {
border: none;
padding: 0;
white-space: nowrap;
}
/*
radio button groups often benefit from a legend to
provide context as to what the different
options pertain to. Ideally this would be visible to all
users, but you know...
*/
.radio-switch legend {
font-size: 2px;
opacity: 0;
position: absolute;
}
/*
relative labels to help position the pseudo elements
the z-index will be handy later, when the labels that
overlap the visual switch UI need to be adjusted
to allow for a user to toggle the switch without
having to move their mouse/finger to the different
sides of the UI
*/
.radio-switch label {
display: inline-block;
line-height: 2;
position: relative;
z-index: 2;
}
/*
inputs set to opcacity 0 are still accessible.
Apparently there can be issues targetting inputs with
Dragon speech recognition software if you use the typical
'visually-hidden' class...so might as well just avoid that issue...
*/
.radio-switch input {
opacity: 0;
position: absolute;
}
/*
a 2 option toggle can only have 2 options...so instead of
adding more classes, i'm just going to use some
structural pseudo-classes to target them...
cause why let all that good work go to waste?!
the large padding is used to position the labels
on top of the visual UI, so the switch UI itself
can be mouse clicked or finger tapped to toggle
the current option
*/
.radio-switch label:first-of-type {
padding-right: 5em;
}
.radio-switch label:last-child {
margin-left: -4.25em;
padding-left: 5em;
}
/*
oh focus within, I can't wait for you to have even more support.
But you'll never be in IE11, so we're going to need a
polyfill for you for a bit...
*/
.radio-switch:focus-within label:first-of-type:after {
box-shadow: 0 0 0 2px #fff, 0 0 0 4px #2196f3;
}
/* polyfill class*/
.radio-switch.focus-within label:first-of-type:after {
box-shadow: 0 0 0 2px #fff, 0 0 0 4px #2196f3;
}
/* making the switch UI. */
.radio-switch label:first-of-type:before,
.radio-switch label:first-of-type:after {
border: 1px solid #aaa;
content: "";
height: 2em;
overflow: hidden;
pointer-events: none;
position: absolute;
vertical-align: middle;
}
.radio-switch label:first-of-type:before {
background: #fff;
border: 1px solid #aaa;
border-radius: 100%;
position: absolute;
right: -.075em;
transform: translateX(0em);
transition: transform .2s ease-in-out;
width: 2em;
z-index: 2;
}
.radio-switch label:first-of-type:after {
background: #222;
border-radius: 1em;
margin: 0 1em;
transition: background .2s ease-in-out;
width: 4em;
}
/*
Visually change the switch UI to match the
checked state of the first radio button
*/
.radio-switch input:first-of-type:checked ~ label:first-of-type:after {
background: #2196f3;
}
.radio-switch input:first-of-type:checked ~ label:first-of-type:before {
transform: translateX(-2em);
}
/* Move the 2nd label to have a lower z-index, so when that
option is toggled, the first label will overlay on top of the
Switch ui, and the switch can be pressed again to toggle back
to the prevoius state. */
.radio-switch input:last-of-type:checked ~ label:last-of-type {
z-index: 1;
}
/*
system defaut fonts sure do load quick...
*/
body {
padding: 3em;
font-family: arial;
}
/**
Focus within pollyfill
Credit: https://gist.github.com/aFarkas/a7e0d85450f323d5e164
*/
(function(window, document){
'use strict';
var slice = [].slice;
var removeClass = function(elem){
elem.classList.remove('focus-within');
};
var update = (function(){
var running, last;
var action = function(){
var element = document.activeElement;
running = false;
if(last !== element){
last = element;
slice.call(document.getElementsByClassName('focus-within')).forEach(removeClass);
while(element && element.classList){
element.classList.add('focus-within');
element = element.parentNode;
}
}
};
return function(){
if(!running){
requestAnimationFrame(action);
running = true;
}
};
})();
document.addEventListener('focus', update, true);
document.addEventListener('blur', update, true);
update();
})(window, document);
Also see: Tab Triggers