| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <!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>
- .box1{
- width: 400px;
- height: 400px;
- border:3px dashed red;
- position: relative;
- }
- .box2{
- width: 200px;
- height: 200px;
- background-color: blue;
- /* margin-top: 50px; */
- /* 相对定位 元素相对于其原来的位置进行定位 */
- /* 定位之后 可以使用四个方向属性 top bottom left right */
- position: relative;
- top:50px;
- }
- .box3{
- width: 100px;
- height: 100px;
- background-color: green;
- }
- span{
- background-color: yellow;
- /* 绝对定位 元素相对于其最近的定位祖先元素进行定位 */
- /* 满足两个条件 1 必须是祖先元素 2这个祖先元素必须定位(不能使用static) */
- position: absolute;
- /* right: 0;
- bottom: 0; */
- /* 50% 表示距离顶部50% 相较于父元素的高度 */
- top: 50%;
- /* 50% 表示距离左侧50% 相较于父元素的宽度 */
- left: 50%;
- /* margin 使用负数的时候 可以将元素向相反方向移动 */
- margin-top: -20px;
- }
- </style>
- </head>
- <body>
- <div class="box1">
- <div class="box2"></div>
- <div class="box3"></div>
- <span>这是一个span标签</span>
- </div>
- </body>
- </html>
|