html - 在图像上定位多个文本项

标签 html css

将几个单独的文本项放置在单个图像顶部的最简单方法(使用 css)是什么?

我有以下内容:-

<div>
    <img src="image.jpg"/>
    <p>One</p>
    <p>Two</p>
    <p>Three</p>
</div>

使用 CSS:-

div {
    position: relative;
}

div p {
    position: absolute;
    top: 0;
    left: 0;
}

但这行不通,因为所有的文本项都在另一个之上。 最好的方法是用列表来做吗?还是有别的办法?

最佳答案

像这样:

div { position:relative; }
div img { position:absolute; top: 0; left: 0; }  
div p { position:relative; z-index: 2; }

但这样 div 将采用文本的高度,而不是图像的高度。

关于html - 在图像上定位多个文本项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20080291/

相关文章:

css - DIV 不会在 IE 中堆叠

html - 将 div 定位到左侧,并在容器内右对齐 div

c# - 地铁 UI CSS ASP NET MVC

html - 避免使用 !important css 属性来显示我的容器

css - 如何在 mvc bootstrap 中自定义导航栏(宽度)

JavaScript 切换显示/隐藏响应式菜单

javascript - 显示带有书签的隐藏 div

javascript - YouTube 中的 Rel=0 和 autoplay=1 放大弹出窗口中的嵌入播放器不起作用

javascript - 如何在 if 语句返回 false 的情况下中止 jquery 函数

css - 移动端使用全屏背景图片固定约束的解决方法