zoukankan      html  css  js  c++  java
  • Css之Relative

    CSS 中position:absolute的定位到底是相当于body,还是父级元素的问题

    position:absolute的定位是一个相对麻烦的问题。

    首先在文档流中,定义为position:absolute的元素已经被删除了。

    那它的定位到底是相对于body,还是父级元素呢?

    结论如下:

    position:absolute是相对于他的包含块中第一个有position:absolute或者position:relative属性的父级元素,如果都没有,就是相对于body。

    举例如下:

    1、它的父级元素都没有,就是相对于body定位的。

    <!DOCTYPE html>
    
    <html>
    <head>
        <title>test</title>
    </head>
    
    <body>
        <div style="200px;height:200px;background-color:red;">
            <div style="100px;height:100px;background-color:blue;">
                <div style="50px;height:50px;background-color:yellow;position:absolute;left:20px;top:10px;"></div>
            </div>
        </div>
    </body>
    </html>

    2、它的父级元素里有,就是相对于哪个有的父级元素。

    例一:相对于第一个div定位

    <!DOCTYPE html>
    
    <html>
    <head>
        <title>test</title>
    </head>
    
    <body>
        <div style="200px;height:200px;background-color:red;position:absolute;">
            <div style="100px;height:100px;background-color:blue;">
                <div style="50px;height:50px;background-color:yellow;position:absolute;left:20px;top:10px;"></div>
            </div>
        </div>
    </body>
    </html>

    例二:相对于第二个div定位

    <!DOCTYPE html>
    
    <html>
    <head>
        <title>test</title>
    </head>
    
    <body>
        <div style="200px;height:200px;background-color:red;position:absolute;">
            <div style="100px;height:100px;background-color:blue;position:relative;">
                <div style="50px;height:50px;background-color:yellow;position:absolute;left:20px;top:10px;"></div>
            </div>
        </div>
    </body>
    </html>
  • 相关阅读:
    学习笔记
    js闭包
    一个非必现问题的定位和反思
    C语言的设计模式面向对象机制的实现(一)
    多线程和单线程的执行效率问题
    python 多态
    C语言的设计模式接口隔离
    构建表达式二叉树
    C语言的设计模式依赖倒置
    C语言的设计模式单一职责
  • 原文地址:https://www.cnblogs.com/zyxiaohuihui/p/9119168.html
Copyright © 2011-2022 走看看