css 详细的position
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style type="text/css"> .box { width: 200px; height: 200px; background-color: aquamarine; } .box1 { background-color: red; } .box2 { width: 300px; background-color: blue; position: fixed; /* top: 0px; left: 200px; */ } .box3 { width: 400px; background-color: green; } .subbox { margin-top: 30px; width: 100px; height: 100px; background-color: blueviolet; } .box4 { position: relative; } .box6 { width: 50px; height: 50px; background-color: blueviolet; position: absolute; /* left: 50%; top: 50%; transform: translate(-50%, -50%); */ left: 0; right: 0; top: 0; bottom: 0; margin: auto } /* 相对定位:relative 1、开启相对定位后但不设置偏移量元素的位置不会发生变化 2、开启相对定位后,不对脱离文件流还占着原来的位置 3、开启相对定位后元素的属性不会变化 4、相对定位相对于原来的位置设置偏移量进行定位 绝对定位:absolute 1、开启绝对定位后但不设置偏移量元素的位置不会发生变化 2、开启绝对定位后,脱离了文件流不会还占着原来的位置 3、开启绝对定位后元素的属性会变化 行内元素变成块元素 4、绝对定位相对于包含块(若父(最近爷往上)元素有定位(任何定位都可以的)则以父元素为准 若所有的父级都没有定位则以html为准)的位置设置偏移量进行定位 包含块: 正常情况下:离当前元素最近的祖先块元素 绝对定位的包含块:若父(最近爷往上)元素有定位则以父元素为准 若所有的父级都没有定位则以html为准 固定定位:fixed 也是一种绝对定位 但相对于浏览器视口进行定位 */ </style> </head> <body> <!-- <div class="box box1"></div> <div class="box box2"></div> <div class="box box3"></div> <div class="box" style="overflow: hidden;"> <div class="subbox"></div> </div> <h1>------------------</h1> --> <div class="box box4"> <div class="box6"></div> </div> </body> </html>