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 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.
<div class="tabs">
<div class="tab">
<input class="tab-radio" type="radio" id="tab-1" name="tab-group-1" checked>
<label class="tab-label" for="tab-1">Huh? Tabs again?</label>
<div class="tab-panel">
<div class="tab-content">
<h3>Why would this be cool?</h3>
<p>First look at the HTML structure: all elements related to one tab are within one block. Now think about that for a while. This must be done by Chris Coyier's <a href="https://css-tricks.com/functional-css-tabs-revisited/">Functional CSS Tabs Revisited</a> already, right?</p>
<p>Maybe. But then you may notice another thing: Chris' example uses absolute positioning for the content. This means the tabs must be of fixed height. Yet these tabs here certainly aren't!</p>
</div>
</div>
</div>
<div class="tab">
<input class="tab-radio" type="radio" id="tab-2" name="tab-group-1">
<label class="tab-label" for="tab-2">Variable height content</label>
<div class="tab-panel">
<div class="tab-content">
<h3>What is this devilry?</h3>
<p>First of all we are working with inline content here and being smart about it. We eliminate white space using the dirty but sufficient zero font-size.</p>
<p>There is one additional element in comparison to what Chris had: <code>.tab-panel</code> element, which works as a container for the <code>.tab-content</code> element. The panel is inline-block by default, which means it can take styles like overflow, position, and width and height. We set these to zero size, use <code>overflow: hidden;</code> and <code>position: relative;</code> so that <code>.tab-content</code> disappears. For the active tab we do only one style change and it happens to the <code>.tab-panel</code> element: we make it inline! Thus it no longer follows the other rules that bind inline-block. As a result it's child element will start following the flow of the next container element up in the tree.</p>
<p>As for the <code>.tab-content</code> element it is floated and has a width of 100%. The neat thing about this combination is that it forces the element to go below the row where you can see the tab buttons.</p>
</div>
</div>
</div>
<div class="tab">
<input class="tab-radio" type="radio" id="tab-3" name="tab-group-1">
<label class="tab-label" for="tab-3">Browser support</label>
<div class="tab-panel">
<div class="tab-content">
<h3>The support must be poor!</h3>
<p>The CSS only part of this solution works in Internet Explorer 9+. All the other browsers are supported from so far back that it is enough to tell this works on Firefox, Chrome, Safari and Opera.</p>
<p>The JavaScript is quite simple and it is for IE8 and below. It does no harm to other browsers, but you probably want to hide it behind IE conditional comments: <code><!--[if lte IE 8]> … <![endif]--></code></p>
</div>
</div>
</div>
<div class="tab">
<input class="tab-radio" type="radio" id="tab-4" name="tab-group-1">
<label class="tab-label" for="tab-4">Internet Explorer 8<br /><small>And IE7, why not IE6 too</small></label>
<div class="tab-panel">
<div class="tab-content">
<h3>Supporting the old</h3>
<p>Internet Explorer 8 and below do not support the required <code>:checked</code> selector. This is why there is some JavaScript. It doesn't do a whole lot really: the active <code>.tab</code> element gets class <code>checked</code> and then we use that in CSS.</p>
<p>Due to redrawing issues in IE8 we avoid using selectors like + and ~ with it. And this is also the reason we set class to the <code>.tab</code> element and not the radio button.</p>
<p>The most interesting thing with styling in IE8 and below is to keep the input element on the page: if the element is hidden using <code>display: none;</code> then we never get the click events. To achieve a hidden input we position it absolutely and use IE proprietary Alpha filter to make it invisible. Then finally <code>z-index</code> throws it behind so it can't be clicked. To target the element properly an additional class of <code>.tab-radio</code> is added to the radio button. It is for IE8- only.</p>
<p>All IE8- styles are within one media query so it's easy to remove it. It is also easy to remove IE7 and IE6 only styles by following the comments.</p>
</div>
</div>
</div>
</div>
<h3>Summary</h3>
<p>This method gives you dynamic size for both the tabs and the content area. You can make varying height tabs as you can see and it just works. The tabs can be variable width. The content can be variable height (but not variable width, although that wouldn't make sense anyway). With very little JavaScript and some more CSS we get support for IE6+.</p>
<h3>The HTML Structure</h3>
<pre><code><div class="tabs">
<div class="tab">
<input <span class="for ie8-and-below">class="tab-radio"</span> type="radio" id="tab-X" name="tab-group-Y" <span class="for default-tab">checked</span>>
<label class="tab-label" for="tab-X">TAB TITLE</label>
<div class="tab-panel">
<div class="tab-content">
TAB CONTENT GOES HERE
</div>
</div>
</div>
</div></code></pre>
.tabs {
font-size: 0;
margin: 25px 0;
}
.tabs:after {
clear: both;
content: '';
display: table;
}
.tabs .tab {
display: inline;
}
.tabs .tab-label {
background: #eee;
border: 1px solid #ccc;
display: inline-block;
font-size: 16px;
font-size: 1rem;
left: 1px;
margin-left: -1px;
padding: 10px;
position: relative;
vertical-align: bottom;
}
.tabs .tab > [type="radio"] {
clip: rect(0 0 0 0);
height: 1px;
opacity: 0;
position: fixed;
width: 1px;
z-index: -1;
}
.tabs .tab-panel {
display: inline;
display: inline-block;
overflow: hidden;
position: relative;
height: 0;
width: 0;
}
.tabs .tab-content {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
display: block;
background: white;
padding: 0 20px;
border: 1px solid #ccc;
float: left;
font-size: 16px;
font-size: 1rem;
margin-top: -1px;
width: 100%;
}
.tabs .tab [type="radio"]:checked + .tab-label {
background: white;
border-bottom-width: 0;
padding-bottom: 11px;
z-index: 1;
}
.tabs .tab [type="radio"]:checked ~ .tab-panel {
display: inline;
}
/*
=======================================
INTERNET EXPLORER 8 and below
=======================================
*/
@media \0screen\,screen\9 {
/* IE7 and IE6 */
.tabs {
zoom: 1;
}
/* IE8 and below */
.tabs .tab .tab-radio {
filter: Alpha(opacity=0);
position: absolute;
z-index: -1;
}
/* IE7 and IE6 */
.tabs .tab .tab-panel, {
display: none;
height: auto;
overflow: visible;
position: static;
width: auto;
}
/* IE7 and IE6 */
.tabs .tab-content, {
float: none;
padding-bottom: 1px;
padding-top: 1px;
width: auto;
}
/* IE8 and below */
.tabs .checked label {
background: white;
border-bottom-width: 0;
padding-bottom: 11px;
z-index: 1;
}
/* IE8 */
.tabs .checked .tab-panel {
display: inline;
}
/* IE7 and IE6 */
.tabs .checked .tab-panel, {
display: inline;
float: left;
width: 100%;
}
}
/*
=======================================
UNIMPORTANT STYLES BEGIN HERE
=======================================
*/
html {
background: #333;
color: #DDD;
padding: 1em;
}
a:link,a:visited,a:hover,a:focus,a:active {
color: #58C;
}
label {
cursor: pointer;
}
.tabs {
color: #222;
}
.tabs a:link,.tabs a:visited,.tabs a:hover,.tabs a:focus,.tabs a:active {
color: #36A;
}
body > h3 {
font-variant: small-caps;
letter-spacing: .0625em;
}
p {
line-height: 1.5;
max-width: 50em;
}
code,pre {
font-family: 'Lucida Console', monospace;
}
pre {
background: #191919;
color: #8DA;
overflow-x: auto;
padding: 1em;
}
p > code {
background: #DED;
margin: -2px;
padding: 2px;
}
.for {
display: inline-block;
position: relative;
}
.for:after {
background: #DDD;
bottom: 150%;
color: #222;
font-family: sans-serif;
font-size: 12px;
opacity: 0;
padding: .5em;
pointer-events: none;
position: absolute;
right: 0;
-webkit-transition: opacity .2s ease-in-out;
transition: opacity .2s ease-in-out;
white-space: nowrap;
}
.for:hover:after {
opacity: 1;
}
.for.default-tab {
color: #8AD;
}
.for.ie8-and-below {
color: #DA8;
}
.for.default-tab:after {
content: 'For default tab';
}
.for.ie8-and-below:after {
content: 'For IE8 and below';
}
/* and just for fun... */
@media only screen and (max-width: 43em) {
.tabs .tab > label {
background: #BBB;
border-color: #999;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
}
.tabs .tab [type="radio"]:checked + .tab-label {
background: #222;
border-color: #444;
color: #DDD;
}
}
// in HTML <head>:
// <!--[if lt IE 9]><script>window.ltIE9=true</script><![endif]-->
if(window.ltIE9) {
(function($) {
$('.tabs input[type="radio"]:checked')
.closest('.tab')
.addClass('checked');
$('html').on('click', '.tabs input', function() {
$('input[name="' + this.name + '"]')
.closest('.tab')
.removeClass('checked');
$(this)
.closest('.tab')
.addClass('checked');
});
})(window.jQuery);
}
Also see: Tab Triggers