gpt4 book ai didi

html - 调整窗口大小时如何使图像和文本在 HTML 中对齐?

转载 作者:行者123 更新时间:2023-11-28 02:06:11 24 4
gpt4 key购买 nike

我正在尝试解决以下问题:在房屋蓝图的图像上,我需要放置“标签”。所以,在厨房上方,一定要有“KITCHEN”的标签;在浴室上方,“BATHROOM”等。即使调整浏览器窗口的大小,标签也必须保留在相应的房间上方。如何保证只使用 HTML/CSS/JS?

这是我的 HTML:

<html>
<head>
<link href="overlay.css" rel="stylesheet" type="text/css" />
</head>
<div class="image">
<img src="blueprint.png" alt="" />
<span>LABEL</span>
</div>
</html>

假设厨房位置在顶部 10% 和左侧 40%,overlay.css 将如下所示:

img {
max-width:100%;
max-height:100%;
}
.image {
position: relative;
width: 100%; /* for IE 6 */
overflow: hidden;
}
span {
position: absolute;
top: 10%;
left: 40%;
color: white;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0); /* fallback color */
background: rgba(0, 0, 0, 0.7);
}

这有点管用,但调整窗口大小会使一切都变得不合适和不成比例。

最佳答案

我希望 HTML5 元素给 figurefigcaption 一个机会。它们的嵌套非常和谐。

来自 W3C:

The figure element represents a unit of content, optionally with a caption, that is self-contained, that is typically referenced as a single unit from the main flow of the document, and that can be moved away from the main flow of the document without affecting the document’s meaning.

可以在 The figure & figcaption elements 找到精彩的博文.

让你的图像大小决定区域,不要像使用 img { max-width:100%; 那样用 CSS 强制它。最大高度:100%;

您的 CSS 也需要一些清理。

这是重新调整用途的代码。复制、粘贴并试一试。

<style type="text/css">
figure {
margin: 0;
}
img {
position: absolute;
}
figcaption {
background: black;
color: white;
display: inline-block;
font: bold 24px/45px Helvetica, Sans-Serif;
left: 10px;
padding: 10px;
position: relative;
top: 10px;
}
</style>
<figure>
<img src="bathroom.jpg" alt="Bathroom" />
<figcaption>Bathroom</figcaption>
</figure>

关于html - 调整窗口大小时如何使图像和文本在 HTML 中对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11418697/

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