css:标签样式边框与背景

时间:2018-04-02 09:40:17

标签: html css css3 background border

告诉我,我可以将背景设置为以下列方式形成的边框吗?

https://jsfiddle.net/2Lous8vq/1/

.object {
    position: relative;
    width: 300px;
    height: 0px;
    border-left: 50px solid transparent;
    border-right: 50px solid transparent;
    border-bottom: 100px solid red;
    background: transparent;
}

.object:after {
    content: '';
    position: absolute;
    top: 2px;
    left: calc(2.45px - 50px);
    width: calc(300px - 2 * 1.4px);
    height: 0px;
    border-left: 49px solid transparent;
    border-right: 49px solid transparent;
    border-bottom: calc(100px - 2px) solid white;
    background: transparent;
}

<div class="object"></div>

可能值得使用自定义border-image,但在我看来,这仍然没有足够的功能。

我希望选项卡上的白色代替显示图像背景(例如,https://cdn.pixabay.com/photo/2016/12/02/17/39/texture-1878273__340.jpg

1 个答案:

答案 0 :(得分:2)

您可以使用多个背景和渐变:

.object {
  width: 300px;
  height: 100px;
  background:
  linear-gradient(to bottom left,#fff 49%,red 49%,red 51%,transparent 0) 100% 0/40px 100% no-repeat,
  linear-gradient(to bottom right,#fff 49%,red 49%,red 51%,transparent 0) 0 0/40px 100% no-repeat,
  linear-gradient(red,red) 0 0/100% 2px no-repeat,
  url(https://cdn.pixabay.com/photo/2016/12/02/17/39/texture-1878273__340.jpg)0 0/cover no-repeat;
}
<div class='object'></div>

剪辑路径的另一个想法:

body {
 background:yellow;
}

.object {
  width: 300px;
  height: 100px;
  border-bottom:none;
  background: 
  linear-gradient(red,red) 0 0/100% 2px no-repeat,   
  linear-gradient(to bottom left,red 51%,transparent 51.5%) 100% 0/60px 100%  no-repeat,
  linear-gradient(to bottom right,red 51%,transparent 51.5%) 0 0/60px 100%  no-repeat,   
  url(https://cdn.pixabay.com/photo/2016/12/02/17/39/texture-1878273__340.jpg)0 0/cover no-repeat;
 -webkit-clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%);
clip-path: polygon(20% 0%, 80% 0%, 100% 100%, 0% 100%);
}
<div class='object'></div>

<强>更新

更多支持依赖更多元素的方式:

body {
 background:yellow;
}
* {
 box-sizing:border-box;
}
.object {
  width: 300px;
  height: 100px;
  border-bottom:none;
  font-size:0;
}
.object .left,
.object .right {
  width:50%;
  display:inline-block;
  height:100%;
  border-top:2px solid red;
  position:relative;
  overflow:hidden;
}
.object .left {
  border-left:2px solid red;
  transform:skew(-20deg);
  transform-origin:bottom left;
}
.object .right {
  border-right:2px solid red;
  transform:skew(20deg);
  transform-origin:bottom right;
}
.object .left:before {
  content:"";
  position:absolute;
  top:0;
  left:-20px;
  bottom:0;
  right:-20px;
  transform:skew(20deg);
  background:url(https://cdn.pixabay.com/photo/2016/12/02/17/39/texture-1878273__340.jpg); 
}
.object .right:before {
  content:"";
  position:absolute;
  top:0;
  left:-20px;
  bottom:0;
  right:-20px;
  transform:skew(-20deg);
  background:url(https://cdn.pixabay.com/photo/2016/12/02/17/39/texture-1878273__340.jpg); 
}
<div class='object'>
<div class="left"></div>
<div class="right"></div>
</div>