css3-定位

发布于:2023-01-04 ⋅ 阅读:(154) ⋅ 点赞:(0)

目录

1.定位

1.1 相对定位

1.2 绝对定位

1.3 固定定位

1.4 z-index


1.定位

1.1 相对定位

相对定位:positon:relstive;
相对于原来的位置,进行指定的偏移,相对定位的话,它仍然在标准文档流中!原来的位置会被保留

top,right,left,bottom,是旧图相对于新图在哪个位置。因此,属性值为负的才代表往那个方向移动。

测试代码:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    相对定位:相对于自己原来的位置-->
    <style>
        body{
            padding: 20px;
        }
        div{
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
        }
        #father{
            border: 1px black solid;
            padding: 0px;
        }
        #first{
            background-color: #8EC5FC;
            border: 1px orchid dashed;
            position: relative;/*相对定位:上下左右*/
            top: -20px;
            left: 20px;
        }
        #second{
            background-color: #E0C3FC;
            border: 1px palegreen solid;
            position: relative;
            right: 20px;
        }
        #third{
            background-color: bisque;
            border: 1px orchid dashed;
            position: relative;
            bottom: -10px;
        }
    </style>
</head>
<body>
<div id="father">
    <div id="first">第一个盒子</div>
    <div id="second">第二个盒子</div>
    <div id="third">第三个盒子</div>
</div>
</body>
</html>

练习:

代码实现:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>方块定位</title>
    <style>
        #father{
            width: 300px;
            height: 300px;
            padding: 10px;
            border: 1px solid red;
        }
        a{
            width: 100px;
            height: 100px;
            text-decoration: none;
            background: pink;
            line-height: 100px;
            text-align: center;
            color: whitesmoke;
            display: block;
        }
        a:hover{
            background: paleturquoise;
        }
        .two{
           position: relative;
            left: 200px;
            top: -100px;
        }
        .four{
            position: relative;
            left: 200px;
            top:-100px
        }
        .five{
            position: relative;
            left:100px;
            top:-300px
        }
    </style>
</head>
<body>
<div id="father">
    <a href="" class="one">链接1</a>
    <a href="" class="two">链接2</a>
    <a href="" class="three">链接3</a>
    <a href="" class="four">链接4</a>
    <a href="" class="five">链接5</a>
</div>
</body>
</html>

1.2 绝对定位

定位:基于xxx定位,上下左右~

1)没有父级元素定位的前提下,相对于浏览器定位

2)假设父级元素存在定位,我们通常会相对于父级元素进行偏移

3)在父级元素范围内移动

总结:相对于父级或浏览器的位置,进行指定的偏移,绝对定位的话,它不在标准文档流中,原来的位置不会被保留

测试代码:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--    相对定位:相对于自己原来的位置-->
    <style>
        body{
            padding: 20px;
        }
        div{
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
        }
        #father{
            border: 1px black solid;
            padding: 0px;
            position: relative;
        }
        #first{
            background-color: #8EC5FC;
            border: 1px orchid dashed;
            position: relative;/*相对定位:上下左右*/
        }
        #second{
            background-color: #E0C3FC;
            border: 1px palegreen solid;
            position: absolute;
            right:100px ;
            top: -10px;
        }
        #third{
            background-color: bisque;
            border: 1px orchid dashed;
        }
    </style>
</head>
<body>
<div id="father">
    <div id="first">第一个盒子</div>
    <div id="second">第二个盒子</div>
    <div id="third">第三个盒子</div>
</div>
</body>
</html>

1.3 固定定位

position:fixed

直接在某个位置固定不动了。(例如返回顶部,在右下角,随着鼠标都不动)

测试代码:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>固定定位</title>
    <style>
        body{
            height: 1000px;
        }
        div:nth-of-type(1){
            width: 100px;
            height: 100px;
            background: #E0C3FC;
            position:absolute;
            right: 0px;
            bottom: 0px;
        }
        div:nth-of-type(2){
            width: 50px;
            height: 50px;
            background: pink;
            position: fixed;
            right: 0px;
            bottom: 0px;
        }
    </style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>

1.4 z-index

类似于图层的层级概念

图层-z-index:默认是0,最高无限~999

页面布局:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="content">
    <ul>
        <li><img src="images/a.jpeg" alt=""></li>
        <li class="tipText">学习css</li>
        <li class="tipBg"></li>
        <li>时间:2099-01-01</li>
        <li>地点:月球一号基地</li>
    </ul>
</div>

</body>
</html>

css样式:

#content{
    width: 505px;
    padding: 0px;
    margin: 0px;
    overflow: hidden;
    font-size: 12px;
    line-height: 25px;
    border: 1px black solid;
}
ul,li{
    padding: 0px;
    margin: 0px;
    list-style: none;
}
/*父级元素相对定位*/
#content ul{
    position: relative;
}
.tipText,.tipBg{
    position: absolute;
    width: 380px;
    height: 25px;
    top:258px;
}
.tipText{
    color: azure;
    z-index: 999;
}
.tipBg{
    background: #E0C3FC;
    opacity:0.3;/*背景透明度*/
    /*filter: Alpha(opacity=30);*/
}

最终效果:

本文含有隐藏内容,请 开通VIP 后查看