12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <!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>
- .box{
- width: 400px;
- height: 400px;
- border: 3px solid black;
- /* 设置为相对定位 相对于自己的位置*/
- position: relative;
- top:100px;
- left:200px;
- }
- .div1{
- width: 100px;
- height: 100px;
- background-color: red;
- /* 绝对定位的元素的位置相对于最近的已定位祖先元素 如果元素没有已定位的父元素,那么它的位置相对于<html>*/
- position: absolute; /* 设置为绝对定位 */
- /* right: 0;
- bottom: 0; */
- /* top: 100px;
- left: 50px; */
- top:50%;
- left:50%;
- margin-top:-50px;
- margin-left: -50px;
- z-index: 1; /* 设置层级 数字越大越在上面 */
- }
- /* 固定定位的元素的位置相对于浏览器窗口 */
- .div2{
- position: fixed;
- right: 50px;
- bottom: 50px;
- }
- .div3{
- width: 100px;
- height: 100px;
- background-color: blue;
- position: absolute;
- top: 200px;
- left: 200px;
- z-index: 2; /* 设置层级 数字越大越在上面 */
- }
- </style>
- </head>
- <body>
- <div class="box">
- <div class="div1"></div>
- <div class="div3"></div>
- </div>
- <div class="div2">返回顶部</div>
- </body>
- </html>
|