html5定位

1.常用的三种定位

(1)固定定位:position: fixed;

理解:不会因为变动,脱离了文档流

(2)相对定位position: relative;

相对于自己之前的位置进行的定位,不会脱离文档流

(3)绝对定位position: absolute;

相对于设置了定位的父元素或者祖先元素进行定位,他会脱离正常的文档流

如果父元素没有设置定位他就会继续往上找一直找到根

 

------------------------------------------------------------------------------------------------------------------------------

(1)固定定位:position: fixed;

理解:不会因为变动,脱离了文档流

例子:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            .d1{
                width: 100px;
                height: 100px;
                background-color: aqua;
                position: fixed;
                right: 100px;
                top: 100px;
            }
            .d2{
                width: 100px;
                height: 100000px;
                background-color: black;
            }
        </style>
    </head>
    <body>
        <div class="d1"></div>
        <div class="d2"></div>
        <div class="d3"> </div>
    </body>
</html>

 

(2)相对定位position: relative;

相对于自己之前的位置进行的定位,不会脱离文档流

例子:如上图他自己本来的位置应该在红笔所在的位置,现在跑到了蓝颜色的位置,相对于他自己的位置进行改变

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            .d1{
                width: 30px;
                height: 30px;
                background-color: aqua;
                position: relative;
                lef: 100px;
                top: 100px;
            }
            .d2{
                width: 100px;
                height: 100000px;
                background-color: black;
            }
        </style>
    </head>
    <body>
        <div class="d1"></div>
        <div class="d2"></div>
        <div class="d3"> </div>
    </body>
</html>

 

 

(3)绝对定位position: absolute;

相对于设置了定位的父元素或者祖先元素进行定位,他会脱离正常的文档流

如果父元素没有设置定位他就会继续往上找一直找到根

例子:

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            .d1{
                width: 200px;
                height: 200px;
                background-color: aqua;
                position: relative;
                margin: 100px auto;
            }
            #dd{
                width: 10px ;
                height: 10px;
                background-color: #000000;
                position: absolute;
                bottom: 0px;
                right: 0px;
                
            }
            .d2{
                width: 200px;
                height: 100000px;
                background-color: black;
            }
        </style>
    </head>
    <body>
        <div class="d1">
            <div id="dd">
                
            </div>
        </div>
        <div class="d2"></div>
        <div class="d3"> </div>
    </body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章