小编Roy*_*asa的帖子

为什么 div 的默认位置值是静态的而不是相对的?

就像标题一样,为什么 div (或 p 或 html 中的任何其他元素)将 'position: static' 作为默认值?为什么不是“位置:相对”?我没有看到在我的 CSS 中使用“position: static”有任何好处。使用“position:relative”定义 html 中的所有 div 元素是否安全?

编辑:我很清楚静态、相对、绝对和固定定位之间的区别。但主要问题是为什么 div 或任何其他 html 元素的默认定位是静态的?这个例子:

超文本标记语言

<div class="div1">
    <div class="div2">
        <div class="div3">
            <div class="div-last"></div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.div1 {
    width: 700px;
    height: 700px;
    background-color: #000;
    position: relative;
}
.div2 {
    width: 600px;
    height: 600px;
    background-color: #333;
    position: relative;
    top: 20px;
    left: 20px;
}
.div3 {
    width: 500px;
    height: 500px;
    background-color: #777;
    position: absolute;
    right: 0;
    top: 30px;
}
.div-last {
    width: …
Run Code Online (Sandbox Code Playgroud)

html css css-position

5
推荐指数
1
解决办法
2066
查看次数

标签 统计

css ×1

css-position ×1

html ×1