Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <nav class="nav">
  <ul class="nav__list">
    <li class="nav__item">
      <a class="nav__link is-active" href="#dashboard">Dashboard</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#lorem">Lorem</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#dolor">Doloripsumdot</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#thing">Thing</a>
    </li>
    <li class="nav__item">
      <a class="nav__link" href="#other-thing">Other Thing</a>
    </li>
  </ul>
  <div class="nav__shadow nav__shadow--start"></div>
  <div class="nav__shadow nav__shadow--end"></div>
</nav>

<div id="dashboard" class="container page is-active">
  <h1>Dashboard</h1>
  <h2><a href="#lorem">Lorem</a></h2>
  <p>Lorem Ipsum</p>
  <hr>
  
  <h2><a href="#dolor">Doloripsumdot</a></h2>
  <p>Lorem Ipsum</p>
  <hr>
  
  <h2><a href="#thing">Thing</a></h2>
  <p>Lorem Ipsum</p>
  <hr>
  
  <h2><a href="#other-thing">Other Thing</a></h2>
  <p>Lorem Ipsum</p>
</div>

<div id="lorem" class="container page">
  <h1>Lorem</h1>
</div>

<div id="dolor" class="container page">
  <h1>Doloripsumdot</h1>
</div>

<div id="thing" class="container page">
  <h1>Thing</h1>
</div>

<div id="other-thing" class="container page">
  <h1>Other thing</h1>
</div>

              
            
!

CSS

              
                $nav-height: 3em;
$nav-scrollbar-height: 1.5em;
$nav-shadow-width: 4.5em;

.nav {
  position: relative;
  height: $nav-height;
  display: flex;
  align-items: center;

  @media (pointer: coarse) {
    // Hide the scrollbar on devices with touch input.
    overflow: hidden;
  }
}

.nav__list {
  position: relative;
  display: flex;
  margin-left: 1em;
  margin-right: 1em;
  padding: 0;
  align-items: center;
  list-style-type: none;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;

  @media (pointer: coarse) {
    // Move the scrollbar outside of the visible area
    // by making the element taller than the parent element.
    height: $nav-height + $nav-scrollbar-height;
  }
}

.nav__item {
  flex-shrink: 0;
  
  &:not(:first-child) {
    margin-left: 1em;
  }
}

.nav__link {
  display: inline-flex;
  height: $nav-height;
  align-items: center;
  text-decoration: none;
  
  &.is-active {
    font-weight: bold;
  }
}

.nav__shadow {
  width: $nav-shadow-width;
  height: $nav-height;
  position: absolute;
  top: 0;
  // Using 0% rgba value instead of transparent because of Safari.
  background: linear-gradient(to right, rgba(#fff, 0), #fff 80%);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s;
  
  &.is-visible {
    opacity: 1;
  }
}

.nav__shadow--start {
  left: 0;
  transform: rotate(180deg);
}

.nav__shadow--end {
  right: 0;
}

.page {
  display: none;
  
  &.is-active {
    display: block;
  }
}

.container {
  padding-right: 1em;
  padding-left: 1em;
}

body {
  background: #fff;
  margin: 2em auto;
  width: 20em;
  padding-top: 1em;
  padding-bottom: 1em;
  border: 0.25em solid #444;
  border-radius: 1em;
}

hr {
  margin-top: 1em;
  margin-bottom: 1em;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
              
            
!

JS

              
                const $navList = document.querySelector('.nav__list');
const $body = document.querySelector('body');
const $shadowStart = document.querySelector('.nav__shadow--start');
const $shadowEnd = document.querySelector('.nav__shadow--end');

function activatePage($page) {
  const $activePage = document.querySelector('.page.is-active');
  
  if ($activePage) {
    $activePage.classList.remove('is-active');
  }
  
  $page.classList.add('is-active');
}

function activateItem($item, $container) {
  const href = $item.getAttribute('href');
  const $navItem = $container.querySelector(`[href="${href}"]`);
  const $activeItem = $container.querySelector('a.is-active');
  const $page = document.querySelector(href);
  
  if ($activeItem) {
    $activeItem.classList.remove('is-active');
  }
  
  $navItem.classList.add('is-active');

  activatePage($page);
}

function handleShadowVisibility() {
  const maxScrollStartReached = $navList.scrollLeft <= 0;
  const maxScrollEndReached = $navList.scrollLeft >= $navList.scrollWidth - $navList.offsetWidth;

  toggleShadow($shadowStart, maxScrollStartReached);
  toggleShadow($shadowEnd, maxScrollEndReached);
}

function toggleShadow($el, maxScrollReached) {
  const shadowIsVisible = $el.classList.contains('is-visible');
  const showShadow = !maxScrollReached && !shadowIsVisible;
  const hideShadow = maxScrollReached && shadowIsVisible;

  // Using requestAnimationFrame for optimal scroll performance.
  // https://stackoverflow.com/a/44779316
  if (showShadow) {
    window.requestAnimationFrame(() => $el.classList.add('is-visible'));
  } else if (hideShadow) {
    window.requestAnimationFrame(() => $el.classList.remove('is-visible'));
  }
}

$navList.addEventListener('scroll', (e) => handleShadowVisibility(e));

$body.addEventListener('click', (e) => {
  const $target = e.target;
  const isLink = $target.getAttribute('href').indexOf('#') === 0;
  
  if (isLink) {
    e.preventDefault();
    activateItem($target, $navList);
  }
});

handleShadowVisibility();

              
            
!
999px

Console