我想制作一个菜单导航栏,其中包含多个inline-block的li元素,每个元素必须右侧带有箭头。就像这样:
我在Google上搜索了一下,最常见的答案是使用带透明边框的CSS技巧。就像这个例子:
http://jsfiddle.net/jx99z/5/
HTML:
![my dream menu aww](https://istack.dev59.com/Sa2DV.webp)
<div class="text">Some Text<span class="arrow"></span></div>
CSS:
.text {
background-color:#ff0000;
color:#fff;
display:inline-block;
padding-left:4px;
}
.arrow {
border-style: dashed;
border-color: transparent;
border-width: 0.20em;
display: -moz-inline-box;
display: inline-block;
/* Use font-size to control the size of the arrow. */
font-size: 100px;
height: 0;
line-height: 0;
position: relative;
vertical-align: middle;
width: 0;
background-color:#fff; /* change background color acc to bg color */
border-left-width: 0.2em;
border-left-style: solid;
border-left-color: #ff0000;
left:0.25em;
}
这看起来还不错,但是当我尝试使用:hover添加其他元素时,它们的外观和行为就不正常了:http://jsfiddle.net/txayr2j6/
HTML:
<div class="text">Some Text<span class="arrow"></span></div>
<div class="text">Some Text<span class="arrow"></span></div>
CSS:
.text {
background-color:#ff0000;
color:#fff;
display:inline-block;
padding-left:4px;
}
.arrow {
border-style: dashed;
border-color: transparent;
border-width: 0.20em;
display: -moz-inline-box;
display: inline-block;
/* Use font-size to control the size of the arrow. */
font-size: 100px;
height: 0;
line-height: 0;
position: relative;
vertical-align: middle;
width: 0;
background-color:#fff; /* change background color acc to bg color */
border-left-width: 0.2em;
border-left-style: solid;
border-left-color: #ff0000;
left:0.25em;
}
.text:hover {
background-color:#ccc;
border-left-color: #ccc;
}
我发现另一个解决方法是,我可以使用SVG(可缩放矢量图形)绘制任何元素,类似于这样:http://codepen.io/anon/pen/OXWoXd
HTML:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<polygon points="
0,0
0,200
270,200
300,100
270,0
150,0
" />
<div>Item 1</div>
</svg>
CSS:
svg polygon {
fill: transparent;
stroke: black;
stroke-width: 2px;
}
svg {
background-color: #ccc;
height: 50%;
}
body, html {
height: 100%;
margin: .2em;
}
但那个解决方案更加糟糕:我无论如何都不能让元素变得大于300像素,还有那些丑陋的边框和背景。此外,我希望那个条可以响应式。谢谢!