【发布时间】:2022-02-13 04:06:43
【问题描述】:
任何 CSS 大师都知道为什么会这样吗?或者可以解释一下吗?通过添加相对于主体的位置,将 p 提升为在 div 内部的行为。我知道position:static 默认情况下发生在正文中,但是当我将其设置为相对时,p 标签的行为就像它嵌套在 div 标签内一样。有谁知道为什么会发生这种行为?
body {
background-color: purple;
margin: 0px;
padding: 0px;
position: relative;
}
div {
background-color: red;
height: 100px;
position: relative;
margin:0px;
padding: 0px;
}
p {
background-color: blue;
position: absolute;
bottom: 0px;
margin:0px;
}
<!DOCTYPE html>
<html>
<body>
<div>
<!-- it behaves like if it was here -->
</div>
<p>Hello World</p>
</body>
</html>
【问题讨论】:
-
position:relative 默认情况下不适用于任何元素。 static 甚至是 body 的默认值
-
对不起,我是这个意思。我更新了它
标签: html css document-body