8

I am trying to create a nav bar with an arrow under the item hovered upon. Here's is what I am trying to make:

enter image description here

For the arrow I have used the pseudo elements before and after. Here is some of the code:

body {
  background: #FFFFFF;
  color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  height: 100%;
}
.clear {
  clear: both;
}
.page-wrap {
  width: 980px;
  margin: 0px auto;
  height: 100%;
}
#main-menu {
  background: white;
  height: 55px;
  width: 100%;
  padding: 0px;
  margin: 0px;
  border-bottom: 1px solid black;
}
ul {
  font-family: Arial, Verdana;
  font-size: 18px;
  margin: 0;
  padding: 0;
  list-style: none;
}
ul li {
  display: block;
  position: relative;
  float: left;
}
li ul {
  display: none;
}
ul li a {
  display: block;
  text-decoration: none;
  color: black;
  padding: 0 9px 0 9px;
  background: white;
  margin-left: 1px;
  white-space: nowrap;
  line-height: 55px;
  font: 18px;
  font-family: Arial, Helvetica, sans-serif;
  outline: none;
}
ul li a:hover {
  color: black;
}
#menu a:hover:after {
  content: "";
  position: absolute;
  top: 40px;
  left: 50%;
  margin-left: -15px;
  width: 0px;
  height 0px;
  xxmargin: 0px auto;
  border-left: 15px solid transparent;
  border-right: 15px solid transparent;
  border-bottom: 15px solid black;
}
<header id="main-menu">
  <div class="page-wrap">
    <ul id="menu">
      <li><a href="#">Recommended</a></li>
      <li><a href="#">Recent</a></li>
    </ul>
  </div>
</header>

Fiddle Link

The arrows are black in color, because of the border color. How to show just the borders of the arrow?

1
  • The approach used here could help you :) It is not exactly what you want but the approach can be adopted.
    – Harry
    Jun 3, 2015 at 7:02

3 Answers 3

10

Just add before pseudo element like what you add :after

#menu a:hover:after {
  content: "";
  position: absolute;
  top: 41px;
  left: 50%;
  margin-left: -15px;
  width: 0px;
  height 0px;
  margin: 0px auto;
  border-left: 15px solid transparent;
  border-right: 15px solid transparent;
  border-bottom: 15px solid white;
}
#menu a:hover:before {
  content: "";
  position: absolute;
  top: 40px;
  left: 50%;
  margin-left: -15px;
  width: 0px;
  height 0px;
  margin: 0px auto;
  border-left: 15px solid transparent;
  border-right: 15px solid transparent;
  border-bottom: 15px solid black;
}

and i have updated you pen please check

http://codepen.io/anon/pen/vOxmGZ

1
  • 2
    plus one , that's perfect :)
    – zey
    Jun 3, 2015 at 7:10
4

You can use a rotated pseudo element with borders :

body {
  background: #FFFFFF;
  color: #FFFFFF;
  margin: 0px;
  padding: 0px;
  height: 100%;
}
.page-wrap {
  width: 980px;
  margin: 0px auto;
  height: 100%;
}
#main-menu {
  background: white;
  height: 55px;
  width: 100%;
  padding: 0px;
  margin: 0px;
  border-bottom: 1px solid black;
}
ul {
  font-family: Arial, Verdana;
  font-size: 18px;
  margin: 0;
  padding: 0;
  list-style: none;
}
ul li {
  display: block;
  position: relative;
  float: left;
}
li ul {
  display: none;
}
ul li a {
  display: block;
  text-decoration: none;
  color: black;
  padding: 0 9px 0 9px;
  background: white;
  margin-left: 1px;
  white-space: nowrap;
  line-height: 55px;
  font: 18px;
  font-family: Arial, Helvetica, sans-serif;
  outline: none;
}
ul li a:hover {
  color: black;
}
#menu a:hover:after {
  content: "";
  position: absolute;
  bottom:-10px;
  left: 50%;
  margin-left:-10px;
  width: 20px;
  height: 20px;
  background:#fff;
  -webkit-transform:rotate(45deg);
  -ms-transform:rotate(45deg);
  transform:rotate(45deg);
  border-left:1px solid #000;
  border-top:1px solid #000;
  box-sizing:border-box;
}
<header id="main-menu">
  <div class="page-wrap">
    <ul id="menu">
      <li><a href="#">Recommended</a>
      </li>

      <li><a href="#">Recent</a>
      </li>
    </ul>
  </div>
</header>

0
1

It can be easily done using the before and after class. Change your pseudo element to before and than use an after pseudo element to draw a white triangle inside the black trainagle. For your code, the css can be something like this

#menu a:hover:before {
    content: "";
    position: absolute;
    top: 40px;
    left: 50%;
    margin-left: -15px;
    width: 0px;
    height 0px;
    xxmargin: 0px auto;
    border-left: 15px solid transparent;
    border-right: 15px solid transparent;
    border-bottom: 15px solid black;

    }
#menu a:hover:after {
    content: "";
    position: absolute;
    top: 40px;
    left: 50%;
    margin-left: -15px;
    width: 0px;
    height 0px;
    xxmargin: 0px auto;
    border-left: 12px solid transparent;
    border-right: 12px solid transparent;
    border-bottom: 12px solid white;

}

1
  • You could have checked the accepted answer before proceeding to add another one. Other than the selector order and a couple of border values this is the same and doesn't really add anything extra.
    – Harry
    Jun 3, 2015 at 8:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.