gpt4 book ai didi

html - 动态高度包装器中的垂直居中文本

转载 作者:太空宇宙 更新时间:2023-11-04 01:43:43 24 4
gpt4 key购买 nike

我通常只使用 line-height 来垂直居中,但这次我使用的布局有点棘手。

我已经将这个 jsfiddle 放在一起以显示我目前所处的位置。所有的 CSS hacks 建议为此使用表格单元格技巧,但我只能在包装器具有绝对高度的情况下让它工作,所以对我来说这个文本不是垂直居中的:

<div class="wrap">
<a href="#">
<img src="http://www.thekrausemouse.com/wp-content/uploads/2016/03/sample-1.jpg" />
<span class="text"><span>Text that might span multiple lines</span></span>
</a>
</div>

https://jsfiddle.net/fdtbvmcw/

我基本上需要的是文本,无论它跨越多少行,都位于图像的中间。图像不能是背景图像,我不能将固定宽度或高度附加到包装器。

包装器在更大的页面模板中模拟响应列,我需要图像保留您看到的该列的完整宽度。如果需要,可以在列中添加其他 HTML。

想法?

最佳答案

Flexbox 可以做到这一点......

.wrap {
height: auto;
position: relative;
width: 50%;
}

.wrap a img {
height: auto;
width: 100%;
}

.wrap a span.text {
height: 100%;
left: 0;
position: absolute;
text-align: center;
top: 0;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}

.wrap a span.text span {
color: #fff;
font-size: 20px;
font-weight: bold;
line-height: 1.25
}
<div class="wrap">
<a href="#">
<img src="http://www.thekrausemouse.com/wp-content/uploads/2016/03/sample-1.jpg" />
<span class="text"><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id praesentium nihil iure amet dolore nulla totam modi </span></span>
</a>
</div>

关于html - 动态高度包装器中的垂直居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44924602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com