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

              
                 <div class="h-wrap">
 <nav>
 <div id="burger-menu">
 <div class="stripe"></div>
 <div class="stripe"></div>
 <div class="stripe"></div>
 </div>
 <ul id="site-navigation">
 <li class="menu-item current-menu-item"><a href="#">Seite 1</a></li>
 <li class="menu-item"><a href="#">Seite 2</a></li>
 <li class="menu-item"><a href="#">Seite 3</a></li>
 <li class="menu-item"><a href="#">Seite 4</a></li>
 <li class="menu-item"><a href="#">Seite 5</a></li>
 <li class="menu-item"><a href="#">Seite 6 - langer Titel</a></li> 
 </ul> 
 </nav> 
 </div>
 <article>
 <h1>Animierter Unterstich für ein Menü</h1>
 <h2>Javascript</h2>
 <p>
Mit der Maus über das Menü fahren, um den Effekt zu sehen!
 </p>
<p>Folgendes gilt zu beachten:</p>
 <div class="code">nav a {
     color:#ccc;
     text-decoration: none;
     font-size: 1.2em;
     padding:0; /*<b>Kein Padding</b>*/
     }
     nav ul>li {
     display:inline-block;
     list-style: none;
     padding: 25px 20px; /*<b>Hier Padding</b>*/
     }</div>   
 </article>
 <footer>kontakt@mg-otterson.de</footer>
<script>
document.querySelector("#burger-menu").addEventListener("click", function() {
 document.querySelector("nav > ul").classList.toggle("act-ul");
})

for(let el of document.querySelectorAll("#site-navigation li")) {
 el.addEventListener("click", function() { 
 document.querySelector("#site-navigation").classList.remove("act-ul")
 });
}
</script>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Lato:wght@300;700&family=Roboto:wght@300;400;500;700&display=swap');
* {
  box-sizing:border-box;  
}
/* Navigation Anfang */
nav {
    text-align: center;
    overflow: visible;
    height: auto;
    background: #444;
    position: fixed;
    top:0;
    left:0;
    overflow: visible;
    width: 100%;
}
nav>ul {
    margin:0;
    padding:0;
    text-align: center;
    overflow:visible;
    background:#333;
}

nav ul>li {
    display:inline-block;
    list-style: none;
    padding: 25px 20px;
}
nav ul>li a  {
    display: block;
}
nav a{
    color:#ccc;
    text-decoration: none;
    font-size:1.2em;
}
@media only screen and (max-width: 630px) {
    nav {
        height:72px;   
    }
    #site-navigation {
        margin-top: 72px;
    }
    nav ul>li { 
        width:100%;
    }
    nav>ul {
        display:none;
        margin-top: 0px;
    }
    nav>ul>li {
        position: relative;
        width:100%;
        padding: 10px 20px;
    }
    #burger-menu { 
        display:block !important;
    }
    nav ul>li a  {
        padding: 5px 32px;
        display: block;
    }
}
@media only screen and (min-width: 630px) {
    nav>ul {
        display:block !important;
        margin-top:0px;
    }
}
#burger-menu {
    position: absolute;
    right:20px;
    top:20px;
    width:30px;
    height:30px;
    display:none;
}
.act,.act-ul {
    display:block !important;
}
/* Navigation Ende */
h1,h2,h3,h4 {
    font-weight: 400;
}
body {
    margin:0;
    padding:0;
    font-family:Arial, sans-serif;
    font-family: 'Roboto';
    font-weight: 300;
}
header {
    width:100%;
    background:#444;
    overflow: visible;
    position: fixed;
    position: relative;
    top:0;
    left:0;
}
.stripe {
    position: relative;
    width:100%;
    height:4px;
    background:#ccc;
    margin-bottom:5px;
}
article {
    width:100%;
    max-width:1300px;
    margin:0 auto;
    margin-top:72px;
    font-size:18px;
    line-height:1.5;
    padding:30px;
    background:#f2f2f2;
    border-left:1px solid #ccc;
    border-right:1px solid #ccc;
    border-bottom:1px solid #ccc;
}
h1 {
    font-size:2em;
    margin:0 0 30px 0;
}
h2 {
    font-size:1.5em;
    margin:0 0 30px 0;
}
h3 {
    font-size:1.2em;
    font-weight: bold;
    margin:0;
}
footer {
    text-align: center;
    padding:30px 20px;
    margin-top: 0px;
}

