如何在纯css导航栏上获得三角形悬停效果?

时间:2012-01-12 19:45:30

标签: html css css-shapes

我想在用户将鼠标悬停在不同标签上时指向文本下方的小三角形。这是我正在使用的一些代码。

css navbar

3 个答案:

答案 0 :(得分:11)

我认为这可能就是你要找的东西:

Fiddle

另外,请使用语义标记:

  1. 如果您使用HTML5将导航包裹在<nav>标记中。
  2. 您的链接(如果它们确实是链接)应为<a>元素。
  3. 对于您所拥有的链接列表,建议使用列表(<ul>&amp; <li>)。

答案 1 :(得分:2)

Here是对jsfiddle的修改:

我添加了一个<span class="arrow"></span>来包含HTML中的三角形:

<div class="tab_container">
    <div id="test1-header" class="accordion_headings header_highlight" >Home<span class="arrow"></span></div>
    <div id="test2-header" class="accordion_headings" >About<span class="arrow"></span></div>
    <div id="test3-header" class="accordion_headings" >Work<span class="arrow"></span></div>
    <div id="test4-header" class="accordion_headings" >Social<span class="arrow"></span></div>
    <div id="test5-header" class="accordion_headings" >Contact<span class="arrow"></span></div>
</div>

以下是对菜单所做的更改,这些更改会缩小三角形的大小,并将其悬停在每个菜单项的底部中心位置: CSS:

/*
.accordion_headings:hover{
    background:#00CCFF;
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 5px solid red;
}
*/
.accordion_headings{
    position:relative;
}
.accordion_headings .arrow{
    display:none;
}
.accordion_headings:hover .arrow{
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 5px solid red;

    display:block;
    position:absolute;
    bottom:0;
    left:49%;
}

答案 2 :(得分:0)

这是fiddle,它使用将在悬停的菜单项上显示的背景图像。它不漂亮,但进一步的CSS应该有所帮助。

更新

抱歉,我一定误读了。这是一个有效的fiddle,其中一个较小的箭头指向正确的方向。