CSS基础之相对定位,绝对定位,固定定位,z-index

2017-12-26  本文已影响85人  Owen270

1.相对定位

1.1.相对定位,就是微调元素位置的。让元素相对自己原来的位置,进行位置调整。也就是说,如果一个盒子想进行位置调整,那么就要使用相对定位

a position:relative; → 必须先声明,自己要相对定位了

b left:100px; → 然后进行调整。

c top:150px; → 然后进行调整。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        div{
            width: 200px;
            height: 200px;
        }
        .box1{
            background-color: yellowgreen;
        }
        .box2{
            background-color: skyblue;
            position: relative;
            top: 150px;
            left: 100px;
        }
        .box3{
            background-color: orange;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
</html>
图片.png

1.2.不脱标,老家留坑,形影分离,相对定位不脱标,真实位置是在老家,只不过影子出去了,可以到处飘。

图片.png

1.3.相对定位的用途:相对定位有坑,所以一般不用于做“压盖”效果。页面中,效果极小。就两个作用:

a:微调元素

b:做绝对定位的参考,子绝父相

1.4.相对定位的定位值

a:可以用left、right来描述盒子右、左的移动,相当于marginLeft ,marginRight

b:可以用top、bottom来描述盒子的下、上的移动,marginTop,marginBottom

图片.png
图片.png
图片.png

2.绝对定位

2.1.绝对定位的盒子,是脱离标准文档流的。所以,所有的标准文档流的性质,绝对定位之后都不遵守了。绝对定位之后,标签就不区分所谓的行内元素、块级元素了,不需要display:block;就可以设置宽、高了:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 200px;
            height: 200px;
        }
        .box1{
            background-color: yellowgreen;
        }
        .box2{
            background-color: skyblue;
            position: absolute;
            top: 100px;
            left: 140px;
        }
        .box3{
            background-color: gold;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
</html>
图片.png

2.2.参考点,如果用top描述,那么定位参考点就是页面的左上角,而不是浏览器的左上角,如果用bottom描述,那么就是对应的页面的左下角,当页面滚动时,这个盒子显示的位置也跟者滚动。绝对定位脱标!:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        div{
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
            bottom: 100px;
            left: 100px;
        }
    </style>
</head>
<body>
    <div></div>
    <img src="images/2.jpg" alt="" />
</body>
</html>
图片.png

2.3. 以盒子为参考点

a:一个绝对定位的元素,如果父辈元素中出现了也定位了的元素,那么将以父辈这个元素,为参考点。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 400px;
            height: 400px;
            border: 10px solid red;
            margin: 100px;
            position: relative;
        }
        p{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute;
            top: 40px;
            left: 40px;
        }
    </style>
</head>
<body>
    <div>
        <p></p>
    </div>
