我正在尝试在网站上构建横幅元素,但在使其满足我所追求的标准时遇到问题。理想情况下,我希望有一个仅 CSS 的解决方案。
布局非常简单 - 背景图像,覆盖居中的文本。如下图:
按照优先级顺序,我需要元素执行以下操作:
aspect-ratio
CSS 中使用图像宽度和高度。我的第一次尝试是这样的:
div.wrapper {
display: grid;
grid-template-rows: repeat(1, minmax(0, 1fr));
grid-template-columns: repeat(1, minmax(0, 1fr));
}
div.image-holder {
width: 100%;
min-height: 80svh;
grid-row-start: 1;
grid-column-start: 1;
position: relative;
}
div.text-holder {
padding: 2rem;
display: flex;
justify-content: center;
align-items: center;
grid-row-start: 1;
grid-column-start: 1;
position: relative;
z-index: 30;
}
p {
max-width: 760px;
font-size: 1.8rem;
line-height: 2.3rem;
font-weight: 700;
text-align: center;
font-family: sans-serif;
}
img {
object-fit: cover;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
display: block;
opacity: 0.5;
}
<div class="wrapper">
<div class="image-holder">
<img
src="https://placebear.com/1200/800" alt="Bears" />
</div>
<div class="text-holder">
<p>Central text element, amount of text unknown. Solution must work for any amount of added text.</p>
</div>
</div>
这满足了标准 1,但只要元素足够大以容纳文本,整体高度始终设置为 80svh,并且不会对图像的纵横比进行任何操作。
然后我尝试了以下方法:
div.wrapper {
display: grid;
grid-template-rows: repeat(1, minmax(0, 1fr));
grid-template-columns: repeat(1, minmax(0, 1fr));
}
div.image-holder {
width: 100%;
grid-row-start: 1;
grid-column-start: 1;
position: relative;
}
div.text-holder {
padding: 2rem;
display: flex;
justify-content: center;
align-items: center;
grid-row-start: 1;
grid-column-start: 1;
position: relative;
z-index: 30;
}
p {
max-width: 760px;
font-size: 1.8rem;
line-height: 2.3rem;
font-weight: 700;
text-align: center;
font-family: sans-serif;
}
img {
object-fit: cover;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
display: block;
opacity: 0.5;
}
<div class="wrapper">
<div class="image-holder">
<img
src="https://placebear.com/1200/800" alt="Bears" />
</div>
<div class="text-holder" style="aspect-ratio: 1200 / 800; // This is inlined as it is pulled from the image size from the CMS">
<p>Central text element, amount of text unknown. Solution must work for any amount of added text.</p>
</div>
</div>
这更接近,并且满足标准 1 和 3,但现在我没有将元素的最大高度设置为 80svh。因此该元素将以纵横比缩放到任意高度。
我不知道下一步该尝试什么。我希望这不是一个不寻常的布局目标,并且之前有人已经做到了这一点。如果我需要依靠 javascript 来实现这一点,我会的,但如果可能的话,我希望用 CSS 来完成它。
假设我们可以更改 HTML 结构,我们可以将
max-height: 100%
添加到文本和图像容器的容器中(在本示例中我使用 both-holder
),然后还将 max-height: 80svh
添加到主 wrapper
:
div.wrapper {
display: grid;
grid-template-rows: repeat(1, minmax(0, 1fr));
grid-template-columns: repeat(1, minmax(0, 1fr));
max-height: 80svh;
}
div.image-holder {
z-index: 5;
}
div.both-holder {
display: flex;
justify-content: center;
align-items: center;
grid-row-start: 1;
grid-column-start: 1;
position: relative;
max-height: 100%;
}
.text-holder {
z-index: 30;
padding: 2rem;
}
p {
max-width: 760px;
font-size: 1.8rem;
line-height: 2.3rem;
font-weight: 700;
text-align: center;
font-family: sans-serif;
}
img {
object-fit: cover;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
display: block;
opacity: 0.5;
}
<div class="wrapper">
<div class="both-holder" style="aspect-ratio: 1200 / 800; // This is inlined as it is pulled from the image size from the CMS">
<div class="image-holder">
<img src="https://placebear.com/1200/800" alt="Bears" />
</div>
<div class="text-holder">
<p>Central text element, amount of text unknown. Solution must work for any amount of added text.</p>
</div>
</div>
</div>
对于你的情况,你需要添加一些技巧。例如,另一张图像将具有相对于
position: absolute;
的 .wrapper
,以防 .image-holder
的高度太小。body{
min-width: 320px;
margin: 0;
}
.wrapper {
display: grid;
grid-template-areas: 'a';
position: relative;
--max-img-height: 80svh;
}
.image-holder {
grid-area: a;
height: fit-content(var(--max-img-height));
display: grid;
opacity: 0.5;
}
.image-holder img {
width: 100%;
}
.image-holder img:first-child {
max-height: var(--max-img-height);
}
.image-holder img:last-child {
position: absolute;
height: 100%;
object-fit: cover;
}
.text-holder {
padding: 2rem;
position: relative;
z-index: 1;
grid-area: a;
display: grid;
place-items: center;
}
.text-holder p {
max-width: 760px;
font-size: 1.8rem;
line-height: 2.3rem;
font-weight: 700;
text-align: center;
font-family: sans-serif;
margin: 0;
}
<div class="wrapper">
<div class="image-holder">
<img src="https://placebear.com/1200/800" alt="Bears">
<img src="https://placebear.com/1200/800" alt="Bears">
</div>
<div class="text-holder">
<p>Central text element, amount of text unknown. Solution must work for any amount of added text. Central text element, amount of text unknown.</p>
</div>
</div>
body {
min-width: 320px;
margin: 0;
}
.wrapper {
display: grid;
grid-template-areas: 'a';
position: relative;
--max-img-height: 80vh;
}
.image-holder {
grid-area: a;
min-height: 100%; /* Ensure it's tall enough to accommodate the text */
max-height: var(--max-img-height); /* Not taller than 80vh */
display: grid;
opacity: 0.5;
}
.image-holder img {
width: 100%;
height: 100%;
object-fit: cover; /* Maintain aspect ratio and cover the container */
}
.text-holder {
padding: 2rem;
position: relative;
z-index: 1;
grid-area: a;
display: grid;
place-items: center;
}
.text-holder p {
max-width: 760px;
font-size: 1.8rem;
line-height: 2.3rem;
font-weight: 700;
text-align: center;
font-family: sans-serif;
margin: 0;
}
<div class="wrapper">
<div class="image-holder">
<img src="https://placebear.com/1200/800" alt="Bears">
<img src="https://placebear.com/1200/800" alt="Bears">
</div>
<div class="text-holder">
<p>Central text element, amount of text unknown. Solution must work for any amount of added text. Central text element, amount of text unknown.</p>
</div>
</div>