12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>绝对定位</title>
- <style>
- /*
- 绝对定位
- -当元素的position属性值设置为absolute时,则开启了元素的绝对定位
- -绝对定位的特点:
- 1、开启绝对定位后,如果不设置偏移量,元素的位置不会发生变化
- 2、开启绝对定位后,元素会从文档流中脱离
- 3、绝对定位会改变元素的性质,行内变成块,块的宽高被内容撑开(没有单独设置宽高的情况)
- 4、绝对定位会使元素提升一个层级
- 5、绝对定位元素是相对于其包含块进行定位的
- 包含块(containing block)
- -正常情况下:
- 包含块就是离当前元素最近的祖先块元素
- <div><div></div></div>
- 内层div最近的祖先块元素就是外层的div
- <div><span><em>hello</em></span></div>
- span的祖先块元素是外层的div,em的祖先块元素也是div
- -绝对定位的包含块
- 包含块就是离它最近的开启了定位的祖先元素
- 如果所有的祖先元素都没有开启定位,则根元素就是它的包含块
- -html(根元素,初始包含块)
- */
- body{
- font-size: 50px;
- }
- .box1{
- width: 200px;
- height: 200px;
- background-color: #bfa;
- }
- .box2{
- width: 200px;
- height: 200px;
- background-color:orange;
- position: absolute;
- left: 0;
- top: 0;
- }
- .box3{
- width: 200px;
- height: 200px;
- background-color: yellow;
- }
- .box4{
- width: 400px;
- height: 400px;
- background-color: tomato;
- position: relative;
- }
- .box5{
- width: 300px;
- height: 300px;
- background-color: aliceblue;
- /* position: relative; */
- }
- </style>
- </head>
- <body>
- <div class="box1">1</div>
- <div class="box4">
- 4
- <div class="box5">
- 5
- <div class="box2">2</div>
- </div>
- </div>
- <!-- <div class="box3">3</div> -->
- </body>
- </html>
|