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="container">
<button>General Sibling Selector (<code>~</code>)</button>
<button>Adjacent Sibling Selector (<code>+</code>)</button>
<button>Clickable Label for Checkbox</button>
<h3>Click on the buttons above to learn more about the related CSS concepts</h3>
<!--CORRESPONDS TO THE GENERAL SIBLING SELECTOR-->
<p class="general"><strong>Description:</strong> This selector will select any sibling(s) (in our case the first three paragraphs) that follow(s) the former specified element (in our case the <code>h3</code> element) supposing that both share the same parent.
<span><strong>Example:</strong> <code>h3 ~ p { border: 1px solid red; }</code>.</span>
</p>
<!--CORRESPONDS TO THE ADJACENT SIBLING SELECTOR-->
<p class="adjacent"><strong>Description:</strong> This selector will select only the first sibling (in our case the first paragraph) that immediately follows the former specified element (in our case the h3 element).
<span><strong>Example:</strong> <code>h3 + p { border: 1px solid red; }</code>. </span>
</p>
<!--CORRESPONDS TO THE CHECKBOX-->
<section class="label">
<p>There are two possible ways to associate a <code>label</code> with a form control (in our case <code>the input[type="checkbox"]</code> element).</p>
<p>First, by taking advantage of the label's <code>for</code> attribute. The value of it should match the <code>id</code> attribute value of the corresponding checkbox (see example code for more info).
<span><strong>Example:</strong></span>
</p>
<input type="checkbox" name="checkbox" id="check_id" value="value">
<label for="check_id">Label for Checkbox</label>
<p>Second, by wrapping the form control within a <code>label</code> element (see example code for more info).
<span><strong>Example:</strong></span>
</p>
<label>
<input type="checkbox" name="checkbox" value="value">Label for Checkbox
</label>
<h4><code>:checked</code> pseudo-class</h4>
<p>The <code>:checked</code> pseudo-class matches checkboxes and radio buttons that are checked ("on" state).</p>
<p>This capability provides us a way to mimic the Javascript's <code>onclick</code> event.</p>
<p>The <strong>checkbox hack</strong> is a way to create Javacript-based things like accordions and off canvas menus by taking advantage of the <code>:checked</code> pseudo-class and the label that is associated to this checkbox.</p>
<span class="last"><strong>Example:</strong></span>
<p>In the example below, we're using the adjacent sibling selector along with the <code>:checked</code> pseudo-class to change the appearance of the label. So, first we hide the checkbox. Then, when we click on the corresponding label we set its background
color to yellow.</p>
<div class="checkbox-example">
<input type="checkbox" id="check_example">
<label for="check_example">When you click me. I 'll have the yellow as background color.</label>
</div>
<h4>Browser Support</h4>
<table>
<thead>
<tr>
<th>Chrome</th>
<th>Mozilla</th>
<th>IE</th>
<th>Opera</th>
<th>Safari</th>
</tr </thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>9</td>
<td>9</td>
<td>3.1</td>
</tr>
</tbody>
</table>
</section>
<!--CORRESPONDS TO THE GENERAL AND ADJACENT SIBLING SELECTORS-->
<section class="example">
<h3>This is an <code>h3</code></h3>
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>
<p>This is the third paragraph</p>
<div>
<p>This is a paragraph nested inside a <code>div</code></p>
</div>
</section>
<!--CORRESPONDS TO THE GENERAL AND ADJACENT SIBLING SELECTORS-->
<section class="br-support">
<h4>Browser Support</h4>
<table>
<thead>
<tr>
<th>Chrome</th>
<th>Mozilla</th>
<th>IE</th>
<th>Opera</th>
<th>Safari</th>
</tr </thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>7</td>
<td>9</td>
<td>3</td>
</tr>
</tbody>
</table>
</section>
<!--CORRESPONDS TO THE GENERAL SIBLING SELECTOR-->
<section class="rs-general">
<h4>Useful Resources</h4>
<ul>
<li><a href="http://dev.w3.org/csswg/selectors-3/#general-sibling-combinators">http://dev.w3.org/csswg/selectors-3/#general-sibling-combinators</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors">https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors</a></li>
</ul>
</section>
<!--CORRESPONDS TO THE ADJACENT SIBLING SELECTOR-->
<section class="rs-adjacent">
<h4>Useful Resources</h4>
<ul>
<li><a href="http://dev.w3.org/csswg/selectors-3/#adjacent-sibling-combinators">http://dev.w3.org/csswg/selectors-3/#adjacent-sibling-combinators</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors" target="_blank">https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors</a></li>
</ul>
</section>
<!--CORRESPONDS TO THE CHECKBOX-->
<section class="rs-checkbox">
<h4>Useful Resources</h4>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/checkbox">https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/checkbox</a></li>
<li><a href="http://dev.w3.org/csswg/selectors-4/#checked"> http://dev.w3.org/csswg/selectors-4/#checked</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/CSS/%3Achecked">https://developer.mozilla.org/en-US/docs/Web/CSS/%3Achecked</a></li>
<li><a href="http://stackoverflow.com/questions/6293588/how-to-create-an-html-checkbox-with-a-clickable-label"> http://stackoverflow.com/questions/6293588/how-to-create-an-html-checkbox-with-a-clickable-label</a></li>
</ul>
<h4>Articles on SitePoint related to the checkbox (or radio button) hack</h4>
<ul>
<li><a href="http://www.sitepoint.com/replacing-radio-buttons-without-replacing-radio-buttons/">Replacing Radio Buttons Without Replacing Radio Buttons</a></li>
<li><a href="http://www.sitepoint.com/pure-css-off-screen-navigation-menu/">Pure CSS Off-screen Navigation Menu</a></li>
</ul>
</section>
</div>
.container {
max-width: 700px;
margin: auto;
text-align: center;
padding: 0 20px;
line-height: 1.3;
}
.container * {
text-align: left;
}
button {
margin-top: 20px;
}
button:first-child,
input {
margin-left: 10px;
}
.general,
.adjacent,
section {
display: none;
}
.container span,
.container a {
display: block;
}
span {
margin-top: 10px;
margin-bottom: 30px;
}
.label span {
margin-bottom: 0;
}
[type="checkbox"] + label {
margin-bottom: 20px;
}
p:not(.general):not(.adjacent) {
width: 80%;
}
section,
span,
strong,
[type="checkbox"]:last-child,
div,
code {
margin-left: 0;
}
table th,
table td {
border: 1px solid #ccc;
}
table th {
padding: 10px;
}
table td {
padding: 5px;
text-align: center;
}
.active {
background: tomato;
}
.borderClass {
border: 1px solid tomato;
}
.last {
margin-left: 10px;
}
/* CHECKBOX STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */
.checkbox-example [type="checkbox"] {
position: absolute;
left: -9999px;
}
.checkbox-example [type="checkbox"] + label {
width: 20%;
display: block;
cursor: pointer;
padding: 5px;
border: 1px solid tomato;
margin-top: 30px;
}
.checkbox-example [type="checkbox"]:checked + label {
background: yellow;
}
// Demo by George Martsoukos. See article:
// FIRST BUTTON
$('button:nth-child(1)').on('click', function() {
$('h3:first-of-type, .adjacent, .label, .rs-adjacent, .rs-checkbox').hide();
$('.general, .example, .br-support, .rs-general').show();
$('p').removeClass('borderClass');
$('button').removeClass('active');
$('.example h3 ~ p').addClass('borderClass');
$(this).addClass('active');
});
// SECOND BUTTON
$('button:nth-child(2)').on('click', function() {
$('h3:first-of-type, .general, .label, .rs-general, .rs-checkbox').hide();
$('.adjacent, .example, .br-support, .rs-adjacent').show();
$('p').removeClass('borderClass');
$('button').removeClass('active');
$('.example h3 + p').addClass('borderClass');
$(this).addClass('active');
});
// THIRD BUTTON
$('button:nth-child(3)').on('click', function() {
$('h3:first-of-type, .adjacent, .general, .example, .br-support, .rs-general, .rs-adjacent').hide();
$('.label, .rs-checkbox').show();
$('button').removeClass('active');
$(this).addClass('active');
});
Also see: Tab Triggers