</body>
</html>
图片.png
b: 不一定是相对定位,任何定位,都可以作为参考点,子绝父绝、子绝父相、子绝父固,都是可以给儿子定位的。但是,工程上子绝、父绝,没有一个盒子在标准流里面了,所以页面就不稳固,没有任何实战用途。工程上,“子绝父相”有意义,父亲没有脱标,儿子脱标在父亲的范围里面移动。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 400px;
            height: 400px;
            padding: 100px;
            border: 10px solid red;
            margin: 100px;
            position: relative;
        }
        .box2{
            width: 300px;
            height: 300px;
            border: 50px solid blue;
        }
        p{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute; 
            top: 40px;
            left: 40px;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2">
            <p></p>
        </div>
    </div>
</body>
</html>
图片.png

2.4.绝对定位的盒子居中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        div{
            width: 400px;
            height: 60px;
            background-color: green;
            position: absolute;
            left: 50%;
            margin-left: -200px;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>
图片.png

3.固定定位

3.1.固定定位,就是相对浏览器窗口定位。页面如何滚动,这个盒子显示的位置不变。固定定位脱标!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        p{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: fixed;
            top: 100px;
            left: 100px;
        }
    </style>
</head>
<body>
    <p></p>
    <img src="images/2.jpg" alt="" />
</body>
</html>
图片.png
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        body{
            /*为什么要写这个?*/
            /*不希望我们的页面被nav挡住*/
            padding-top: 60px;
            /*IE6不兼容固定定位,所以这个padding没有什么用,就去掉就行了*/
            _padding-top:0;
        }
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 60px;
            background-color: #333;
            z-index: 99999999;
        }
        .inner_c{
            width: 1000px;
            height: 60px;
            margin: 0 auto;

        }
        .inner_c ul{
            list-style: none;
        }
        .inner_c ul li{
            float: left;
            width: 100px;
            height: 60px;
            text-align: center;
            line-height: 60px;
        }
        .inner_c ul li a{
            display: block;
            width: 100px;
            height: 60px;
            color:white;
            text-decoration: none;
        }
        .inner_c ul li a:hover{
            background-color: gold;
        }
        p{
            font-size: 30px;
        }
        .btn{
            display: block;
            width: 120px;
            height: 30px;
            background-color: orange;
            position: relative;
            top: 2px;
            left: 1px;
        }
    </style>
</head>
<body>
    <div class="nav">
        <div class="inner_c">
            <ul>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
                <li><a href="#">网页栏目</a></li>
            </ul>
        </div>
    </div>
    
    <img src="images/2.jpg" alt="" />

    <p>
        <a href="" class="btn">按钮</a>
    </p>
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
    <img src="images/2.jpg" alt="" />
 
     
</body>
</html>
图片.png

4.z-index值

● z-index值表示谁压着谁。数值大的压盖住数值小的。
● 只有定位了的元素,才能有z-index值。也就是说,不管相对定位、绝对定位、固定定位,都可以使用z-index值。而浮动的东西不能用。
● z-index值没有单位,就是一个正整数。默认的z-index值是0。
● 如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。定位了的元素,永远能够压住没有定位的元素。
● 从父现象:父亲怂了,儿子再牛逼也没用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 200px;
            height: 200px;
            background: yellowgreen;
            position: absolute;
            top: 100px;
            left: 100px;
            z-index: 444;
        }
        .box2{
            width: 200px;
            height: 200px;
            background: skyblue;
            position: absolute;
            top: 180px;
            left: 180px;
            z-index: 333;
        }
    </style>
</head>
<body>
    <div class="box1">绿</div>
    <div class="box2">蓝</div>
</body>
</html>

图片.png

如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面能压住别人。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            width: 200px;
            height: 200px;
            background: yellowgreen;
        }
        .box2{
            width: 200px;
            height: 200px;
            background: skyblue;
            position: relative;
            top: 100px;
            left: 30px;
        }
        .box3{
            width: 200px;
            height: 200px;
            background: pink;
            /*为了z-index值生效,必须加上一个定位:*/
            position: relative;
            top: 0;
            left: 0;
            z-index: 999;
        }
    </style>
</head>
<body>
    <div class="box1">绿</div>
    <div class="box2">蓝</div>
    <div class="box3">粉</div>
</body>
</html>
图片.png

从父现象:父亲怂了,儿子再牛逼也没用。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        .linzhiying{
            width: 200px;
            height: 200px;
            background-color: blue;
            position: relative;
            z-index: 10;
        }
        .tianliang{
            width: 200px;
            height: 200px;
            background-color: orange;
            position: relative;
            z-index: 9;
        }
        .kimi{
            width: 60px;
            height: 60px;
            background-color: green;
            position: absolute;
            top: 300px;
            left: 450px;
            z-index: 454;
        }
        .cindy{
            width: 60px;
            height: 60px;
            background-color: pink;
            position: absolute;
            top: 130px;
            left: 490px;
            z-index: 45454;
        }
    </style>
</head>
<body>
    <div class="linzhiying">
        <p class="kimi"></p>
    </div>
    <div class="tianliang">
        <p class="cindy"></p>
    </div>
</body>
</html>
图片.png
上一篇 下一篇

猜你喜欢

热点阅读