Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

html - How do you get a triangle hover effect on a pure css navbar?

I would like to have a little triangle underneath the the text that points up when the user hovers over the different tabs. Here is a bit of code I'm working with.

css navbar

* {
  margin: 0;
  padding: 0;
  list-style: none;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 11px;
  margin: 10px;
}

.tab {
  width: 100%;
  padding: 5px;
  background: #fff;
  color: #000;
  cursor: pointer;
  font-weight: bold;
  border-bottom: 1px solid black;
  position: relative;
}

.tab:hover {
  background: #a0a0a0;
}

.tab:hover span {
  display: block;
}

.tab_child {
  padding: 15px;
  background: #fff;
}

.selected {
  background: #a0a0a0;
}

.contain * {
  float: left;
  width: 100px;
}

span.triangle {
  background-image: url("http://www.inner.org/torah_and_science/mathematics/images/triangle.gif");
  background-repeat: none;
  display: none;
  height: 14px;
  width: 16px;
  position: absolute;
  top: 25px;
  left: 25%;
}
<div class="contain">
  <div id="one" class="tab selected">Link1</div>
  <div id="two" class="tab">Link2</div>
  <div id="three" class="tab">Link3</div>
  <div id="four" class="tab">Link4</div>
  <div id="five" class="tab">Link5</div>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this is probably what you're looking for:

Fiddle

Also, please use semantic markup:

  1. If your using HTML5 wrap your navigation in <nav> tags.
  2. Your links (if they really are going to be links) should be <a> elements.
  3. For a list of links like you have it is advised to use a list (<ul> & <li>).

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 11px;
}

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

nav li {
  float: left;
  width: 20%;
}

nav a {
  display: block;
  padding: 5px;
  border-bottom: 1px solid black;
  background: #fff;
  color: #000;
  font-weight: bold;
  position: relative;
}

nav a:hover,
.active {
  background: #bbb;
}

nav a:hover:after {
  content: "";
  display: block;
  border: 12px solid #bbb;
  border-bottom-color: #000;
  position: absolute;
  bottom: 0px;
  left: 50%;
  margin-left: -12px;
}
<nav>
  <ul>
    <li><a href="#" class="active">Link1</a></li>
    <li><a href="#">Link2</a></li>
    <li><a href="#">Link3</a></li>
    <li><a href="#">Link4</a></li>
    <li><a href="#">Link5</a></li>
  </ul>
</nav>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...