Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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

              
                <input type="checkbox" id="main-navigation-toggle" class="btn btn--close" title="Toggle main navigation" />
<label for="main-navigation-toggle">
  <span></span>
</label>

<nav id="main-navigation" class="nav-main">
  <ul class="menu">
    <li class="menu__item">
      <a class="menu__link" href="#0">Home</a>
    </li>
    <li class="menu__item">
      <a class="menu__link" href="#0">About</a>
    </li>
    <li class="menu__item">
      <a class="menu__link" href="#0">Clients</a>
      <ul class="submenu">
        <li class="menu__item">
          <a class="menu__link" href="#0">Burger King</a>
        </li>
        <li class="menu__item">
          <a class="menu__link" href="#0">Southwest Airlines</a>
        </li>
        <li class="menu__item">
          <a class="menu__link" href="#0">Levi Strauss</a>
        </li>
      </ul>
    </li>
    <li class="menu__item">
      <a class="menu__link" href="#0">Services</a>
      <ul class="submenu">
        <li class="menu__item">
          <a class="menu__link" href="#0">Print Design</a>
        </li>
        <li class="menu__item">
          <a class="menu__link" href="#0">Web Design</a>
        </li>
        <li class="menu__item">
          <a class="menu__link" href="#0">Mobile App Development</a>
        </li>
      </ul>
    </li>
    <li class="menu__item">
      <a class="menu__link" href="#0">Contact</a>
    </li>
  </ul>
</nav>

<main class="page-container">
  <h1>Fork This Nav Challenge</h1>
  <p>Click the toggle to open the menu and see the magic. This experiment relies on only CSS to style, animate and position elements.</p>
  <p>The technique used to select siblings based on the element being hovered extends from the work done by <a href="https://codepen.io/giana">Giana</a> in <a href="https://codepen.io/giana/pen/dWJzMK/">this pen</a>.</p>
  <p>Created for the <a href="https://codepen.io/challenges/2019/august/">Navigation with Sub-Navigation</a> Fork This CodePen Challenge in August 2019. The code within has not been browser tested. This was made to experience a quick thrill with the power of CSS.<p>
</main>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Rubik+Mono+One|Roboto+Mono:400,700&display=swap');

:root {
  --color-primary: #18181A;
  --color-secondary: #75757C;
  --color-dark: #364C62;
  --color-light: #F5F5F5;
  --font-family-primary: 'Roboto Mono', monospace;
  --font-family-secondary: 'Rubik Mono One', sans-serif;
  
  // Global transition values
  --td: 150ms;
  --te: cubic-bezier(0.215, 0.61, 0.355, 1);
}

// ========================
// Global elements
// ========================

html, body {
  height: 100%;
}

body {
  color: var(--color-dark);
  font-family: var(--font-family-primary);
  overflow-x: hidden;
  position: relative;
}

h1 {
  font-size: calc(1.5rem + 4vmin);
  font-weight: 700;
  margin-bottom: 2rem;
}

p {
  line-height: 1.4;
}

a {
  font-weight: 700;
}

.page-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  justify-content: center;
  margin: 0 auto;
  max-width: 45rem;
  padding: 1rem;
  
  > * + * {
    margin-top: 1.5rem;
  }
}

// ================================
// Main navigation
// ================================

.nav-main {
  align-items: center;
  display: flex;
  height: 100%;
  justify-content: center;
  left: -100%;
  position: fixed;
  transition: 
    left 0s calc(var(--td) * 2),
    transform 0s calc(var(--td) * 2);
  width: 100%;
  
  &::before,
  &::after {
    content: '';
    background-color: var(--color-primary);
    height: 50%;
    left: 0;
    position: absolute;
    transform: translateX(-110%);
    transform-origin: 0 50%;
    transition: transform calc(var(--td) * 2) var(--td) var(--te);
    width: 100%;
    z-index: -100;
  }
  
  &::before {
    top: 0;
  }
  
  &::after {
    bottom: 0;
  }
  
  .menu__item {
    opacity: 0;
    transform: translateX(-1rem);
    transition: 
      opacity var(--td) var(--te),
      transform var(--td) var(--te);
  }
}

// ================================
// Main navigation toggle
// ================================

[id="main-navigation-toggle"] {
  opacity: 0;
  position: fixed;
  top: -100%;
  
  ~ label {
    cursor: pointer;
    position: fixed;
    right: 1rem;
    top: 1rem;
    z-index: 100;
    
    span {
      display: block;
      height: 2rem;
      padding: 0.5rem;
      position: relative;
      transition: transform calc(var(--td) * 3) var(--te);
      width: 2rem;
      
      &::before,
      &::after {
        background-color: var(--color-dark);
        bottom: 0;
        content: '';
        height: 4px;
        left: 0;
        margin: auto;
        position: absolute;
        right: 0;
        transition: transform calc(var(--td) * 3) var(--te);
        top: 0;
        width: calc(100% - 1rem);
      }

      &::before {
        transform: rotate(0) translateY(-100%);
      }

      &::after {
        transform: rotate(0) translateY(100%);
      }
    }
  }
}

