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.
<p class="warning">
This is an old and outdated implementation. Check the <a href="https://codepen.io/gsarig/pen/ExKJEoM" target="_blank">updated pen</a> and <a class="more" href="https://www.gsarigiannidis.gr/tabs-on-desktop-accordion-on-mobile/?GWgNdx" target="_blank">read more about the logic</a>
</p>
<!-- THE MARKUP:
<article class="tabbed-content tabs-side">
<nav class="tabs">
<ul>
<li><a href="#{ID}" class="active">{TITLE}</a></li>
</ul>
</nav>
<section id="{ID}" class="item active" data-title="{TITLE}">
<div class="item-content">
{CONTENT}
</div>
</section>
</article>
-->
<h2>Tabs on top</h2>
<article class="tabbed-content">
<nav class="tabs">
<ul>
<li><a href="#tab1" class="active">Tab 1</a></li>
<li><a href="#tab2">Tab 2</a></li>
<li><a href="#tab3">Tab 3</a></li>
</ul>
</nav>
<section id="tab1" class="item active" data-title="Tab 1">
<div class="item-content">
<p>A simple solution for having tabbed content on a large screen but turn it into accordion on a smaller one. Just resize your screen below 800 pixels to see the tabbed interface transforming into an accordion.</p>
<p>In order to keep the HTML as clean as possible, without duplicating elements, we use element attributes as CSS content to display the accordion tabs.</p>
</div>
</section>
<section id="tab2" class="item" data-title="Tab 2">
<div class="item-content">
Tab 2 content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
</div>
</section>
<section id="tab3" class="item" data-title="Tab 3">
<div class="item-content">
Tab 3 content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
</div>
</section>
</article>
<h2>Tabs on the side</h2>
<article class="tabbed-content tabs-side">
<nav class="tabs">
<ul>
<li><a href="#side_tab1" class="active">Tab 1</a></li>
<li><a href="#side_tab2">Tab 2</a></li>
<li><a href="#side_tab3">Tab 3</a></li>
</ul>
</nav>
<section id="side_tab1" class="item active" data-title="Tab 1">
<div class="item-content">
<p>This example brings the tabs at the left side by simply adding an extra class "tabs-side" at the ".tabbed-content" container. Just resize your screen below 800 pixels to see the tabbed interface transforming into an accordion.</p>
<p>The change applies only on large screens. On smaller screens (in this example: below 800 pixels) it falls back to the same accordion we have on the previous example.</p>
</div>
</section>
<section id="side_tab2" class="item" data-title="Tab 2">
<div class="item-content">
Tab 2 content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
</div>
</section>
<section id="side_tab3" class="item" data-title="Tab 3">
<div class="item-content">
Tab 3 content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
</div>
</section>
</article>
p.warning {
text-align: center;
line-height: 2;
a {
background: #3bacff;
color: #fff;
padding: .2rem .5rem;
border-radius: 3px;
white-space: nowrap;
&.more {
background: #45ba04;
}
}
}
/*
SCSS variables and mixins
*/
$color__main: #444;
$color__active: grey;
$background__active: #eee;
$width__tabs-side: 150px;
$breakpoint: 800px;
@mixin tab-active() {
background: $color__main;
color: $background__active;
}
/*
Some styles to make this demo look pretty (or at least decent)
If you want to style everything yourself, you shouldn't need these
*/
@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,900');
body {
padding: 1em;
background: $background__active;
color: $color__main;
font-family: 'Roboto', sans-serif;
font-weight: 100;
a {
text-decoration: none;
color: $color__main;
}
p {
margin-top: 0;
}
div {
box-sizing: border-box;
}
}
h2 {
margin: 2em auto 0;
text-align: center;
}
.tabbed-content {
background: #fff;
box-shadow: 1px 1px 6px #ccc;
max-width: 600px;
padding: 1em;
margin: 1em auto;
}
.tabs {
ul {
margin: 0;
padding: 0 0 1em 0;
font-weight: bold;
li {
background: $background__active;
a {
padding: .5em 1em;
&:hover,
&.active {
@include tab-active();
}
}
}
}
}
.item {
margin-bottom: 2px;
&::before {
cursor: pointer;
font-weight: bold;
background: $background__active;
padding: .5em;
display: block;
}
&.active {
&::before {
@include tab-active();
}
.item-content {
padding: 1em;
-webkit-transition: opacity 0.3s ease-in-out;
-moz-transition: opacity 0.3s ease-in-out;
-o-transition: opacity 0.3s ease-in-out;
-ms-transition: opacity 0.3s ease-in-out;
transition: opacity 0.3s ease-in-out;
}
}
}
@media all and (min-width: $breakpoint ) {
.item {
&.active {
.item-content {
padding-top: 0;
}
}
}
.tabs-side {
.tabs {
li {
margin-bottom: 2px;
}
}
}
}
/*
The project specific CSS starts here
This is the minimum CSS that you will need in order for this to work
*/
// Accordion for small screens (mobile first principle)
.tabbed-content {
.tabs {
display: none;
}
.item {
min-height: 2em; // We need a minimum height for each item or else the accordion buttons would dissapear
&::before {
content: attr(data-title); // Instead of polluting the HTML with a duplicate set of tabs, we get the accordion using the data-title attribute.
}
.item-content { // We hide the inactive content with zero opacity instead of "display:none" because this allows us to apply transition effects if we want.
opacity: 0;
visibility: hidden;
height: 0;
}
&.active {
.item-content {
opacity: 1;
visibility: visible;
height:auto;
}
}
}
}
// Tabs for larger screens
@media all and (min-width: $breakpoint) {
.tabbed-content {
.tabs {
display: block;
li {
display: inline-block;
a {
display: block;
}
}
}
.item {
min-height: 0;
&::before {
display: none;
}
}
// Tabs on the side (works only on big screens)
&.tabs-side {
.tabs {
width: $width__tabs-side;
float: left;
li {
display: block;
}
}
.item {
margin-left: $width__tabs-side;
}
}
}
}
tabControl();
/*
We also apply the switch when a viewport change is detected on the fly
(e.g. when you resize the browser window or flip your device from
portrait mode to landscape). We set a timer with a small delay to run
it only once when the resizing ends. It's not perfect, but it's better
than have it running constantly during the action of resizing.
*/
var resizeTimer;
$(window).on('resize', function(e) {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
tabControl();
}, 250);
});
/*
The function below is responsible for switching the tabs when clicked.
It switches both the tabs and the accordion buttons even if
only the one or the other can be visible on a screen. We prefer
that in order to have a consistent selection in case the viewport
changes (e.g. when you esize the browser window or flip your
device from portrait mode to landscape).
*/
function tabControl() {
var tabs = $('.tabbed-content').find('.tabs');
if(tabs.is(':visible')) {
tabs.find('a').on('click', function(event) {
event.preventDefault();
var target = $(this).attr('href'),
tabs = $(this).parents('.tabs'),
buttons = tabs.find('a'),
item = tabs.parents('.tabbed-content').find('.item');
buttons.removeClass('active');
item.removeClass('active');
$(this).addClass('active');
$(target).addClass('active');
});
} else {
$('.item').on('click', function() {
var container = $(this).parents('.tabbed-content'),
currId = $(this).attr('id'),
items = container.find('.item');
container.find('.tabs a').removeClass('active');
items.removeClass('active');
$(this).addClass('active');
container.find('.tabs a[href$="#'+ currId +'"]').addClass('active');
});
}
}
Also see: Tab Triggers