如何通过CSS创建带有渐变色条纹边框的矩形?

How to create Rectangle With Gradient Color Stripes Border via CSS?

我想创建带有渐变色条纹边框的圆边矩形

我想使用 img 标签或 div 标签来包含图像和渐变条纹边框。

它应该是这样的:

我尝试四处搜索,发现 (SCSS) 示例:https://jsfiddle.net/rami7250/yujsz7wf/

我得到了这个 SCSS 代码:

.module {
  background: white;
  border: 1px solid #ccc;
  margin: 3%;
  > h2 {
    padding: 1rem;
    margin: 0 0 0.5rem 0;
  }
  > p {
    padding: 0 1rem;
  }
  /*animation: widen 10s linear alternate infinite;*/
}

.stripe-1 {
  color: white;
  background: repeating-linear-gradient(
    45deg,
    #606dbc,
    #606dbc 10px,
    #465298 10px,
    #465298 20px
  );
}

如您所见,该功能仅影响 background 而不会影响 border

如何使矩形通过 CSS/SCSS 在他的边框中显示渐变色条纹?

找到了可以成为该设计关键的东西。看这里:https://jsfiddle.net/rami7250/jaca7sb4/

代码来源如下:http://codepen.io/SitePoint/pen/DKhkf

CSS:

div {
  position: relative;
  height: 100%;

}
div:before, div:after {
  position: absolute;
  content: '';
  height: 1em;
  top: 0;
  left: 0;
  right: 0;
}
div:after {
  top: auto;
  bottom: 0;
}
div:before {
  background: linear-gradient(90deg, #1abc9c 1%, #2ecc71 1%, #2ecc71 29%, #3498db 29%, #3498db 32%, #9b59b6 32%, #9b59b6 37%, #34495e 37%, #34495e 53%, #f1c40f 53%, #f1c40f 69%, #e67e22 69%, #e67e22 67%, #e74c3c 67%, #e74c3c 74%, #ecf0f1 74%, #ecf0f1 100%, #95a5a6 100%);
}
div:after {
  background: linear-gradient(90deg, #1abc9c 10%, #2ecc71 10%, #2ecc71 12.5%, #3498db 12.5%, #3498db 28%, #9b59b6 28%, #9b59b6 35%, #34495e 35%, #34495e 60%, #f1c40f 60%, #f1c40f 69%, #e67e22 69%, #e67e22 83%, #e74c3c 83%, #e74c3c 88%, #ecf0f1 88%, #ecf0f1 96%, #95a5a6 96%);
}

div {
  padding-top: 20px;
  width: 450px;
}

但是,我无法将其设置在div 的 侧。仅影响顶部和底部。

一种选择是在所需模式中使用 linear-gradient 作为 background-image,并将所需内容放在该背景之上。这种方法只要内容有效,无论是图像还是任何文本或任何不需要透明的内容。将鼠标悬停在元素上可以看到输出也是响应式的。

只有条带:(白色图层放在渐变的顶部以仅显示条带)

div {
  height: 250px;
  width: 400px;
  background: linear-gradient(white, white), 
              linear-gradient(60deg, orange 0%, orange 3.5%, white 3.5%, white 7%, hotpink 7%, hotpink 10.5%, black 10.5%, black 14%, brown 14%, brown 17.5%, aqua 17.5%, aqua 21%, green 21%, green 25%, orange 25%, orange 28.5%, white 28.5%, white 32%, hotpink 32%, hotpink 35.5%, black 35.5%, black 39%, brown 39%, brown 42.5%, aqua 42.5%, aqua 46%, green 46%, green 50%, orange 50%, orange 53.5%, white 53.5%, white 57%, hotpink 57%, hotpink 60.5%, black 60.5%, black 64%, brown 64%, brown 67.5%, aqua 67.5%, aqua 71%, green 71%, green 75%, orange 75%, orange 78.5%, white 78.5%, white 82%, hotpink 82%, hotpink 85.5%, black 85.5%, black 89%, brown 89%, brown 92.5%, aqua 92.5%, aqua 96%, green 96%, green 100%);
  padding: 10px 20px;
  border-radius: 20px;
  background-origin: content-box, padding-box;
  background-clip: content-box, padding-box;
}

/* just for demo */

div {
  transition: all 1s;
}
div:hover {
  height: 300px;
  width: 500px;
}
<div></div>


里面有图片 div:

div {
  height: 250px;
  width: 400px;
  background: linear-gradient(white, white), linear-gradient(60deg, orange 0%, orange 3.5%, white 3.5%, white 7%, hotpink 7%, hotpink 10.5%, black 10.5%, black 14%, brown 14%, brown 17.5%, aqua 17.5%, aqua 21%, green 21%, green 25%, orange 25%, orange 28.5%, white 28.5%, white 32%, hotpink 32%, hotpink 35.5%, black 35.5%, black 39%, brown 39%, brown 42.5%, aqua 42.5%, aqua 46%, green 46%, green 50%, orange 50%, orange 53.5%, white 53.5%, white 57%, hotpink 57%, hotpink 60.5%, black 60.5%, black 64%, brown 64%, brown 67.5%, aqua 67.5%, aqua 71%, green 71%, green 75%, orange 75%, orange 78.5%, white 78.5%, white 82%, hotpink 82%, hotpink 85.5%, black 85.5%, black 89%, brown 89%, brown 92.5%, aqua 92.5%, aqua 96%, green 96%, green 100%);
  padding: 10px 20px;
  border-radius: 20px;
  background-origin: content-box, padding-box;
  background-clip: content-box, padding-box;
}
img {
  height: 250px;
  width: 400px;
}
/* just for demo */

div, img {
  transition: all 1s;
}
div:hover, div:hover img {
  height: 300px;
  width: 500px;
}
<div>
  <img src='http://lorempixel.com/400/250/nature/1' />
</div>


具有重复线性渐变:(感谢 vals 创建演示)

div {
  height: 250px;
  width: 400px;
  background: linear-gradient(white,white), repeating-linear-gradient(60deg, orange 0%, orange 3.5%, white 3.5%, white 7%, hotpink 7%, hotpink 10.5%, black 10.5%, black 14%, brown 14%, brown 17.5%, aqua 17.5%, aqua 21%, green 21%, green 25%);
  background-attachment: padding-box, border-box;
  padding: 10px 20px;
  border-radius: 20px;
  background-origin: content-box, padding-box;
  background-clip: content-box, padding-box;
}

/* just for demo */

div {
  transition: all 1s;
}
div:hover {
  height: 300px;
  width: 500px;
}
<div>
</div>

注意:锯齿状边缘是使用倾斜渐变时很难避免的。可以通过调整颜色停止点来减少它们。

不用写的这么复杂,两行CSS:

.module {
    border: 20px solid;
    border-image: repeating-linear-gradient(45deg,#606dbc,#606dbc 10px,#465298 10px,#465298 20px) 20;
}

你的 Fiddle 的一个分支:https://jsfiddle.net/5k70jt5f/1/

另一个想法,我们可以屏蔽重复的线性渐变背景。

body {
  background: gray;
}

.stripe {
  --thickness: 10px;
  --stripe: 20px;
  height: 200px;
  width: 300px;
  --mask: linear-gradient(#000 0 0) 0 0/100% var(--thickness) no-repeat, 
          linear-gradient(#000 0 0) 0 0/var(--thickness) 100% no-repeat, 
          linear-gradient(#000 0 0) 0 100% / 100% var(--thickness) no-repeat, 
          linear-gradient(#000 0 0) 100% 0/ var(--thickness) 100% no-repeat;
  -webkit-mask: var(--mask);
  mask: var(--mask);
  background: repeating-linear-gradient(45deg, #febf28 0 var(--stripe), #000 var(--stripe) calc(var(--stripe) * 2))
}

.original-gradient {
  margin-top: 20px;
  --stripe: 20px;
  height: 200px;
  width: 300px;
  background: repeating-linear-gradient(45deg, #febf28 0 var(--stripe), #000 var(--stripe) calc(var(--stripe) * 2))
}
<div class="stripe"></div>
<div class="original-gradient"></div>