// ================================
// Main navigation toggle:checked
// ================================

[id="main-navigation-toggle"]:checked ~ label {
  span {
    transform: rotate(1turn);

    &::before {
      transform: rotate(45deg);
    }

    &::after {
      transform: rotate(-45deg);
    }
  }
}

[id="main-navigation-toggle"]:checked ~ .nav-main {
  left: 0;
  transition: transform 0s;
  
  &::before,
  &::after {
    transform: translateX(0);
    transition-delay: 0s;
  }
  
  &::after {
    transition-delay: calc(var(--td) / 2);
  }
  
  .menu__item {
    opacity: 1;
    transform: translateX(0);
    transition: 
      opacity calc(var(--td) * 2) var(--te),
      transform calc(var(--td) * 2) var(--te);
  }
  
  @for $i from 1 through 5 {
    .menu__item:nth-child(#{$i}) {
      transition-delay: calc(var(--td) * 2 * (#{$i} * 0.25));
      z-index: $i * -1;
    }
  }
}

// ================================
// Menu
// ================================

.menu {
  position: relative;
  text-align: center;
  z-index: 1;
  
  > .menu__item {
    font-family: var(--font-family-secondary);
    font-size: 10vmin;
  }
}

// ================================
// Submenu
// ================================

.submenu {
  left: 0;
  opacity: 0;
  position: absolute;
  transform: translateY(-10%);
  top: 100%;
  width: 100%;
  visibility: hidden;
  z-index: 2;
  
  .menu__item {
    font-family: var(--font-family-primary);
    font-size: 3.5vmin;
    width: 100%;
  }
  
  .menu__link {
    color: var(--color-secondary);
    text-shadow:
      1px 1px 0 var(--color-primary),
      2px 2px 0 var(--color-primary);

    &::before,
    &::after {
      display: none;
    }
  }
}

// ================================
// Menu item
// ================================

.menu__item {
  display: block;
  position: relative;
  
  &:hover .menu__link {
    &::before,
    &::after {
      animation: blink 1s var(--td) steps(1, end) forwards infinite;
      transform: translateX(calc(100% - 0.5rem));
      transition-duration: calc(var(--td) * 3);
    }
    
    &::after {
      transition-delay: calc(var(--td) / 2);
    }
  }
  
  &:hover .submenu {
    opacity: 1;
    transform: translateY(0);
    transition: 
      transform calc(var(--td) * 2) calc(var(--td) * 3) var(--te),
      opacity calc(var(--td) * 2) calc(var(--td) * 3) var(--te),
      visibility 0s calc(var(--td) * 3);
    visibility: visible;
  }
}

// ================================
// Menu link
// ================================

.menu__link {
  color: var(--color-dark);
  display: inline-block;
  font-weight: normal;
  overflow: hidden;
  padding: 0.5rem 1rem 0.125rem;
  position: relative;
  text-decoration: none;
  transition: 
    color var(--td) var(--te),
    opacity var(--td) var(--te),
    transform var(--td) var(--te);
  z-index: 1;
  
  &::before,
  &::after {
    content: '';
    background-color: var(--color-light);
    height: 50%;
    left: 0;
    position: absolute;
    transform: translateX(-110%);
    transform-origin: 0 50%;
    transition: transform 0s var(--te);
    width: 100%;
    z-index: -1;
  }
  
  &::before {
    top: 0;
  }
  
  &::after {
    bottom: 0;
  }
}

// ================================
// Menu item hover styles
// ================================

.menu:not(:focus-within):not(:hover) .menu__item {
  .menu__link { 
    opacity: 1;
    transform: translate(0, 0);
  }
}

.menu__item {
  --pull: 30%;
  
  .menu__link {
    opacity: 0.25;
    transition-duration: calc(var(--td) * 3);
    transform: translate(0, calc(var(--pull) * -1));
  }
  
  .submenu .menu__link {
    opacity: 1;
  }
  
  &:hover > .menu__link {
    color: var(--color-secondary);
    opacity: 1;
    transform: translate(0, 0);
    
    &:hover {
      color: var(--color-light);
      transition-delay: 0s;
    }
  }
  
  &:hover ~ .menu__item > .menu__link {
    transition-duration: calc(var(--td) * 3);
    transform: translate(0, var(--pull));
  }
}

// ================================
// Animations
// ================================

@keyframes blink {
  50%, 100% { opacity: 0 }
}
              
            
!

JS

              
                         // const nav = document.getElementById('main-navigation')

// nav.addEventListener('mousemove', function(e) {
//   const r = this.getBoundingClientRect()

//   this.style.setProperty('--x', e.clientX - (r.left + Math.floor(r.width / 2)))
//   this.style.setProperty('--y', e.clientY - (r.top + Math.floor(r.height / 2))) 
// });

// nav.addEventListener('mouseout', function() {
//   this.style.setProperty('--x', 0)
//   this.style.setProperty('--y', 0)
// });
              
            
!
999px

Console