2015-06-03 48 views
8

我正在试图创建一个导航栏,并在其上悬停了该项目下的箭头。这里就是我试图让:使用CSS在导航栏中显示边框三角形

enter image description here

因为我已经使用了伪元素beforeafter箭头。下面是一些代码:

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

的箭头是黑色的,因为边框颜色的。如何只显示箭头的边界?

+0

使用[这里]的方法(http://stackoverflow.com/questions/27462276/border-on-a-div-与中心箭头使用CSS)可以帮助你:)这不完全是你想要的,但可以采取的方法。 – Harry

回答

9

只需添加before伪元素像你加什么: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; 
} 

,我已经更新了你的笔请

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

+2

加1,这是完美:) – zey

4

您可以使用一个旋转的伪元素与边框:

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>

1

它可以很容易地使用课前和课后。将您的伪元素更改为before,然后使用after伪元素在黑色trainagle中绘制白色三角形。 为您的代码,CSS的可以是这样的

#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; 

}

+0

您可以在继续添加另一个之前检查接受的答案。除了选择器顺序和几个边界值之外,这是相同的,并且不会真的添加任何额外的东西。 – Harry