使用 object-fit 在 flex 容器中实现重叠图像
Achieving an Overlapping image in a flex container with object-fit
嘿,我之前问过类似的问题并且能够实现它,问题是我必须使用 png 图像,缺点是它太大了。
经过一番研究,我发现了一种使用 svg 容器以及图像的 alpha 和 beta 通道的方法。
然而,我 运行 遇到的主要困难是让对象适合工作,这样图像将始终覆盖其 flexbox 容器的全部 50%...这有可能吗?我错过了什么..非常感谢。
https://codepen.io/HendrikEng/pen/RVzYoR?editors=0100
.c-hero {
align-items: center;
display: flex;
flex-direction: row;
justify-content: center;
background: grey;
height: 30px * 15;
text-align: center;
&__image {
display: flex;
margin-bottom: -60px;
margin-top: 60px + 19px;
max-height: 682px;
min-height: calc(100% + 140px);
object-fit: cover;
object-position: top;
width: 50%;
}
&__item {
align-self: center;
width: 50%;
}
}
<section>
<div class="c-hero">
<svg class="c-hero__image" preserveAspectRatio="xMinYMin" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<mask id="transparentmask">
<image width="100%" height="100%" xlink:href="http://i.imgur.com/n080w42.png"></image>
</mask>
</defs>
<image mask="url(#transparentmask)" width="100%" height="100%" xlink:href="http://i.imgur.com/LTgnz9E.jpg"></image>
</svg>
<div class="c-hero__item">
<p>Lorem Ipsum</p>
</div>
</div>
</section>
虽然问题建议使用重叠图像,但如果我正确解释它,实际情况是并排放置图像和填充的容器。要使用的图像是底部有一点透明度的图像。因此,虽然我们的眼睛可能会区分差异,但对于浏览器而言,它只是整个图像。
由于您希望图像旁边容器的高度 = 图像的高度 - transparent/white 区域的高度。
实现它的方法很少
1) 使用 2 个单独的图像:
看起来重叠的部分可能是具有绝对定位的不同图像,其 z-index 大于背景图像元素。
背景图像元素和下一个填充容器可以具有相同的高度。
2) 如果我们有一个固定的图像高度,那么对于这种特殊情况,我们可以为另一半使用图像高度的 86%。它会产生同样的错觉。 86% 因为完全覆盖的背景是整个图像的 86%。是的,我使用 GIMP 测量了像素比。
这种特殊情况更多地与您使用的图像大小有关,而不是一些编程技巧。虽然你所寻求的可以实现。
为了复制我在 codepen.
中创建的响应式解决方案
.wrapper {
max-width: 1200px;
margin: 0 auto;
}
.hero-img {
background-image: url(http://i.imgur.com/CkwLRd0.jpg);
background-size: cover;
background-position: bottom;
background-repeat: no-repeat;
width: 100%;
height: 100%;
}
.banner {
display: flex;
}
.half {
width: 50%;
height: 400px;
}
.red {
background: indianred;
}
.text-holder {
height: 86%;
}
<div class="wrapper">
<div class="banner">
<div class="half">
<div class="hero-img"></div>
</div>
<div class="half">
<div class="red text-holder"></div>
</div>
</div>
</div>
请注意,由于使用了相对图像大小,包装器设置为最大宽度:1200 像素。
让我知道它是否解决了您的问题,或者您是否需要更多帮助。
请将以下内容css放入您的codepen:
/**
* @define c-hero; weak
*/
.c-hero {
align-items: stretch;
display: flex;
flex-direction: row;
justify-content: center;
background: grey;
height: 28.45vw;
text-align: center;
&__image {
flex: 1 0 auto;
min-height: 130.96%;
}
&__item {
align-self: center;
width: 50%;
}
}
嘿,我之前问过类似的问题并且能够实现它,问题是我必须使用 png 图像,缺点是它太大了。
经过一番研究,我发现了一种使用 svg 容器以及图像的 alpha 和 beta 通道的方法。
然而,我 运行 遇到的主要困难是让对象适合工作,这样图像将始终覆盖其 flexbox 容器的全部 50%...这有可能吗?我错过了什么..非常感谢。
https://codepen.io/HendrikEng/pen/RVzYoR?editors=0100
.c-hero {
align-items: center;
display: flex;
flex-direction: row;
justify-content: center;
background: grey;
height: 30px * 15;
text-align: center;
&__image {
display: flex;
margin-bottom: -60px;
margin-top: 60px + 19px;
max-height: 682px;
min-height: calc(100% + 140px);
object-fit: cover;
object-position: top;
width: 50%;
}
&__item {
align-self: center;
width: 50%;
}
}
<section>
<div class="c-hero">
<svg class="c-hero__image" preserveAspectRatio="xMinYMin" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<mask id="transparentmask">
<image width="100%" height="100%" xlink:href="http://i.imgur.com/n080w42.png"></image>
</mask>
</defs>
<image mask="url(#transparentmask)" width="100%" height="100%" xlink:href="http://i.imgur.com/LTgnz9E.jpg"></image>
</svg>
<div class="c-hero__item">
<p>Lorem Ipsum</p>
</div>
</div>
</section>
虽然问题建议使用重叠图像,但如果我正确解释它,实际情况是并排放置图像和填充的容器。要使用的图像是底部有一点透明度的图像。因此,虽然我们的眼睛可能会区分差异,但对于浏览器而言,它只是整个图像。
由于您希望图像旁边容器的高度 = 图像的高度 - transparent/white 区域的高度。
实现它的方法很少
1) 使用 2 个单独的图像:
看起来重叠的部分可能是具有绝对定位的不同图像,其 z-index 大于背景图像元素。 背景图像元素和下一个填充容器可以具有相同的高度。
2) 如果我们有一个固定的图像高度,那么对于这种特殊情况,我们可以为另一半使用图像高度的 86%。它会产生同样的错觉。 86% 因为完全覆盖的背景是整个图像的 86%。是的,我使用 GIMP 测量了像素比。
这种特殊情况更多地与您使用的图像大小有关,而不是一些编程技巧。虽然你所寻求的可以实现。 为了复制我在 codepen.
中创建的响应式解决方案.wrapper {
max-width: 1200px;
margin: 0 auto;
}
.hero-img {
background-image: url(http://i.imgur.com/CkwLRd0.jpg);
background-size: cover;
background-position: bottom;
background-repeat: no-repeat;
width: 100%;
height: 100%;
}
.banner {
display: flex;
}
.half {
width: 50%;
height: 400px;
}
.red {
background: indianred;
}
.text-holder {
height: 86%;
}
<div class="wrapper">
<div class="banner">
<div class="half">
<div class="hero-img"></div>
</div>
<div class="half">
<div class="red text-holder"></div>
</div>
</div>
</div>
请注意,由于使用了相对图像大小,包装器设置为最大宽度:1200 像素。
让我知道它是否解决了您的问题,或者您是否需要更多帮助。
请将以下内容css放入您的codepen:
/**
* @define c-hero; weak
*/
.c-hero {
align-items: stretch;
display: flex;
flex-direction: row;
justify-content: center;
background: grey;
height: 28.45vw;
text-align: center;
&__image {
flex: 1 0 auto;
min-height: 130.96%;
}
&__item {
align-self: center;
width: 50%;
}
}