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 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

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

              
                <h1>Nav bar Dropdowns (on .click or .hover)</h1>
<hr>
<nav role='navigation' class="clearfix">
    <ul class="dropdown-container fll">
        <li><a href="#">Link 1</a>
            <div class="dropdown">Dropdown 1
                <p><a href="#" class="close">Close [x]</a></p>
            </div>
        </li>
        <li><a href="#">Link 2</a>
            <div class="dropdown">Dropdown 2
                <p><a href="#" class="close">Close [x]</a></p>
            </div>
        </li>
        <li><a href="#">Link 3</a>
            <div class="dropdown">Dropdown 3
                <p><a href="#" class="close">Close [x]</a></p>
            </div>
        </li>
    </ul>
    <ul class="dropdown-container flr">
        <li><a href="#" class="no-arrow">Login</a></li>
        <li><a href="#">Cart</a>
            <div class="dropdown cart">This DIV would contain the shopping cart information
                <p><a href="#" class="close">Close [x]</a></p>
            </div>
        </li>
    </ul>
</nav>
<hr>
<p>These dropdowns are very user friendly because they:
    <ul>
        <li>Collapse when:
            <ul>
                <li>Another dropdown slides down</li>
                <li>Clicking anywhere on the page</li>
                <li>Clicking on the <code>Close[x]</code> button</li>
                <li>Pressing the <code>ESC</code> key</li>
            </ul>
        </li>
        <li>Only one dropdown is visible at a time</li>
        <li>You can combine separate groups of dropdowns in the same nav bar linke in this demo (ie: Nav bar left side + Login & Cart right side)</li>
        <li>You can use your keyboard to navigate through the nav items and activate/collapse the dropdowns</li>
    </ul>
</p>
<u><em><strong>Additional Notes:</strong></u>
<ul>
    <li>I DO NOT recommend you use these dropdowns on <code>.hover</code>, this practice has <a href="http://uxmovement.com/navigation/why-hover-menus-do-users-more-harm-than-good/" target="_blank">serious usability issues</a>. Leave them on <code>.click</code>        and your users will thank you forever.</li>
    <li>Megamenus are good for usability, check this article by Jacob Nielsen (the Father of Usability): <a href="http://www.nngroup.com/articles/mega-menus-work-well/" target="_blank">Mega Menus Work Well for Site Navigation</a></li>
    <li>Twitter Bootstrap and Zurb's Foundation frameworks have this kind of functionality on <code>.click</code> as well. However, Twitter Bootstrap has ONLY <a href="https://getbootstrap.com/javascript/#dropdowns" target="_blank"><code>.hover</code></a>,
        while <a href="https://foundation.zurb.com/docs/components/top-bar.html" target="_blank">Zurb's Foundation has both <code>.click</code> and <code>:hover</code></a> but <code>:hover</code> is made with CSS.</em>
    </li>
</ul>
<hr>
<p class="pepe">Created by <a href="http://ricardozea.me/=?codepen" target="_blank">Ricardo Zea</a></p>
              
            
!

CSS

              
                //Styling stuff, not needed for the demo
//Google Fonts
@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic);

/*Reading font*/

//Sass/Compass stuff
@import "compass/css3/images";

