三角形的边框图像渐变模式

时间:2016-07-28 09:26:21

标签: css border css-shapes border-image css-gradients

我想用css创建一个奇形怪状的三角形。我的第一个想法是使用transform: rotate的透明边框并且它有效(见左三角形)。现在我想使用渐变边框图像模式作为同一个三角形的背景,但我无法使其工作。我尝试了许多改变border-width,使用包装器和overflow:hidden之类的东西,没有任何效果。在这里,我发布了一个尝试(见正确的形状),因为你看到图案占据了所有的空间,而不是遵循三角形的形状。有什么想法吗?

#top-left {
  position:absolute;
  left:78px;
  width: 0;
  height: 0;
  border-top: 100px solid transparent;  
  border-right: 80px solid black;
  border-bottom: 50px solid transparent;
  -webkit-transform: rotate(-20deg); 
}

#top-right {
  position:absolute;
  left:300px;
  width: 0;
  height: 0;
  border-image: repeating-linear-gradient( 0deg, pink, pink 1%, purple 1%, purple 8%) 10;
  border-image-width: 100px 80px 50px 0px;
  border-width: 100px 80px 50px 0px;
  border-style: solid;
  -webkit-transform: rotate(-20deg); 
}
<div id="wrapper">
 <div id="top-left"></div>  
 <div id="top-right"></div>
</div>

编辑:Andrey Fedorov的答案很好,但是当背景不是纯色时会出现问题,例如:

body{  
background-color: #6d695c;
background-image:
repeating-linear-gradient(120deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
repeating-linear-gradient(60deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
linear-gradient(60deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1)),
linear-gradient(120deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1));
background-size: 70px 120px;
}
#wrapper {
  position: relative;
}

#top-left {
  position:absolute;
  left:0px;
	width: 0;
	height: 0;
	border-top: 100px solid #fff;  
	border-right: 80px solid transparent;
	border-bottom: 50px solid #fff;
  -webkit-transform: rotate(-20deg); 
}

#top-right {
  position:absolute;
  z-index: -10;
  left:0;
  width: 0;
  height: 0;
  border-image: repeating-linear-gradient( 0deg, pink, pink 1%, purple 1%, purple 8%) 10;
  border-image-width: 100px 80px 50px 0px;
  border-width: 100px 80px 50px 0px;
  border-style: solid;
  -webkit-transform: rotate(-20deg); 
}
<div id="wrapper">
 <div id="top-left"></div>  
 <div id="top-right"></div>
</div>

2 个答案:

答案 0 :(得分:1)

一种可能的解决方案是:

  • 将两个形状放在同一个地方。
  • 使用z-index使带有模式的那个隐藏在另一个
  • 之后
  • 使用白色(或任何颜色为形状背景)绘制三角形外的边框区域。
  • 使具有三角形颜色的边框透明

&#13;
&#13;
#wrapper {
  position: relative;
  }

#top-left {
  position:absolute;
  left:0px;
	width: 0;
	height: 0;
	border-top: 100px solid #fff;  
	border-right: 80px solid transparent;
	border-bottom: 50px solid #fff;
  -webkit-transform: rotate(-20deg); 
}

#top-right {
  position:absolute;
  z-index: -10;
  left:0;
  width: 0;
  height: 0;
  border-image: repeating-linear-gradient( 0deg, pink, pink 1%, purple 1%, purple 8%) 10;
  border-image-width: 100px 80px 50px 0px;
  border-width: 100px 80px 50px 0px;
  border-style: solid;
  -webkit-transform: rotate(-20deg); 
}
&#13;
<div id="wrapper">
 <div id="top-left"></div>  
 <div id="top-right"></div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您仍然可以linear-gradient使用no-repeatbackground-size来绘制每个部分:

来自单个标签的步骤示例:

/* testing gradients */
p , div#wrapper {
  width:80px;
  float:left;
  margin:1em;
  height:150px;
  /* see me then remove this  shadow */
  box-shadow:0 0 0 2px;
}
p {
  background:
    linear-gradient(130deg, transparent 49.75%, pink 50.5%) 0 42px  no-repeat ;
  background-size:
    100% 15px;
  transform: rotate(-20deg); 
}
p + p{
  background:
    linear-gradient(130deg, transparent 49.75%, pink 50.5%) 0 42px  no-repeat ,
    linear-gradient(130deg,transparent 62px, purple 63px) top no-repeat;
  background-size:
    100% 15px,
    100% 65%;
}
p + p + p {
  
  background:
    linear-gradient(130deg, transparent 49.75%, pink 50.5%) 0 42px  no-repeat ,
    linear-gradient(130deg,transparent 62px, purple 63px) top no-repeat,
    linear-gradient(33deg , transparent 42px, pink 43px) no-repeat bottom;
  background-size:
    100% 15px,
    100% 65%, 
    100% 8px;
}
p+ p + p + p {
    background:
    linear-gradient(130deg, transparent 49.75%, pink 50.5%) 0 42px  no-repeat ,
    linear-gradient(130deg,transparent 62px, purple 63px) top no-repeat,
    linear-gradient(33deg , transparent 42px, pink 43px) no-repeat bottom, 
    linear-gradient(33deg, transparent 42px, purple 43px) bottom no-repeat;
  background-size:
    100% 15px,
    100% 65%, 
    100% 8px, 
    100% 35.5%;
  }
p:last-of-type{
  box-shadow:0 0
}

/* your original CSS/issue */
body{  
background-color: #6d695c;
background-image:
repeating-linear-gradient(120deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
repeating-linear-gradient(60deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
linear-gradient(60deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1)),
linear-gradient(120deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1));
background-size: 70px 120px;
}
#wrapper {
  position: relative;
}

#top-left {
  position:absolute;
  left:0px;
	width: 0;
	height: 0;
	border-top: 100px solid #fff;  
	border-right: 80px solid transparent;
	border-bottom: 50px solid #fff;
  -webkit-transform: rotate(-20deg); 
  transform: rotate(-20deg); 
}

#top-right {
  position:absolute;
  z-index: -10;
  left:0;
  width: 0;
  height: 0;
  border-image: repeating-linear-gradient( 0deg, pink, pink 1%, purple 1%, purple 8%) 10;
  border-image-width: 100px 80px 50px 0px;
  border-width: 100px 80px 50px 0px;
  border-style: solid;
  -webkit-transform: rotate(-20deg); 
  transform: rotate(-20deg); 
}
<!-- your issue -->
<div id="wrapper">
 <div id="top-left"></div>  
 <div id="top-right"></div>
</div>
<!-- p for testing purpose -->
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>

inbricated element + gradient&amp;变换也可以做到:

body{  
background-color: #6d695c;
background-image:
repeating-linear-gradient(120deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
repeating-linear-gradient(60deg, rgba(255,255,255,.1), rgba(255,255,255,.1) 1px, transparent 1px, transparent 60px),
linear-gradient(60deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1)),
linear-gradient(120deg, rgba(0,0,0,.1) 25%, transparent 25%, transparent 75%, rgba(0,0,0,.1) 75%, rgba(0,0,0,.1));
background-size: 70px 120px;
}

div.inbricate {
  margin:1em;
  height:150px;
  width:80px;
  position:relative;
  overflow:hidden;
  transform:rotate(-20deg);
  box-shadow: 0 0 ;
}
.inbricate div {
  transform:rotate(31deg) scale(1.2, 0.9) skew(-5deg);
  transform-origin: 100% 102%;
  height:100%;
  background:linear-gradient(-40deg,   pink 8%, purple 8%, purple 65%, pink 65%, pink 75%, purple 75% )
}
<div class=inbricate>
  <div>
  </div>
</div>