【发布时间】:2019-10-19 13:40:55
【问题描述】:
我不确定如何解释我的问题。所以这里有一个小提琴: https://jsfiddle.net/8zsqydj0/
.background-img-wrapper:before {
content: "";
top: 0;
left: 0;
position: absolute;
height: 100%;
width: 100%;
background: linear-gradient(to right, rgba(252, 252, 252, 1) 0%, rgba(255, 255, 255, 0) 20%, rgba(255, 255, 255, 0) 80%, rgba(252, 252, 252, 1) 100%);
}
.background-img-wrapper {
max-width: 1920px;
}
.background:before {
content: "";
top: 0;
left: 0;
position: absolute;
height: 100%;
width: 100%;
background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0) 80%, rgba(252, 252, 252, 1) 100%);
}
.background {
width: 100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<div class="row justify-content-center position-fixed background">
<div class="background-img-wrapper">
<img class="img-fluid" src="https://via.placeholder.com/1920x1080/000000/" />
</div>
</div>
我正在尝试将渐变应用到图像的三个侧面。我到了这么远。但是,图像以页面为中心并具有 1920 像素的固定(最大)宽度。渐变应用于页面 100% 的父 div,并且图像是 img-fluid。因此,当以 1920 像素或更低的分辨率查看渐变时,一切看起来都很好。但是,当您将页面高于宽度调整为 1920 像素时,渐变会随着窗口的一侧移动,而不是固定在图像的两侧。如果这有意义的话。
那么,如何将渐变应用到图像而不是父 div,或者如何将渐变的边限制为图像大小?我正在使用 Bootstrap 4。
另外,我不一定需要保持当前的结构。如果有更好的方法来实现这一切,请告诉我:)
【问题讨论】:
-
位置:相对于
.background-img-wrapper? -
嗯,谢谢你的建议!这确实可以将左/右渐变限制在图像的两侧。但它(重新?)移动了我的底部渐变。
-
z-index: 1;到另一个渐变 -
哇,成功了!非常感谢!如果您将其发布为完整答案,如果您愿意,我可以将其标记为解决方案。如果您愿意,请提出一个问题:是否有可能完全摆脱 background-img-wrapper ?我觉得有点没用无论如何,我的主要问题已解决。再次感谢! :)
标签: html css twitter-bootstrap bootstrap-4 linear-gradients