css剪辑路径重叠和边框

时间:2017-12-30 04:30:03

标签: html css css-shapes clip-path

这是我所做的代码: https://codepen.io/dickeddocks/pen/opWBwQ

这就是我想要实现的目标: a rough picture made in MS-Paint

所以我试图制作方框2,一个div重叠框1,它也是一个div 我想让黄色边框覆盖形状而不是容器盒。我对stackoverflow进行了一些研究,并且为剪辑路径形状添加边框的答案是将相同的剪辑路径添加到容器中。 但我有点困惑,因为div本身就是容器,所以为什么边界没有包裹它。

HTML:

<div class="box-1">
BOX 1
</div>
<div class="box-2">
BOX 2
</div>

的CSS:

* {
    padding: 0;
    margin: 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

body {
    background-color: aqua;
}

.box-1 {
    padding: 25vh;
    background-color: aqua;
}

.box-2 {
    z-index: 200;
    -webkit-clip-path: polygon(50% -10%, 100% 11%, 100% 100%, 0 100%, 0 11%);
    clip-path: polygon(50% -10%, 100% 11%, 100% 100%, 0 100%, 0 11%);
    padding: 25vh;
    background-color: aquamarine;
    border-top: 10px solid yellow;
}

1 个答案:

答案 0 :(得分:0)

如果你想使用剪辑路径,你必须首先使用负边距使第二个框与第一个框重叠,或者更改最高值。然后你必须使用伪元素创建小箭头(你可以阅读更多关于How do CSS triangles work?

的信息

所以你会有这样的东西(没有剪辑路径):

&#13;
&#13;
* {
  padding: 0;
  margin: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  background-color: aqua;
}

.box-1 {
  padding: 25vh;
  background-color: aqua;
}

.box-2 {
  z-index: 2;
  position: relative;
  margin-top: -50px;
  padding: 25vh;
  background-color: #dede3b;
  border-top: 50px solid yellow;
}

.box-2:before {
  content: "";
  position: absolute;
  border-bottom: 40px solid #dede3b;
  border-right: 50px solid transparent;
  border-left: 50px solid transparent;
  right: 50%;
  margin-right: -50px;
  top: -40px;
}
&#13;
<div class="box-1">
  BOX 1
</div>
<div class="box-2">
  BOX 2
</div>
&#13;
&#13;
&#13;

然后创建剪辑路径以隐藏不需要的部分,如下所示:

&#13;
&#13;
* {
  padding: 0;
  margin: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  background-color: aqua;
}

.box-1 {
  padding: 25vh;
  background-color: aqua;
}

.box-2 {
  z-index: 2;
  position: relative;
  margin-top: -50px;
  padding: 25vh;
  background-color: #dede3b;
  border-top: 50px solid yellow;
  clip-path: polygon(0% 40px, 0% 100%, 100% 100%, 100% 40px, calc(50% + 50px) 40px, 50% 0, calc(50% - 50px) 40px);
  ;
}

.box-2:before {
  content: "";
  position: absolute;
  border-bottom: 40px solid #dede3b;
  border-right: 50px solid transparent;
  border-left: 50px solid transparent;
  right: 50%;
  margin-right: -50px;
  top: -40px;
}
&#13;
<div class="box-1">
  BOX 1
</div>
<div class="box-2">
  BOX 2
</div>
&#13;
&#13;
&#13;

以下是了解我用于剪辑路径的多边形的说明:

enter image description here

这是另一种不使用剪辑路径的解决方案(对所有浏览器都能更好地工作)。我们的想法是使用2个伪元素来创建双箭头:

&#13;
&#13;
* {
  padding: 0;
  margin: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  background-color: aqua;
}

.box-1 {
  padding: 25vh;
  background-color: aqua;
}

.box-2 {
  position: relative;
  padding: 25vh;
  background-color: #dede3b;
  border-top: 10px solid yellow;
}

.box-2:before {
  content: "";
  position: absolute;
  border-bottom: 50px solid yellow;
  border-right: 60px solid transparent;
  border-left: 60px solid transparent;
  right: 50%;
  margin-right: -60px;
  top: -55px;
  z-index: 1;
}

.box-2:after {
  content: "";
  position: absolute;
  border-bottom: 40px solid #dede3b;
  border-right: 50px solid transparent;
  border-left: 50px solid transparent;
  right: 50%;
  margin-right: -50px;
  top: -40px;
  z-index: 2;
}
&#13;
<div class="box-1">
  BOX 1
</div>
<div class="box-2">
  BOX 2
</div>
&#13;
&#13;
&#13;