gpt4 book ai didi

html - 响应图像中的中心文本

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:04 25 4
gpt4 key购买 nike

我在将响应式图片中的文本居中时遇到问题。我试过 vertical-align:middle。它使文本垂直居中,但位于图像旁边,而不是图像前面。

如您所见,我的图片是响应式的,因此我希望文本随图片一起移动。预先感谢您的帮助。

HTML:

 <div class="image">
<img src="http://static.inqmind.co/content/2013/05/aap-mob-always-strive-and-prosper/feature.jpg" class="ri" id="hv" />
ASAP ROCKY - Jack New City</div>
</div>

CSS:

 img.ri
{
position: relative;
max-width: 75%;
display: inline-block;
vertical-align: middle;
}

@media screen and (orientation: portrait) {
img.ri { max-width: 90%; }
}
@media screen and (orientation: landscape) {
img.ri { max-height: 90%; }
}

img#hv {
filter: url(filters.svg#grayscale); /* Firefox 3.5+ */
filter: gray; /* IE6-9 */
-webkit-filter: grayscale(1); /* Google Chrome & Safari 6+ */
}

img#hv:hover {
filter: none;
-webkit-filter: grayscale(0);

最佳答案

这是您可以做到的一种方法:http://jsfiddle.net/yJ8wh/

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>

.image {position: relative; text-align: center;}

.image span {position: absolute; line-height: 20px; display: block; top: 50%; margin-top: -10px; width: 100%; color: white;}

img.ri
{
position: relative;
max-width: 75%;
display: inline-block;
vertical-align: middle;
}

@media screen and (orientation: portrait) {
img.ri { max-width: 90%; }
}
@media screen and (orientation: landscape) {
img.ri { max-height: 90%; }
}

img#hv {
filter: url(filters.svg#grayscale); /* Firefox 3.5+ */
filter: gray; /* IE6-9 */
-webkit-filter: grayscale(1); /* Google Chrome & Safari 6+ */
}

img#hv:hover {
filter: none;
-webkit-filter: grayscale(0);}

</style>
</head>
<body>

<div class="image">
<img src="http://static.inqmind.co/content/2013/05/aap-mob-always-strive-and-prosper/feature.jpg" class="ri" id="hv">
<span>ASAP ROCKY - Jack New City</span>
</div>

</body>
</html>

如果你想让所有东西都左对齐,这是另一个选项:http://jsfiddle.net/DqCuA/

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>

.image {position: relative; text-align: center;}

.image b {display: block;position: absolute;line-height: 20px; top: 50%; margin-top: -10px; width: 100%; color: white; font-weight: normal;}

img.ri
{
position: relative;
max-width: 75%;
vertical-align: middle;
}

@media screen and (orientation: portrait) {
img.ri { max-width: 90%; }
}
@media screen and (orientation: landscape) {
img.ri { max-height: 90%; }
}

img#hv {
filter: url(filters.svg#grayscale); /* Firefox 3.5+ */
filter: gray; /* IE6-9 */
-webkit-filter: grayscale(1); /* Google Chrome & Safari 6+ */
}

img#hv:hover {
filter: none;
-webkit-filter: grayscale(0);}

</style>
</head>
<body>

<span class="image">
<img src="http://static.inqmind.co/content/2013/05/aap-mob-always-strive-and-prosper/feature.jpg" class="ri" id="hv">
<b>ASAP ROCKY - Jack New City</b>
</span>

</body>
</html>

关于html - 响应图像中的中心文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17024604/

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