*, *:before, *:after { box-sizing: border-box; }
html { height: 100%; }
body {
    @include background(radial-gradient(lighten(#272822, 5%), #272822 80%));
    color: white;
    font: 16px 'Source Sans Pro', Arial, Helvetica, sans-serif;
    padding: 20px 50px;
    color: white;
}

code {
    padding: 5px;
    font-style: italic;
    background: rgba(black, .15);
    font: 14px Courier;
    box-shadow: inset 0 0 1px rgba(black, .5);
}

hr {
    margin: 30px auto;
    border: none;
    border-top: #666 1px dotted;
}

h1 {
    text-align: center;
    margin: 0;
}

li {
    margin-bottom: 10px;
}

a,
a:visited {
    color: #2963BD;
}

a:hover {
    color: #c90;
    text-decoration: none;
}

//OOCSS
.fll {
    float: left;
}

.flr {
    float: right;
}

//Clearfix - http://davidwalsh.name/css-clear-fix
.clearfix:before,
.clearfix:after {
    display: block;
    content: "";
    height: 0;
    overflow: hidden;
}

.clearfix:after {
    clear: both;
}

//======================================
//Demo stuff - Edit below this 
.dropdown-container {
    list-style-type: none;
    margin: 0;
    padding: 0;
    position: relative;
    & > li {
        float: left;
        width: 150px;
        margin: 0 10px 0 0;
        position: relative;
        &:last-of-type {
            margin: 0;
        }
        //Top level links    
        & > a,
        & > a:visited {
            display: block;
            padding: 10px 20px;
            border-radius: 2px;
            text-decoration: none;
            background: #c90;
            color: black;
            font-weight: bold;
            text-shadow: 0 -1px 0 rgba(white, .4);
            position: relative;
            box-shadow: inset 0 0 0 2px rgba(black, .2);
        }
        & > a:hover,
        & > a:focus,
        & > a:active {
            background: lighten(#c90, 30%);
        }
        //Arrows
        @mixin arrow-bottom {
            float: right;
            width: 0;
            height: 0;
            content: '';
            border-bottom: 10px solid #272822;
            border-right: 10px solid transparent;
        }
        & > a:after {
            @include arrow-bottom;
            margin-top: 3px;
            transform: rotate(-45deg);
        }
        & > a.no-arrow:after {
            display: none;
        }
        //Active Top level link
        &.active > a {
            background: #2963BD;
            color: white;
            border-radius: 2px 2px 0 0;
            text-shadow: 0 -1px 0 rgba(black, .4);
            //Arrow on active link
            &:after {
                @include arrow-bottom;
                margin-top: 10px;
                transform: rotate(135deg);
            }
        }
    }
}

.dropdown {
    display: none;
    width: 300px;
    height: 200px;
    padding: 25px 20px 20px;
    position: absolute;
    left: 0;
    background: lighten(#2963BD, 10%);
    font-size: 14px;
    color: white;
    text-shadow: 0 -1px 0 rgba(black, .4);
    border-radius: 0 2px 2px 2px;
    box-shadow: 0 2px 5px rgba(black, .5), inset 0 0 0 2px rgba(black, .2);
    &.cart {
        width: 320px;
        left: inherit;
        right: 0;
        border-radius: 2px 0 2px 2px;
    }
    .close {
        padding: 6px;
        color: rgba(white, .6);
        position: absolute;
        top: 0;
        right: 0;
        font-size: 12px;
        background: rgba(black, .3);
        text-decoration: none;
        border-radius: 0 0 0 2px;
        &:hover {
            background: rgba(black, .5);
            color: white;
        }
    }
}

//Not needed for demo
.new-window {
    position: relative;
    padding-right: 16px;
    &:after {
        content: '';
        display: block;
        width: 14px;
        height: 12px;
        position: absolute;
        top: 10%;
        right: 0;
        background: url(https://i.stack.imgur.com/3H2PQ.png) no-repeat;
    }
}
              
            
!

JS

              
                //Nav bar Dropdowns
var dropdown = ".dropdown";

//If you want the Dropdowns on hover, just change the ".click" method to ".hover". 
//Note: I DO NOT recommend you use these dropdowns on .hover, this practice has serious usability issues. Leave them on .click and your users will Thank You forever.
$(".dropdown-container > li").click(function() { 
    //Hide any other Dropdwn that's visible*
    $(this).siblings().find(dropdown).slideUp("fast");
    //Hide Cart Dropdwn
    $(this).parents(".dropdown-container").siblings().find(dropdown).slideUp("fast"); 
    //Toggle class .active
    $(this).toggleClass("active");
    //Remove class .active from any other <li>
    $(this).parents().find(".active").not(this).removeClass("active");
    //Slide up/down the actual Dropdwn*		
    $(this).find(dropdown).stop(true, true).slideToggle("fast");
    //Deactivate the "focus" on the nav item when clicked
    $(this).children("a").blur();
});

//Close button/link
$(".close").click(function() {
    $(this).parents(".dropdown").slideUp("fast", function() {
        //Remove the class .active from parent element
        $(this).parents().find(".active").removeClass("active");
    });
});

//Collapse Dropdowns when clicking anywhere else on the page	
var mouse_is_inside = false;
//Select the parent element of the Dropdwn
$(".dropdown-container li").hover(
    function() {
        mouse_is_inside = true;
    },
    function() {
        mouse_is_inside = false;
    });

$("html").mouseup(function(e) {
    e.stopPropagation();
    if (!mouse_is_inside)
        $(".dropdown").slideUp("fast", function() {
            $(".active").removeClass("active");
        });
});

//Avoid collapsing the Dropdowns when clicking inside of them
$(".dropdown").click(function(e) {
    e.stopPropagation();
});

//Close any open Dropdowns when pressing the ESC key
var elem = $(".dropdown");
$(document).on("keydown", function(e) {
    if (e.keyCode === 27) {
        $(elem).slideUp("fast");
        e.stopPropagation();
    }
});

/*NOT NEEDED FOR THE DEMO*/

/*Stop the default action of dummy links using "#" so CodePen doesn"t freak out*/

//jQuery version:
$(".dropdown-container > li a").click(function(e) {
    e.preventDefault();
});

//Plain JavaScript verison:
/*var anchors = document.getElementsByClassName("a");
 for(i=0, len=anchors.length; i<len; i++){
     anchors[i].addEventListener("click", function(e){e.preventDefault();});
 }*/

//Add a title to the links that open in a new tab/window
$("a[target='_blank']").attr({
    title: "Opens in a new tab/window"
}).addClass("new-window");
              
            
!
999px

Console