【问题标题】:Gradient Blend Multiple Images渐变混合多个图像
【发布时间】:2017-11-28 12:16:34
【问题描述】:

如何使用 CSS 使多个图像仅在特定区域相互渐变,如下图所示?

我尝试过的:

.container {
  position: relative;
  display: flex;
  height: 200px;
}

.container:before {
  content: '';
  position: absolute;
  width: 80px;
  top: 0;
  bottom: 0;
  right: 50%;
  margin-right: -40px;
  background: url(https://www.w3schools.com/w3css/img_fjords.jpg);
  filter: blur(5px);
  -webkit-filter: blur(5px);
}

.container > div {
  flex: 1;
  background-size: 100% 100%;
}
<div class="container">
  <div style="backgroud-image:url(https://www.w3schools.com/w3css/img_fjords.jpg)"></div>
  <div style="background-image:url(https://www.w3schools.com/w3css/img_fjords.jpg)"></div>
</div>

但是,如下图所示,背景图像没有褪色/过渡:

更新

我的问题没有得到任何可靠的答案,但这段代码似乎是迄今为止我能得到的最接近的答案。

PEN BY Peter Ramsing的修改

<div class="hero-image">
  <img src="http://static.peter.coffee/codepen-assets/keyboard-background.jpg" />
</div>
<div class="hero-before">
  <img src="http://static.peter.coffee/codepen-assets/keyboard-background.jpg" />
</div>

<style>
img {
  /*  To contain the image to the confines of the div  */
  max-width: 100%;
}

.hero-image {
  max-width: 100%; 
  width: 800px;
  margin: auto;
 }
.hero-before {
  max-width: 100%; 
  width: 800px;
  margin: auto;
}

.hero-image::after {
  display: block;
  position: relative;
  background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0) 0, #fff 100%);
  margin-top: -50px;
  height: 40px;
  width: 100%;
  content: '';
}
.hero-before::after {
  display: block;
  position: relative;
  background-image: linear-gradient(to bottom, #fff 0%, rgba(255, 255, 255, 0) 100%);
  margin-top: -345px;
  height: 50px;
  width: 100%;
  content: '';
}
</style>

【问题讨论】:

    标签: html css image gradient linear-gradients


    【解决方案1】:

    您可以在两张图片之间使用一些伪元素并在其上应用线性渐变。通过使用相同的颜色,您将创建此效果。您可能会注意到该解决方案可以使用 background colorbackround image,您只需要尊重背景中使用的颜色并将它们应用于伪元素即可。

    这里是一个例子,你可以根据需要调整伪元素的宽度:

    .container {
      position: relative;
      display: flex;
      min-height: 100px;
      margin-bottom:20px;
    }
    
    .container:before {
      content: '';
      position: absolute;
      width: 80px;
      top: 0;
      bottom: 0;
      right: 50%;
      margin-right: -40px;
      background: linear-gradient(to right, #c3986b, #0a4b67);
    }
    
    .container>div {
      flex: 1;
      background-size:100% 100%;
    }
    <div class="container">
      <div style="background:#c3986b;"></div>
      <div style="background:#0a4b67;"></div>
    </div>
    
    <div class="container">
      <div style="background-image:url(https://dummyimage.com/150x100/c3986b/14151a)"></div>
      <div style="background-image:url(https://dummyimage.com/150x100/0a4b67/14151a)"></div>
    </div>

    这是另一个带有面具的想法,适用于任何类型的图像

    .container {
      display: flex;
      min-height: 300px;
      margin-bottom:20px;
    }
    
    
    .container>div {
      flex: 1;
      background-size:0 0;
      position:relative;
      z-index:-1;
    }
    .container>div::before {
      content:"";
      position:absolute;
      background-image:inherit;
      background-size:cover;
      background-position:center;
      z-index:-1;
      top:0;
      bottom:0;
    }
    .container>div:first-child::before {
      left:0;
      right:-50px;
      -webkit-mask:linear-gradient(to left,transparent ,#fff 50px);
              mask:linear-gradient(to left,transparent ,#fff 50px);
    }
    .container>div:last-child::before {
      right:0;
      left:-50px;
      -webkit-mask:linear-gradient(to right,transparent ,#fff 50px);
              mask:linear-gradient(to right,transparent ,#fff 50px);
    }
    <div class="container">
      <div style="background-image:url(https://picsum.photos/id/1074/800/800.jpg)"></div>
      <div style="background-image:url(https://picsum.photos/id/1060/800/800.jpg)"></div>
    </div>
    
    <div class="container">
      <div style="background-image:url(https://picsum.photos/id/1070/800/800.jpg)"></div>
      <div style="background-image:url(https://picsum.photos/id/1062/800/800.jpg)"></div>
    </div>

    【讨论】:

    • 如果背景图片不是一种颜色怎么办?
    • @AmalinaAziz 我知道这有点晚了,但我用另一个适用于图像的解决方案更新了我的答案
    【解决方案2】:

    您可以将 background: linear-gradient()Flexbox 结合使用,以实现如下效果:

    div {
      display: flex; /* displays flex-items (children) inline */
      justify-content: space-around; /* horizontal alignment / icons are evenly distributed with equal space around them, i.e. all have equal space on both sides, that's why there are two units of space between them / you can also experiment with other values such as: "space-between", "space-evenly", "center" etc. */
      align-items: center; /* vertically centered */
      height: 100px;
      background: linear-gradient(to right, #c3986b 45%, #0a4b67 55%); /* adjust the % to your needs, the sum of both needs to evaluate to 100% */
    }
    
    img {border-radius: 50%}
    <div>
      <img src="http://lorempixel.com/50/50/" alt="icon1">
      <img src="http://lorempixel.com/50/50/" alt="icon2">
    </div>

    在给定的示例中,我将linear-gradient() 设为父级宽度的10%。该数字是通过减去%55% - 45% 中的两个值来计算的。

    为了增加其宽度,如果需要,只需增加较大的数字并减少较小的数字,最好减少相同数量的%,例如40% / 60%,使其水平居中。为了减小它的 width,只需做相反的事情。

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 2018-12-13
      • 2010-09-11
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      相关资源
      最近更新 更多