渐变混合多个图像
Gradient Blend Multiple Images
如何使用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>
然而,没有fading/transition关于背景图片,如下图所示:
更新
我的问题没有得到任何可靠的答案,但这段代码似乎是迄今为止我能得到的最接近的答案。
的修改
<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>
您可以使用一些放在两张图片之间的伪元素,并在其上应用线性渐变。通过使用相同的颜色,您将创建此效果。您可能会注意到该解决方案将通过使用 背景颜色 和 背景图像 来工作,您只需要尊重背景中使用的颜色并应用他们到伪元素。
这里是一个例子,你可以根据需要调整伪元素的宽度:
.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>
您可以将 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,只需执行相反的操作即可。
如何使用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>
然而,没有fading/transition关于背景图片,如下图所示:
更新
我的问题没有得到任何可靠的答案,但这段代码似乎是迄今为止我能得到的最接近的答案。
的修改<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>
您可以使用一些放在两张图片之间的伪元素,并在其上应用线性渐变。通过使用相同的颜色,您将创建此效果。您可能会注意到该解决方案将通过使用 背景颜色 和 背景图像 来工作,您只需要尊重背景中使用的颜色并应用他们到伪元素。
这里是一个例子,你可以根据需要调整伪元素的宽度:
.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>
您可以将 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,只需执行相反的操作即可。