4

I'm trying to create a hover effect in my drop down menu but I can't figure out how to do it.

What I want to do is this:

enter image description here

instead of this:

enter image description here

My Code - You can also see it here, (updated version)

HTML:

<nav>
    <ul>
        <li><a href="#">One</a>
            <ul>
                <li><a href="1.html">1.1</a></li>
                <li><a href="2.html">1.2</a>
            </ul>
        </li>
        <li><a href="#">Two</a>
            <ul>
                <li><a href="3.html">2.1</a></li>
                <li><a href="4.html">2.2</a></li>
                <li><a href="5.html">2.3</a></li>
            </ul>
        </li>
        <li><a href="#">Three</a>
            <ul>
                <li><a href="6.html">3.1</a></li>
                <li><a href="7.html">3.2</a></li>
            </ul>
        </li>
        <li><a href="8.html">Four</a></li>
    </ul>
</nav>

CSS:

nav {
    float: left;
}
nav ul ul {
    display: none;
}
nav ul li:hover > ul {
    display: block;
}
nav ul {
    list-style: none;
    position: relative;
    display: inline-table;
}
nav ul:after {
    content:"";
    clear: both;
    display: block;
}
nav ul li {
    float: left;
}
nav ul li:hover {
}
nav ul li:hover {
    background-color: #000;
}
nav ul li a:hover {
    color: #fff;
}
nav ul li a {
    display: block;
    padding: 25px 15px;
    color: #6F0;
    text-decoration: none;
}
nav ul ul {
    border-radius: 0px;
    padding: 0;
    position: absolute;
}
nav ul ul li {
    float: none;
    position: relative;
}
nav ul ul li a {
    padding: 15px 40px;
    color: #000;
}

Any help will be appreciated.

0

1 Answer 1

3

Working fiddle:

Change the styles for the below two:

nav ul li:hover {
border-bottom: 1px solid #000;
color: #000;
}

nav ul li a:hover {
color: #000;
}

And add:

nav ul li:hover li:hover{
        background: #000;
}

In order to style the sub-menus.

The first (li:hover) you want to set a bottom border - you can change the width of this border from 1px to something more thick, say, 3px

Sign up to request clarification or add additional context in comments.

5 Comments

I tried it and it works. The only problem is that I don't want the hover bar to appear in the submenu. Also if you take a look in the updated jsfiddle code the submenu doesn't work now. If i use margin and move it in order to be closer to the menu it works but how can I get it to work if it isn't close to the menu?
@KalPetros Let me take a look
@KalPetros What is wrong with the fiddle I supplied? My answer, I believe, answers the original question.
Yes your fiddle answers the original question but now after some changes that I made (I moved the submenu 2-3 pixels away from the menu) the submenu doesn't work. Is there a way to fix it?
@KalPetros don't quote me on this one - I haven't tested it, but I think instead of moving the submenu away from the main menu, you should add padding to the top of the sub-menu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.