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.
//PEN HEADER
.header
h1.header__title Tabs Navigation UI
.header__btns.btns
a.header__btn.btn(href="https://github.com/nat-davydova/tab-navigation" title="Check on Github" target="_blanc") Check on Github
//PEN CONTENT
.content
//content title
h2.content__title Click to any navigation link
//content inner
.content__inner
//tabs
.tabs
//tabs navigation
.tabs__nav
ul.tabs__nav-list
li.tabs__nav-item.js-active Profile
li.tabs__nav-item Settings
li.tabs__nav-item About Us
li.tabs__nav-item FAQ
//tabs panels
.tabs__panels
//single panel
.tabs__panel
.tabs__panel-card
.tabs__panel-avatar
.tabs__panel-content
.tabs__panel-card
.tabs__panel-content
.tabs__panel-card
.tabs__panel-content
//single panel
.tabs__panel
.tabs__panel-card
.tabs__panel-content
.tabs__panel-content
.tabs__panel-card
.tabs__panel-content
.tabs__panel-card.tabs__panel-card--spaced-between
.tabs__panel-img
.tabs__panel-img
.tabs__panel-img
//single panel
.tabs__panel
.tabs__panel-card
.tabs__panel-content
.tabs__panel-card
.tabs__panel-content
.tabs__panel-img
.tabs__panel-card
.tabs__panel-img
.tabs__panel-content
//single panel
.tabs__panel
.tabs__panel-card
.tabs__panel-content
.tabs__panel-content
.tabs__panel-card
.tabs__panel-content
.tabs__panel-card
.tabs__panel-content
.tabs__panel-content
//mixins
@mixin transition-mix ($property: all, $duration: 0.2s, $timing: linear, $delay: 0s) {
transition-property: $property;
transition-duration: $duration;
transition-timing-function: $timing;
transition-delay: $delay;
}
@mixin position-absolute ($top: null, $left: null, $right: null, $bottom: null) {
position: absolute;
top: $top;
left: $left;
right: $right;
bottom: $bottom;
}
//variables
$theme-font-color: #2c2c2c;
//common styles
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font: {
family: 'Lato', sans-serif;
size: 16px;
}
color: $theme-font-color;
a {
color: inherit;
text-decoration: none;
}
}
.btn {
@include transition-mix;
padding: 10px 20px;
margin-right: 10px;
background-color: #fff;
border: 1px solid $theme-font-color;
border-radius: 3px;
cursor: pointer;
outline: none;
&:last-child {
margin-right: 0;
}
&:hover,
&.js-active{
color: #fff;
background-color: $theme-font-color;
}
}
//header styles
.header {
max-width: 500px;
margin: 50px auto;
text-align: center;
}
.header__title {
margin-bottom: 30px;
font: {
weight: 500;
}
}
//content styles
.content {
max-width: 700px;
margin: auto;
}
.content__title {
margin-bottom: 20px;
font: {
size: 18px;
weight: 500;
}
text-align: center;
}
.content__inner {
width: 375px;
height: 550px;
margin: auto;
box-shadow:
0 8px 17px 2px rgba(0,0,0,0.14),
0 3px 14px 2px rgba(0,0,0,0.12),
0 5px 5px -3px rgba(0,0,0,0.2);
}
//tabs styles
$tabs-padding: 15px;
.tabs {
position: relative;
padding: $tabs-padding;
height: 100%;
overflow: hidden;
}
//tabs nav styles
$theme-accent-color: #119DA4;
.tabs__nav {
position: relative;
}
//nav decoration is appended by js
.tabs__nav-decoration {
position: absolute;
top: 0;
left: 0;
height: 100%;
transition: width .2s linear 0s,
transform .2s ease-out 0s;
background-color: $theme-accent-color;
border-radius: 3px;
z-index: 1;
}
.tabs__nav-list {
position: relative;
display: flex;
justify-content: space-between;
list-style-type: none;
z-index: 5;
}
.tabs__nav-item {
@include transition-mix($delay: 0s);
padding: 15px;
cursor: pointer;
//active styles
&.js-active {
@include transition-mix($delay: .05s);
color: #fff;
}
}
$panels-bg-color: rgba(0,0,0,.15);
.tabs__panels {
position: relative;
margin-top: 30px;
}
.tabs__panel {
@include position-absolute($top: 0, $left: 0);
transition: none;
transform: scale(.8);
width: 100%;
opacity: 0;
//active panel styles
&.js-active {
transition: all .25s linear 0s;
transform: scale(1);
opacity: 1;
}
}
.tabs__panel-card {
display: flex;
margin-bottom: 30px;
padding: 15px;
box-shadow:
0 2px 2px 0 rgba(0,0,0,0.14),
0 3px 1px -2px rgba(0,0,0,0.12),
0 1px 5px 0 rgba(0,0,0,0.2);
&:last-child {
margin-bottom: 0;
}
}
.tabs__panel-card--spaced-between {
justify-content: space-between;
}
.tabs__panel-avatar {
flex-shrink: 0;
width: 100px;
height: 100px;
border-radius: 50%;
background-color: $panels-bg-color;
}
.tabs__panel-img {
flex-shrink: 0;
width: 80px;
height: 80px;
border-radius: 4px;
background-color: $panels-bg-color;
}
.tabs__panel-content {
width: 100%;
margin-left: 30px;
&:first-child {
margin-left: 0;
}
&:not(:last-child) {
margin-right: 30px;
}
&:before,
&:after {
display: block;
width: 100%;
height: 20px;
content: '';
background-color: $panels-bg-color;
}
&:before {
margin-bottom: 15px;
}
}
const DOM = {
tabsNav: document.querySelector('.tabs__nav'),
tabsNavItems: document.querySelectorAll('.tabs__nav-item'),
panels: document.querySelectorAll('.tabs__panel')
};
//set active nav element
const setActiveItem = elem => {
DOM.tabsNavItems.forEach(el => {
el.classList.remove('js-active');
});
elem.classList.add('js-active');
};
//find active nav element
const findActiveItem = () => {
let activeIndex = 0;
for(let i = 0; i < DOM.tabsNavItems.length; i++) {
if (DOM.tabsNavItems[i].classList.contains('js-active')) {
activeIndex = i;
break;
};
};
return activeIndex;
};
//find active nav elements parameters: left coord, width
const findActiveItemParams = (activeItemIndex) => {
const activeTab = DOM.tabsNavItems[activeItemIndex];
//width of elem
const activeItemWidth = activeTab.offsetWidth - 1;
//left coord in the tab navigation
const activeItemOffset_left = activeTab.offsetLeft;
return [activeItemWidth, activeItemOffset_left];
};
//appending decoration block to an active nav element
const appendDecorationNav = () => {
//creating decoration element
let decorationElem = document.createElement('div');
decorationElem.classList.add('tabs__nav-decoration');
decorationElem.classList.add('js-decoration');
//appending decoration element to navigation
DOM.tabsNav.append(decorationElem);
//appending styles to decoration element
return decorationElem;
};
//appending styles to decoration nav element
const styleDecorElem = (elem, decorWidth, decorOffset) => {
elem.style.width = `${decorWidth}px`;
elem.style.transform = `translateX(${decorOffset}px)`;
};
//find active panel
const findActivePanel = index => {
return DOM.panels[index];
};
//set active panel class
const setActivePanel = index => {
DOM.panels.forEach(el => {
el.classList.remove('js-active');
});
DOM.panels[index].classList.add('js-active');
};
//onload function
window.addEventListener('load', () => {
//find active nav item
const activeItemIndex = findActiveItem();
//find active nav item params
const [decorWidth, decorOffset] = findActiveItemParams(activeItemIndex);
//appending decoration element to an active elem
const decorElem = appendDecorationNav();
//setting styles to the decoration elem
styleDecorElem(decorElem, decorWidth, decorOffset);
//find active panel
findActivePanel(activeItemIndex);
//set active panel
setActivePanel(activeItemIndex);
});
//click nav item function
DOM.tabsNav.addEventListener('click', e => {
const navElemClass = 'tabs__nav-item';
//check if we click on a nav item
if(e.target.classList.contains(navElemClass)) {
const clickedTab = e.target;
const activeItemIndex = Array.from(DOM.tabsNavItems).indexOf(clickedTab);
//set active nav item
setActiveItem(clickedTab);
//find active nav item
const activeItem = findActiveItem();
//find active nav item params
const [decorWidth, decorOffset] = findActiveItemParams(activeItem);
//setting styles to the decoration elem
const decorElem = document.querySelector('.js-decoration');
styleDecorElem(decorElem, decorWidth, decorOffset);
//find active panel
findActivePanel(activeItemIndex);
//set active panel
setActivePanel(activeItemIndex);
}
});
Also see: Tab Triggers