匹配两个 div 的渐变,如果它们具有不同的宽度

Match gradients of two divs, if those have different width

我有 10 个内联 div,它们具有相同的渐变类型 - 45deg 线,但渐变具有不同的颜色并且 div 具有不同的宽度。

是否可以匹配渐变? (希望下面的图片解释它)

我的 CSS 渐变。只有颜色变化。

   #div1 {
     background: repeating-linear-gradient(
        45deg,
        rgba(155,155,155,0.8),
        rgba(155,155,155,0.8) 3px,
        rgba(250,250,250,0.4) 3px,
        rgba(250,250,250,0.4) 6px);
    }

    #div2 {
     background: repeating-linear-gradient(
        45deg,
        rgba(235,102,107,0.6),
        rgba(235,102,107,0.6) 3px,
        rgba(250,250,250,0.4) 3px,
        rgba(250,250,250,0.4) 6px);
    }
div {
  height:100px;
  display:inline-block;
}
<div id="div1" style="width: 30px"></div><div id="div2" style="width: 40px"></div>

现在的样子(线条不匹配):

我希望它看起来像:

将其设为一行 div,并使用如下内容:

    .book {
      background-image: linear-gradient(105deg,
      rgba($color-white, .9) 0%,
      rgba($color-white, .9) 50%,
      transparent 50%),
      url(../img/nat-10.jpg);

百分比(如果相等)会立即导致颜色变化,而不是逐渐变化。所以这从我的白色到透明。因此,使用一个 div 将使线条保持笔直,百分比将改变颜色。我的角度有点倾斜,所以您需要更改初始角度。

HTML:

    <section class="section-book" id="book">
      <div class="row">
        <div class="book">
          <div class="book__form">
            <form action="#" class="form">

我得到了这个:

<div id="div1" style="width: 100px; height: 50px;"></div>

#div1 {
 background: repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}

#div2 {
 background: repeating-linear-gradient(
    45deg,
    rgba(235,102,107,0.6),
    rgba(235,102,107,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
        background-position: -1px;
}
<div id="div1" style="width: 100px; height: 50px;"></div>
<div id="div2" style="width: 100px; height: 50px;"></div>

我认为 background-position 就是您要查找的内容

我认为这可以解决问题:使您的 div 坚固并在其上放置一个条纹 div。 (灵感来自 Byoung730)

div {height: 100px; display: inline-block;}

#div1 {
 background: repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}

#div2 {
 background: repeating-linear-gradient(
    45deg,
    rgba(235,102,107,0.6),
    rgba(235,102,107,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}

#div3 {
 background:rgba(155,155,155,0.8)}
 
#div4 {
 background:rgba(235,102,107,0.6)}
 
#div5 {
  position: relative;
  top: -100px;
  width: 500px;
 background: repeating-linear-gradient(
    45deg,
    rgba(255,255,255,1),
    rgba(255,255,255,1) 3px,
    rgba(255,255,255,0) 3px,
    rgba(255,255,255,0) 6px);}
your example:<br>
<div id="div1" style="width: 100px"></div><div id="div2" style="width: 400px"></div>

smooth one:<br>
<div id="div3" style="width: 100px"></div><div id="div4" style="width: 400px"></div><div id="div5"></div>

您可以在同一元素上使用两种渐变,并使用 background-clip 技巧来隐藏第一个渐变的一部分,直到您将看到第二个渐变的填充:

.box {
  height:100px;
  width:80px;
  padding-right:50px;
  margin:5px;
  display:inline-block;
 background: 
 repeating-linear-gradient(
    45deg,
    rgba(235,102,107,0.6),
    rgba(235,102,107,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) content-box,
 linear-gradient(#fff,#fff) content-box, /*avoid the overlap of both gradient*/
 repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) padding-box;
}
<div class="box"></div>
<div class="box" style="width:100px;"></div>
<div class="box" style="padding-right:100px;"></div>

如果你有超过 2 个渐变,你可以考虑 background-size。它的技巧是在每个背景层下都有一个白色背景层来隐藏之前的渐变:

.box {
  height:100px;
  width:300px;
  margin:5px;
  display:inline-block;
 background: 
 /*First gradient*/
 repeating-linear-gradient(
    45deg,
    rgba(235,102,107,0.6),
    rgba(235,102,107,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) left/30% 100%,
 linear-gradient(#fff,#fff) left/30% 100%,
 /*Second one*/
 repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) left/60% 100%,
 linear-gradient(#fff,#fff) left/60% 100%,
 /**/
 repeating-linear-gradient(
    45deg,
    rgba(15,15,15,0.8),
    rgba(15,15,15,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) left/80% 100%,
 linear-gradient(#fff,#fff) left/80% 100%,
 /**/
 repeating-linear-gradient(
    45deg,
    rgba(12,155,155,0.8),
    rgba(12,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px) left/100% 100%,
 linear-gradient(#fff,#fff) left/100% 100%;
  background-repeat:no-repeat;
}
<div class="box"></div>

这是另一个依靠 mix-blend-mode 以更少的代码实现相同结果的想法:

.box {
  height:100px;
  width:300px;
  position:relative;
  display:inline-block;
  background: 
  repeating-linear-gradient(
    45deg,
    rgba(0,0,0,0.6),
    rgba(0,0,0,0.6) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px),
    #fff;
}
.box::before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background:linear-gradient(to right,blue 20%,red 20%, red 40%,orange 40%);
  mix-blend-mode: lighten;
}
<div class="box"></div>

这是另一个依赖 background-attachment:fixed 的想法,您也可以在其中保持透明度:

.box {
  height:100px;
  width:30px;
  margin:5px 0;
  display:inline-block;
 background-attachment:fixed;
}
#f1 {
 background-image:repeating-linear-gradient(
    45deg,
    rgba(155,155,155,0.8),
    rgba(155,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}
#f2 {
 background-image:repeating-linear-gradient(
    45deg,
    rgba(15,15,15,0.8),
    rgba(15,15,15,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
}
#f3 {
 background-image:repeating-linear-gradient(
    45deg,
    rgba(12,155,155,0.8),
    rgba(12,155,155,0.8) 3px,
    rgba(250,250,250,0.4) 3px,
    rgba(250,250,250,0.4) 6px);
 }
<div class="box" id="f1"></div><div class="box" id="f2" style="width:100px"></div><div class="box" id="f3" style="width:150px"></div>

多背景的另一种方式:

.box {
  height:100px;
  width:300px;
  position:relative;
  display:inline-block;
  background: 
  repeating-linear-gradient(
    45deg,
    transparent,
    transparent 3px,
    rgba(250,250,250,1) 3px,
    rgba(250,250,250,1) 6px),
   linear-gradient(to right,
    rgba(235,102,107,0.6) 20%,
    rgba(155,155,155,0.8) 20%, rgba(155,155,155,0.8) 40%,
    rgba(15,15,15,0.8) 40%);
}
<div class="box"></div>