CSS:绝对定位、相对定位、固定定位

绝对定位

position:

   absolute

特性:

  1. 脱离文档流,定位元素占据的位置会释放
  2. 原点计算:如果该元素做了定位,那么就去找它做了定位的父元素(找最近的)作为原点基准,如果父元素都没做定位,则以浏览器(0,0)作为原点基准。
  3. 对内嵌元素定位后,设置宽高属性就会有效果

应用场景:

    一般情况下,绝对定位用在下拉菜单、焦点图轮播、弹出数字气泡、特别花边等场景

相对定位

position:

   relative

  1. 不脱离文档流,定位元素占据的位置不会被释放
  2. 原点计算:以父级元素作为原点基准,若没有父级元素则以浏览器(0,0)为基准。

一般的应用:父相子绝

  1. 父元素为相对定位,子元素为绝对定位,文档元素不会受影响。
  2. 父元素提供相对定位后,子元素以父元素为基准来进行定位。

应用场景:

   相对定位一般情况下很少自己单独使用,都是配合绝对定位使用,为绝对定位创造定位父级而又不设置偏移量

固定定位

position:

fixed

  1. 脱离了文档流

2、原点计算:以浏览器(0,0)作为原点基准,尽管父元素做了定位也不会影响它的原点

基准。

应用场景:

一般在网页中被用在窗口左右两边的固定广告、返回顶部图标、吸顶导航栏等

 

注意:使用定位后会激活如下5个属性

left | right | top | bottom | z-index

z-index

    改变定位后的叠放顺序

取值范围:-1~9999

其他:

设置网页元素的透明度

opacity: 0~1;

filter: opacity(0.2) | contrast(0.2)

绝对定位(absolute)代码案例:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>绝对定位</title>
        <style type="text/css">
            /*绝对定位:
             *1、脱离文档流,做了定位后它占据的位置会释放。
             *2、原点计算:如果该元素做了定位,那么就去找它做了定位的父元素(最近)作为原点基准,若果父元素
             * 都没做定位,则以浏览器(0,0)作为原点基准。
             *3、对内嵌元素做了定位后,它的宽度高度属性就会有效。
             */
            *{
                padding: 0px;
                margin: 0px;
            }
            .box-father{
                width: 500px;
                height: 500px;
                margin-left: 500px;
                background-color: yellow;
                position: absolute;
            }
            .son{
                width: 400px;
                height: 400px;
                margin-left: 20px;
                background-color: black;
                position: absolute;
            }
            .box{
                width: 300px;
                height: 300px;
                background-color: blue;
                /*绝对定位*/
                position: absolute;
                /*激活4个属性*/
                left: 150px;
                /*right: ;*/
                top: 100px;
                /*bottom: ;*/
            }
            .box2{
                width: 400px;
                height: 400px;
                background-color: red;
            }
            span{
               width: 200px;
               height: 200px;
               background-color: green;
              /* position: absolute;*/
               float: left;
            }
        </style>
    </head>
    <body>
       <div class="zx"> <!-- 祖先 :定位-->
        <div class="box-father">  <!-- 爷爷 :定位-->
            <div class="son">   <!-- 儿子:定位-->
               <div class="box"> <!-- 孙子:如果孙子做了定位,它就去找接近它定位最近的父元素来做为基准 -->
            
               </div>
            </div>
        </div>
      </div>
        <div class="box2">
            
        </div>
        <span>我是span</span>
    </body>
</html>

相对定位(relative)代码案例:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>相对定位</title>
        <style type="text/css">
            /*相对定位:
             *1、没有脱离文档流,元素定位了占据的空间不会被释放
             *2、原点计算:根据父元素的位置来做基准,如果没有父元素则以浏览器(0,0)作为基准。
             *  父相子绝:
             *    1、文档元素不会受影响
             *    2、父元素提供相对定位后,子元素就以父元素为基准来做绝对定位。
             */
            *{
                padding: 0px;
                margin: 0px;
            }
            .box-father{
                width: 500px;
                height: 500px;
                margin-left: 100px;
                background-color: yellow;

                /*相对定位*/
                /*position: relative;
                left: 100px;*/
            }
            .box{
                width: 200px;
                height: 200px;
                background-color: blue;
                
                position: absolute;
                right: 0px;
                bottom:0px;
            }
            .box-one{
                width: 400px;
                height: 400px;
                background-color: red;
            }
            /*.box2{
                width: 400px;
                height: 400px;
                background-color: red;
            }*/
        </style>
    </head>
    <body>
        <div class="box-father">
            <div class="box-one">
                
            </div>
            <div class="box">
            
            </div>
        </div>
        <!--<div class="box2">
            
        </div>-->
    </body>
</html>

固定定位(fixed)代码案例:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>固定定位</title>
        <style type="text/css">
            /*固定定位:
             *1、脱离了文档流
             *2、原点计算:按浏览器(0,0)来作为基准
             */
            *{
                padding: 0px;
                margin: 0px;
            }
            body{
                height: 1800px;
                background-image: url(img/logo.png);
            }
            .box-father{
                width: 500px;
                height: 500px;
                background-color: yellow;
                position: relative;
            }
            .box{
                width: 200px;
                height: 200px;
                background-color: blue;
                /*固定定位*/
                position: fixed;
                right: 0px;
                bottom: 0px;
            }
        </style>
    </head>
    <body>
       <div class="box-father">
        <div class="box">
            
        </div>
       </div>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/Mr_zdk/article/details/82704196