Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropdown menu with top triangle

Tags:

html

css

hover

menu

I want to create a dropdown menu like this:

http://www.howdyjeff.com/

but I cant horizontally center the top triangle and the dropdown menu (based on the li a) and I'm having some :hover problem, because the dropdown ul fades away.

how can I do that?

JSFiddle:

https://jsfiddle.net/zw5rqzut/

html {
    text-align: center;
}
nav {
    width: 100%;
    height: auto;
    background-color: #000;
}
ul li {
    display: inline-block;
    position: relative;
    margin: 1em
}
ul ul {
    display: none;
    position: absolute;
    background-color: #000;
    top: 50px;
}
ul ul:after {
    position: absolute;
    left: 50%;
    top: -8px;
    width: 0;
    height: 0;
    content: '';
    border-left: 20px solid transparent;
    border-right: 20px solid transparent;
    border-bottom: 20px solid #000000;
}
a {
    color: #fff;
    text-decoration: none;
}
li:hover ul {
	display:block;
}
<nav>
    <ul>
        <li>
            <a href="#">Dropdown1</a>
            <ul>
                <li><a href="#">Option1</a></li>
                <li><a href="#">Option2</a></li>
                <li><a href="#">Option3</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Dropdown2</a>
            <ul>
                <li><a href="#">Option1</a></li>
                <li><a href="#">Option2</a></li>
                <li><a href="#">Option3</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Dropdown3</a>
            <ul>
                <li><a href="#">Option1</a></li>
                <li><a href="#">Option2</a></li>
                <li><a href="#">Option3</a></li>
            </ul>
        </li>
    </ul>
</nav>
like image 758
filippo90 Avatar asked Mar 07 '15 15:03

filippo90


1 Answers

For centering the arrow, you can add this:

ul ul:after {
    margin-left: -20px;
}

To fix the dropdown doesn't stay, give enough bottom padding like this:

nav > ul > li > a {
    padding-bottom: 50px;
}

Demo: centering dropdown and arrow:

http://jsfiddle.net/esjrhg5n/

$(document).ready(function() {
    $("nav > ul > li").mouseover(function() {
        var the_width = $(this).find("a").width();
        var child_width = $(this).find("ul").width();
        var width = parseInt((child_width - the_width)/2);
        $(this).find("ul").css('left', -width);
    });
});

Update: Non-JS version below:

https://jsfiddle.net/2Ly1h6uz/

nav {
  background-color: black;
  font-family: sans-serif;
  text-align: center;
}

nav ul,
nav li {
  list-style: none;
  padding: 0;
  margin: 0;
}

nav a {
  text-decoration: none;
  color: white;
}

nav a:hover {
  color: yellow;
}

nav>ul {
  display: flex;
  justify-content: center;
}

nav>ul>li {
  position: relative;
}

nav>ul>li>a {
  display: block;
  padding: 10px 20px;
}

nav>ul>li>ul {
  display: none;
  white-space: nowrap;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  padding-top: 15px;
}

nav>ul>li>ul>li>a {
  display: block;
  padding: 10px 20px;
  background-color: black;
  border-bottom: 1px solid darkgray;
}

nav>ul>li>ul:after {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  top: 5px;
  width: 0;
  height: 0;
  content: '';
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-bottom: 10px solid black;
}

nav>ul>li:hover>ul {
  display: block;
}
<nav>
  <ul>
    <li>
      <a href="#">Dropdown1</a>
      <ul>
        <li><a href="#">Option1</a></li>
        <li><a href="#">Option2 A Long One</a></li>
        <li><a href="#">Option3</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Dropdown2</a>
      <ul>
        <li><a href="#">Option1</a></li>
        <li><a href="#">Option2</a></li>
        <li><a href="#">Option3</a></li>
      </ul>
    </li>
    <li>
      <a href="#">Dropdown3</a>
      <ul>
        <li><a href="#">Option1</a></li>
        <li><a href="#">Option2</a></li>
        <li><a href="#">Option3</a></li>
      </ul>
    </li>
  </ul>
</nav>
like image 75
Stickers Avatar answered Oct 25 '22 01:10

Stickers