Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

How do you create a basic dropdown menu with CSS?

Hey, I'm trying to create a basic 3 link drowndown menu, that displays on hover

I've set the dropdown menu content to display: none, and I'm trying to display the menu when you hover over the "Course Material" link. However, the dropdown menu fails to display.

Any help would be greatly appreciated

<header class="main-header">
        <ul class="main-nav">
            <li><a href="index.html">Home</a></li>
            <li><a href="genInfo.html">General Information</a></li>
            <li><a href="techHome.html" class="drop-link">Course Material</a></li>
            <li><a href="gamesHome.html">Games</a></li>
        </ul>
        <div class="dropdown-content">
                <a href="#">LINK1</a>
                <a href="#">LINK2</a>
                <a href="#">LINK3</a>
            </div>
    </header><!--/.main-header--> 
.drop-link {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    top: 50px;
    right: 50px;
    border-radius: .5em;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    padding: 12px 16px;
    border: 1px solid #5ecafa;
}


.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown-content a:hover {
    background-color: #f1f1f1;
}

.drop-link:hover .dropdown-content {
    display: block;
}

2 Answers

Kallil Belmonte
Kallil Belmonte
35,561 Points

Through CSS, you would need to restructure your HTML, this way you could select the dropdown-content inside the drop-item.

.drop-item:hover .dropdown-content {
    display: block;
}
<header class="main-header">
        <ul class="main-nav">
            <li><a href="index.html">Home</a></li>
            <li><a href="genInfo.html">General Information</a></li>
            <li  class="drop-item">
                <a href="techHome.html">Course Material</a>
                <div class="dropdown-content">
                    <a href="#">LINK1</a>
                    <a href="#">LINK2</a>
                    <a href="#">LINK3</a>
               </div>
            </li>
            <li><a href="gamesHome.html">Games</a></li>
        </ul>
    </header><!--/.main-header--> 

Hey, Kallil, thank you for both methods!

Kallil Belmonte
Kallil Belmonte
35,561 Points

You can do this via jQuery:

$('.drop-link').hover(function(){
    $('.dropdown-content').css("display", "block");
});

Or...

$('.drop-link').hover(function(){
    $('.dropdown-content').show();
});

jQuery - css() Method:

http://www.w3schools.com/jquery/jquery_css.asp

jQuery Effects - Hide and Show:

http://www.w3schools.com/jquery/jquery_hide_show.asp