position笔记

page1
css3对position属性的相关定义:
static:无特殊定位,对象遵循正常文档流。top,right,bottom,left等属性不会被应用。
relative:对象遵循正常文档流,但将依据top,right,bottom,left等属性在正常文档流中偏移位置。而其层叠通过z-index属性定义。
absolute:对象脱离正常文档流,使用top,right,bottom,left等属性进行绝对定位。而其层叠通过z-index属性定义。
fixed:对象脱离正常文档流,使用top,right,bottom,left等属性以窗口为参考点进行定位,当出现滚动条时,对象不会随着滚动。而其层叠通过z-index属性定义。*/
 
page2
static(无特殊定位,它是html元素默认的定位方式,即我们不设定元素的position属性时默认的position值就是static,
它遵循正常的文档流对象,对象占用文档空间,该定位方式下,top、right、bottom、left、z-index等属性是无效的,但也不会报错)
     * {
            margin: 0;
            padding: 0;
        }
        .static-test {
             100px;
            height: 60px;
            border: 1px solid red;
            display: inline-block;
            left: 20px;
            top:10px;
            z-index: 2;
        }    
<body>
    <div class="static-test"></div>
</body>

page3

relative(相对定位,相对它原本在文档流中的位置而进行的偏移,遵循正常的文档流,
但是它的top/left/right/bottom属性是生效的,可以说它是static到absolute的一个中间过渡属性,
最重要的是它还占有文档空间,而且占据的文档空间不会随 top / right / left / bottom 等属性的偏移而发生变动,
也就是说它后面的元素是依据虚线位置( top / left / right / bottom 等属性生效之前)进行的定位,这点一定要理解 )
        * {
            margin: 0;
            padding: 0;
        }
        body {
            border: 1px solid black;
       font-size: 0;/*清除行内块元素之间默认间距*/
}
        div {
             100px;
            height: 60px;
            display: inline-block;
        }
        .relative-test-first {
            background-color: red;
            position: relative;
            top: 10px;
            left: 30px;
            z-index: -1;
        }
        .relative-test-second {
            background-color: blue;
        }
<body>
    <div class="relative-test-first"></div>
    <div class="relative-test-second"></div>
</body>

page4

relative(top / right / left / bottom 属性不会对relative定位的元素所占据的文档空间产生偏移,
但是margin / padding会让该文档空间产生偏移)
        * {
            margin: 0;
            padding: 0;
        }
        body {
            border: 1px solid black;
       font-size: 0;/*清除行内块元素之间默认间距*/
}
        body>div {
             100px;
            height: 60px;
            display: inline-block;
        }
        .relative-test-first {
            background-color: red;
            position: relative;
            margin-left: 30px;
        }
        .relative-test-second {
            background-color: blue;
        }
<body>
    <div class="relative-test-first"></div>
    <div class="relative-test-second"></div>
</body>

page5

absolute(使用absolute定位的元素脱离文档流后,就只能根据祖先类元素(父类以上)进行定位,而这个祖先类还必须是定位过的(即非static方式),
举个例子,a元素使用absolute定位,它会从父类开始找起,寻找以position非static方式定位的祖先类元素,直到<html>标签为止,
这里还需要注意的是,relative和static方式在最外层时是以<body>标签为定位原点的,
而absolute方式在无父级是position非static定位时是以<html>作为原点定位。<html>和<body>元素相差9px左右。)
        html {
            border: 1px solid blue;
        }
        body {
            border: 1px solid red;
        }
        div {
             100px;
            height: 60px;
            display: inline-block;
        }
        .relative-test {
            position: relative;
            background-color: red;
            top: 10px;
            left: 30px;
        }
        .absolute-test {
            position: absolute;
            background-color: blue;
            top: 10px;
            left: 30px;
        }
<body>
    <div class="relative-test"></div>
    <div class="absolute-test"></div>
</body>

page6

absolute(absolute定位一般要加 top:0; left:0;这两个属性是完全必要的
因为我们如果使用absolute或fixed定位的话,必须指定 left、right、 top、 bottom 属性中的至少一个,
否则left/right/top/bottom属性会使用它们的默认值 auto ,
这将导致对象遵从正常的HTML布局规则,在前一个对象之后立即被呈递,简单讲就是都变成relative,会占用文档空间,这点非常重要,
很多人使用absolute定位后发现没有脱离文档流就是这个原因,这里要特别注意~~~
实验一下设置left/right/top/bottom 为auto,absolute定位会不会变成relative
如果top和bottom一同存在的话,那么只有top生效。
如果left和right一同存在的话,那么只有left生效。)
     html {
            border: 1px solid blue;
        }
        body {
            border: 1px solid red;
        }
        div {
             100px;
            height: 60px;
        }
        .relative-test {
            position: relative;
            background-color: red;
            top: 10px;
            left: 30px;
        }
        .absolute-test {
            position: absolute;
            background-color: blue;
            /* top: 10px;
            left: 30px; */
        }
<div class="relative-test"></div>
<div class="absolute-test"></div>

page7

absolute(left:0; right:0; top:0; bottom:0; margin:auto;会使child居中,如果child没有宽高,则会继承父集的宽高,适合body 内的遮罩)
         * {
            margin: 0;
            padding: 0;
        }
        body {
            border: 1px solid black;
            font-size: 0;/*清除行内块元素之间默认间距*/
        }
        .container {
             200px;
            height: 150px;
            background-color: red;
            position: relative;
            margin: auto;
        }
        .absolute-test {
             100px;
            height: 60px;
            background-color: blue;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
        }
<body>
    <div class="container">
        <div class="absolute-test"></div>
    </div>
</body>

page8

fixed(固定定位,它和absoult定位一样,都脱离了文档流,并且能够根据top、right、left、bottom属性进行定位,
但不同的是fixed是根据窗口为原点进行偏移定位的,也就是说它不会根据滚动条的滚动而进行偏移。)
        * {
            margin: 0;
            padding: 0;
        }
        body {
            height: 1000px;
            border: 1px solid black;
            font-size: 0;/*清除行内块元素之间默认间距*/
        }
        .container {
             200px;
            height: 150px;
            background-color: red;
            position: relative;
            margin: auto;
        }
        .fixed-test {
             100px;
            height: 60px;
            background-color: blue;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
        }
<body>
    <div class="container">
        <div class="fixed-test"></div>
    </div>
</body>

page9

z-index属性(又称为对象的层叠顺序,它用一个整数来定义堆叠的层次,整数值越大,则被层叠在越上面,当然这是指同级元素间的堆叠,
如果两个对象的此属性具有同样的值,那么将依据它们在HTML文档中流的顺序层叠,写在后面的将会覆盖前面的。
需要注意的是,父子关系是无法用z-index来设定上下关系的,一定是子级在上父级在下。
Note:使用static 定位或无position定位的元素z-index属性是无效的。)
工欲善其事 必先利其器
原文地址:https://www.cnblogs.com/fengyouqi/p/10967891.html