A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不二晨 金牌黑马   /  2019-1-18 10:01  /  419 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

当position属性值设置为absolute时,则开启了元素的绝对定位               

绝对定位:
        1.开启绝对定位,会使元素脱离文档流
        2.开启绝对定位以后,如果不设置偏移量,则元素的位置不会发生变化
        3.绝对定位是相对于离他最近的、开启了定位的祖先元素进行定位的(一般情况,开启了子元素的绝对定位,都会同时开启父元素的相对定位)如果所有的祖先元素都没有开启定位,则会相对于浏览器窗口进行定位
        4.绝对定位会使元素提升一个层级
        5.绝对定位会改变元素的性质:
                内联元素变成块元素,块元素的宽度和高度默认都被内容撑开

<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <title>绝对定位</title>
        <style type="text/css">
                .box1{
                        width: 200px;
                        height: 200px;
                        background-color: red;
                }
                .box2{
                        width: 200px;
                        height: 200px;
                        background-color: yellow;
                        position: absolute;
                        /*left: 100px;
                        top: 100px;*/
                }
                .box3{
                        width: 300px;
                        height: 300px;
                        background-color: yellowgreen;
                }
                .box4{
                        width: 300px;
                        height: 300px;
                        background-color: orange;
                        /*开启box4的相对定位*/
                        /*position: relative;*/
                }
                .s1{
                        width: 100px;
                        height: 100px;
                        background-color: yellow;
                        /*开启绝对定位*/
                        position: absolute;
                }
        </style>
</head>
<body>
        <div class="box1"></div>
        <div class="box5">
                <div class="box4">
                        <div class="box2"></div>
                </div>
        </div>
        <div class="box3"></div>

        <span class="s1">我是一个span</span>
</body>
</html>
---------------------
【转载,仅作分享,侵删】
作者:YRyr.*
原文:https://blog.csdn.net/weixin_43152725/article/details/85275112


2 个回复

倒序浏览
奈斯,感谢分享
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马