如何在之前添加z-index?

时间:2017-08-25 08:13:04

标签: html css

每个div都应该有一个箭头。我想要这样的东西: four horizontally arranged blocks. Each, except the last, has a wedge on the right hand side

关于我该怎么做的任何建议?



div {
  position: relative;
  background-color: #008000;
  padding: 0px 16px;
  height: 40px;
  line-height: 40px;
  display: inline-block;
  color: white;
  border: 2px solid black;
  border-right: none;
  z-index: 1;
  margin-right: -5px;
}

div:before {
  content: '';
  position: absolute;
  left: 100%;
  z-index: -1;
  width: 28px;
  height: 28px;
  background-color: #008000;
  border-right: 2px solid black;
  border-bottom: 2px solid black;
  transform: rotate(-45deg) translate(-14px, -7px);
}

<div>arrow with border</div>
<div>arrow with border</div>
<div>arrow with border</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

为了使箭头显示在项目上方,您需要使用z-index > 0。 为了将箭头放在正确的位置,我已为每个padding & margin添加了li

为了不为最后一项呈现箭头,我使用了sibling selector(+),因此第一个li将无法获得箭头。

像这样的东西

ul {
  display: block;
  background: gray;
  border-top: 2px solid;
  border-bottom: 2px solid;
  padding: 0;
  margin: 0;
}

li {
  display: inline-block;
  position: relative;
  padding: 0 15px;
  margin-right: 15px;
}

li + li:before {
  content: '';
  position: absolute;
  left: -10px;
  top: -3px;
  z-index: 2;
  width: 12px;
  height: 12px;
  background-color: gray;
  border-right: 2px solid black;
  border-bottom: 2px solid black;
  transform: rotate(-45deg) translate(-14px, -7px);
}
<ul>
  <li>menu 1</li>
  <li>menu 2</li>
  <li>menu 3</li>
  <li>menu 4</li>
</ul>

答案 1 :(得分:0)

希望这有帮助。

&#13;
&#13;
ul {
    margin: 20px 60px;
}

ul li {
    display: inline-block;
    height: 30px;
    line-height: 30px;
    width: 100px;
    margin: 5px 1px 0 0;
    text-indent: 35px;
    position: relative;
}

ul li:before {
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    left: -2px;
    border-style: solid;
    border-width: 15px 0 15px 15px;
    border-color: transparent transparent transparent white;
    z-index: 0;
}

ul li:first-child:before {
    border-color: transparent;
}

ul li a:after {
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    right: -15px;
    border-style: solid;
    border-width: 15px 0 15px 15px;
    border-color: transparent transparent transparent darkgray;
    z-index: 10;
}

ul li.active a {
    background: green;
    z-index: 100;
}

ul li.active a:after {
    border-left-color: green;
}

ul li a {
    display: block;
    background: darkgray;
    text-decoration: none;
}

ul li a:hover {
    background: blue;
    color: white;
}

ul li a:hover:after {
    border-color: transparent transparent transparent blue;
}
&#13;
<ul>
    <li><a href="#">Foobar</a></li>
    <li class="active"><a href="#">Foobar</a></li>
    <li><a href="#">Foobar</a></li>
    <li><a href="#">Foobar</a></li>
</ul>
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

以下是步骤的简单示例尝试其中一个链接。

http://jsfiddle.net/qXCed/5/

nav {
    background: #eee;
    border: 1px solid #bbb;
    height: 30px;
    overflow: hidden;
}
ul {
    float: left;
}
ul li {
    float: right;
    text-indent: 25px;
}
ul li:last-child {
    margin-left: -15px;
}
ul li a {
    background: #ddd;
    background-image: linear-gradient(top, @start_color, @end_color);
    background-image: -webkit-gradient(linear, left top, right bottom, from(#fff), to(#ccc));
    background-image: -moz-linear-gradient(left top, #eee 38%, #ddd 61%);
    border: 1px solid #ccc;
    display: block;
    line-height: 12px;
    margin-top: -33px;
    padding: 40px 0;
    width: 92px;
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
}
ul li a:hover {
    background: #ddd;
    background-image: none;
}
ul li a span {
    display: block;
    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
}
<nav>
    <ul>
        <li>Current</li>
        <li><a href="#3"><span>Third</span></a></li>
        <li><a href="#2"><span>Second</span></a></li>
        <li><a href="#1"><span>First</span></a></li>
    </ul>
</nav>