gpt4 book ai didi

html - 在兄弟 div 上使用负 margin-top 覆盖带有 div 的 img 标签

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

我正在尝试使用 div 上的负 marginimgdiv 叠加,文本div 内部显示在图像上方,而 divbackground 显示在 img 下方。

html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="a">
<div class="b"><img src=""/></div>
<div class="c">somebody</div>
</div>
</body>
</html>

CSS:

.a{
height:60px;
background:#ccc;
}

.b img{
height:60px;
width:60px;
background:red;
}

.c{
background:blue;
height:20px;
margin-top:-20px;
}

我在 IE9 和 chrome 中观察到了这种行为。

这种行为是否符合预期?如果是,原因是什么?

我可以使 div absoluterelative 来实现我正在尝试做的事情,但我无法理解这种行为.

http://jsbin.com/gejuzuza/1/edit

最佳答案

如果您将 position: relative; 添加到 .c 的 CSS,它会将 divc 类在 div 之上,带有 b

关于html - 在兄弟 div 上使用负 margin-top 覆盖带有 div 的 img 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25164585/

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