a {
    cursor:pointer;
}

footer {
    display: flex;
    flex-direction: column;
    align-content: center;
    justify-content: center;
    align-items: center;
    gap: 12px 0;
}
.code {
    font-size: 1em;
    font-weight: 300;
    background: #e2e2e2;
    padding:20px;
    text-align: left;
    white-space: pre-line;
    word-wrap: break-word;
    color: #000;
    margin-bottom: 30px;
}
/* Underscore */

.underscore {
 height:4px;
 position: absolute;
 background:#f00;
}
.underscore-ani {
    transition: width 0.3s, left 0.3s; 
}

@keyframes fadeOut {
    from {opacity: 1;}
    to {opacity: 0;}
}
.fade-out{
    animation-name: fadeOut;
    animation-duration: 0.9s;
    animation-fill-mode: forwards;
}
              
            
!

JS

              
                const navA = document.querySelectorAll("#site-navigation li a");
const navLi = document.querySelectorAll("#site-navigation li");
const nav = document.querySelector("ul#site-navigation");
const currentItem = "current-menu-item";
const marginTop = 50;
const mobile = 630;
var currentIndex = -1;
var stopAniTimeOut;
var underscore;
for (i = 0; i < navLi.length; i++) {
    if (navLi[i].classList.contains(currentItem)) {
        currentIndex = i;
    }
}
const style = window.getComputedStyle(navLi[0]);
defPaddingLeft = parseInt(style.getPropertyValue('padding-left'));
function start() {
    const underscoreAppend = document.createElement("div");
    underscoreAppend.className = "underscore";
    nav.appendChild(underscoreAppend);
    underscore = document.querySelector(".underscore");
    if (currentIndex != -1) {
        let startPos = navLi[currentIndex].offsetLeft + defPaddingLeft;
        let underWidth = navA[currentIndex].offsetWidth;
        let toTop = navLi[currentIndex].offsetTop;

        underscore.style.width = underWidth + "px";
        underscore.style.left = startPos + "px";
        underscore.style.top = toTop + marginTop + "px";
    }
    if (window.innerWidth < mobile || currentIndex == -1) {
        underscore.style.display = "none";
    }
}
document.fonts.ready.then(function () {
    start();
});

function findIndex(el) {
    for (i = 0; i < navLi.length; i++) {
        if (navLi[i] == el) {
            return i;
        }
    }
}
function responsive() {
    if (currentIndex != -1) {
        let leftPos = navLi[currentIndex].offsetLeft + defPaddingLeft;
        let newWidth = navLi[currentIndex].querySelector("a").offsetWidth;
        let toTop = navLi[currentIndex].offsetTop;

        underscore.style.width = newWidth + "px";
        underscore.style.left = leftPos + "px";
        underscore.style.top = toTop + marginTop + "px";
        if (window.innerWidth < mobile) {
            underscore.style.display = "none";
        } else {
            underscore.style.display = "block";
        }
    }
}
for (let el of navA) {
    el.addEventListener("mouseover", function (event) {
        underscore.classList.remove("fade-out");
        underscore.classList.add("underscore-ani");
        clearTimeout(stopAniTimeOut);
        stopAniTimeOut = setTimeout(function () {
            underscore.classList.remove("underscore-ani");
        }, 300);
        if (window.innerWidth < mobile) {
            underscore.style.display = "none";
        } else {
            underscore.style.display = "block";
        }
        let leftPos = event.target.parentElement.offsetLeft + defPaddingLeft;
        let underWidth = event.target.offsetWidth;
        let toTop = event.target.parentElement.offsetTop;
        underscore.style.width = underWidth + "px";
        underscore.style.left = leftPos + "px";
        underscore.style.top = toTop + marginTop + "px";
        currentIndex = findIndex(event.target.parentElement);
    });
}
nav.addEventListener("mouseleave", function () {
    if (window.innerWidth > mobile) {
        underscore.classList.add("fade-out");
    }
});
var timeOutFunctionId;
window.addEventListener("resize", (event) => {
    clearTimeout(timeOutFunctionId);
    timeOutFunctionId = setTimeout(responsive, 10);
}
);
              
            
!
999px

Console