html - 是否可以在同一个 html 页面中多次使用相对位置?

标签 html css position

我在母版页上使用“相对位置”和“绝对位置”。

我有一个使用上面母版页的页面,我试图在这个页面中再次为其他 2 个元素使用“相对位置”和“绝对位置”,但下面的元素(“绝对位置”)在这个页面不是根据他上面的元素('position relative')放置的,而是指母版页中元素的'position relative'..

希望是不是太笨拙的解释..

是否可以在同一个 HTML 页面上多次使用“相对位置”???如果是,怎么办??

html代码

<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="basic.css">

</head>
<body>
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
<div class="four"></div>
</body>
</html>

CSS代码 body { 边距:0px; 填充:0px;

.one{
    width: 200px;
    height: 200px;
    background: red;
    position: relative;
}

.two{
    width: 200px;
    height: 200px;
    background: green;
    position: absolute;
    top: 0px;
    left: 200px;
}

.three{
    width: 200px;
    height: 200px;
    background: blue;
    position: relative;
}

.four{
    width: 200px;
    height: 200px;
    background: black;
    position: absolute;
    top: 0px;
    left: 200px;
}

codepen link

最佳答案

当使用 position: absolute 时,它是根据祖先计算的,而不是兄弟,所以你的 .two.four div 是相对于主体定位,而不是 .one.two,请改用它:

<div class="one">
    <div class="two"></div>
</div>
<div class="three">
    <div class="four"></div>
</div>

关于html - 是否可以在同一个 html 页面中多次使用相对位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25463799/

相关文章:

html - 将整个网站变灰

javascript - 使用 Javascript 和 CSS 动画创建文本选取框

html - 获取两个跨度以将一个跨度显示在缩略图旁边的另一个跨度下方

css - 如何不让 div 覆盖我的页面?

javascript - 如何让 div 在动画后跳回到原来的位置?

html - Firefox 9 (Mac) 解析边距有问题?

javascript - 选择选项之一(单选)时禁用某些输入元素

php - 将html表单数据发送到mysql数据库

html - 使用带图标的 Bootstrap-select

html - 什么是最好的方法或测试网站兼容性