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.
<div class="content">
<details>
<summary>Notes on using Switches</summary>
<p>
A situation one should try to avoid is having "switches" that run full width of a browser's viewport. As rarely is the label of a switch alone an effective means to determine the current state of the control.
</p>
<p>
When they label and UI of the control are positioned on opposite ends of the viewport, people who may be using software to magnify portions of their screen at a time are likely not going to see the label and the state in the same magnified window.
</p>
<p>
Keeping the label and the switch UI closer together will help ensure that the state and label will be easier to visually associate with each other. It also increases the likelihood that they may both be visible in a magnified portion of hte screen.
</p>
<hr>
<p>
While these elements are visually represented as switch controls, they're semantically conveyed as checkboxes. Use caution when representing an element as one type of control to some people, but announcing it as a different control to others.
</p>
<hr>
<p>
Note that the example 2a and b "switches" will not render exactly as intended with IE11, or in earlier versions of Firefox (pre 60).
</p>
</details>
</div>
<div style="max-width: 90%; margin: 0 auto 1.5em;">
<label class="check-switch">
Example 1a:
<input type="checkbox">
<span aria-hidden="true"></span>
</label>
<div class="switchbox">
<label for="switchbox">
Example 2a:
</label>
<input type="checkbox" id="switchbox">
</div>
</div>
<div style="width: 18em; margin: auto;">
<label class="check-switch">
Example 1b:
<input type="checkbox">
<span aria-hidden="true"></span>
</label>
<div class="switchbox">
<label for="switchbox2">
Example 2b:
</label>
<input type="checkbox" id="switchbox2">
</div>
</div>
/* containing label */
.check-switch {
display: block;
overflow: hidden;
padding: .5em;
position: relative;
&:not(:last-child) {
border-bottom: 1px solid #565656;
}
}
/* provide affordance that the entire row is
actionable */
.check-switch:hover {
box-shadow: 0 0 2px 1px #2196f3;
}
/* using the before/after pseudo elements of the span to create the "switch" */
.check-switch span:before,
.check-switch span:after {
border: 1px solid #565656;
content: "";
position: absolute;
top: 50%;
transform: translateY(-50%);
}
/* styling specific to the knob of the switch */
.check-switch span:after {
background: #fff;
border-radius: 100%;
height: 1.5em;
right: 1.5em;
transition: right .1825s ease-in-out;
width: 1.5em;
}
/* styling specific to the knob "container" */
.check-switch span:before {
background: #eee;
border-radius: 1.75em;
height: 1.75em;
right: .25em;
transition: background .2s ease-in-out;
width: 2.75em;
}
/* hide the actual checkbox from view, but not from keyboards or ATs.
Instead of standard visually hidden styling, instead set opacity to
almost 0 (not zero for ChomeVox legacy bug), pointer-events none, and
then set to full height/width of container element so that VO focus
ring outlines the component, instead of a tiny box within the component
*/
.check-switch input {
height: 100%;
left: 0;
opacity: .0001;
position: absolute;
top: 0;
width: 100%;
}
.check-switch input:focus + span:before {
outline: 2px solid;
}
/* change the position of the knob to indicate it has been checked*/
.check-switch input:checked + span:after {
right: .25em;
}
/* update the color of the "container" to further visually indicate state */
.check-switch input:checked + span:before {
background: #2196f3;
}
/* 'color in' the switch knob in high contrast mode by giving it
a large border */
@media screen and (-ms-high-contrast: active) {
.check-switch span:after {
background-color: windowText;
}
}
/**
* Example 2a/b CSS
* Revised from: https://codepen.io/scottohara/pen/KoMKXe
*
* Forked from Richard Keizer:
* https://codepen.io/rakeizer/pen/QQRBoZ
*/
.switchbox {
display: flex;
&:not(:last-child) {
border-bottom: 1px solid #565656;
}
}
/* provide affordance that the entire row is
actionable */
.switchbox:hover {
box-shadow: 0 0 2px 1px #2196f3;
}
.switchbox input {
-moz-appearance: none;
-o-appearance: none;
-webkit-appearance: none;
background-color: #fafafa;
border-radius: .825em;
border: 1px solid #565656;
box-shadow: inset -1.125em 0 0 1px rgba(120, 120, 120, 1);
height: 1.5em;
margin-right: .25em;
outline: none;
position: relative;
transition: box-shadow .2s ease-in-out;
width: 2.5em;
}
.switchbox input:focus {
outline: 2px solid;
outline-offset: 4px;
}
.switchbox input:checked {
border: 1px solid rgba(0, 2, 120, 1);
box-shadow: inset 1.125em 0 0 1px #2196f3;
}
/* remove the possibility of Microsoft's checkbox "check"
showing up uninvited */
.switchbox input::-ms-check {
opacity: 0;
}
.switchbox label {
flex: 1;
padding: .5em;
}
.switchbox > * {
align-self: center;
}
/**
* Demo styles
* Styling specific to the content and wrapping
* elements of each styled form control,
*/
html,
body {
background: #fcfcfc;
font-family: arial;
font-size: 100%;
margin: 0;
padding: 0;
}
body {
padding: 1.5em;
}
*,
*:before,
*:after {
box-sizing: border-box;
}
input {
font-size: inherit; // makes the input based switch larger
line-height: normal;
}
.content {
max-width: 40em;
margin: auto;
}
/*
Checkbox inputs posing as "switches".
Ideally if needs to redesign a checkbox to look like a switch, then the control would also announce itself and act like a swtich to assistive technologies, such as screen readers.
See:
https://scottaohara.github.io/a11y_styled_form_controls/src/checkbox--switch/
*/
Also see: Tab Triggers