06 position

    技术2022-07-12  90

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> /* ****************************************************** */ /* 相对定位:position: relative; */ .a1,.a2,.a3{ width: 100px; height: 100px; } .a1{ background-color: palegreen; } .a2{ background-color: palevioletred; /* 开启相对定位 */ position: relative; top: -100px; left: 100px; } .a3{ background-color: peru; } /* ****************************************************** */ /* 绝对定位:position: absolute; 会脱离文档流,类似于float 绝对定位的元素是相当于其包含块进行定位 包含块:(containing block) 绝对定位的包含块: 离它最近的开起来定位(position)的祖先元素 如果祖先元素没有开启定位,那么根元素就是它的包含块 */ .b1,.b2,.b3{ width: 100px; height: 100px; } .b1{ background-color: palegreen; } .b2{ background-color: pink; /* b2所在的div就是b4所在的div的包含块 */ position: relative; } .b3{ background-color: peru; }.b4{ width: 50px; height: 50px; background-color: palevioletred; /* 开启绝对定位 */ position: absolute; bottom: 0; right: 0; } /* ******************************************************** */ /* 固定定位:position:fixed 将元素的position属性设置为fixed,则开启了固定定位 固定定位也是一种绝对定位,所以固定定位的大部分特点与绝对定位一样 唯一不同的是固定定位永远相对于视口定位 视口:其实就是浏览器的窗口 开启了固定定位之后,元素的位置将不会改变,即使有滑动块也不会改变,网页中的广告就是利用的固定定位 */ /* *********************************************************** */ /* 粘滞定位: /* position: sticky; */ /* 当滑动时相对于顶部20px停住 */ /* top: 20px; */ </style> </head> <body> <!-- 相对定位 position:relative --> <h2>相对定位</h2> <div class="a1">1</div> <div class="a2">2</div> <div class="a3">3</div> <br> <!-- ****************************************************** --> <!-- 绝对定位 position: --> <h2>绝对定位</h2> <div class="b1">1</div> <div class="b2">2 <div class="b4">4</div> </div> <div class="b3">3</div> <br> <!-- ****************************************************** --> <!-- 固定定位 --> <!-- ****************************************************** --> <!-- 粘滞定位 --> </body> </html>
    Processed: 0.018, SQL: 9