2025.4.16 学习日记 CSS定位

CSS 定位是用于控制元素在页面上的位置和布局的重要机制。以下我将列出position属性的不同值,以及topleftrightbottomz-index属性,并给出相应的示例代码。

1. position: static(默认)

staticposition属性的默认值,元素按照正常的文档流进行布局,topleftrightbottomz-index属性对其无效。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Static Positioning</title>
    <style>
        .static-box {
            background-color: lightblue;
            width: 100px;
            height: 100px;
            position: static;
            top: 50px; /* 无效 */
            left: 50px; /* 无效 */
        }
    </style>
</head>

<body>
    <div class="static-box"></div>
</body>

</html>

2. position: relative(绝对定位)

relative定位的元素会相对于其正常位置进行偏移。topleftrightbottom属性可以用来指定偏移量,并且元素仍然会占据原来的空间。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Relative Positioning</title>
    <style>
        .relative-box {
            background-color: lightgreen;
            width: 100px;
            height: 100px;
            position: relative;
            top: 20px;
            left: 20px;
        }
    </style>
</head>

<body>
    <div class="relative-box"></div>
</body>

</html>

3. position: absolute(相对定位)

absolute定位的元素会相对于最近的已定位祖先元素(即position值不为static的祖先元素)进行定位。如果没有已定位的祖先元素,则相对于<html>元素。元素脱离正常文档流,不再占据原来的空间。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Absolute Positioning</title>
    <style>
        .parent {
            position: relative;
            width: 200px;
            height: 200px;
            background-color: lightgray;
        }

        .absolute-box {
            background-color: lightcoral;
            width: 50px;
            height: 50px;
            position: absolute;
            top: 20px;
            right: 20px;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="absolute-box"></div>
    </div>
</body>

</html>

4. position: fixed(固定定位)

fixed定位的元素会相对于浏览器窗口进行定位,无论页面如何滚动,元素的位置都不会改变。元素脱离正常文档流。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fixed Positioning</title>
    <style>
        .fixed-box {
            background-color: lightyellow;
            width: 100px;
            height: 100px;
            position: fixed;
            bottom: 20px;
            right: 20px;
        }

        p {
            height: 2000px;
        }
    </style>
</head>

<body>
    <div class="fixed-box"></div>
    <p>滚动页面查看效果</p>
</body>

</html>

5. position: sticky(粘性定位)

sticky定位的元素会在正常文档流中滚动,直到达到指定的阈值(由topleftrightbottom属性指定),然后固定在该位置。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sticky Positioning</title>
    <style>
        .sticky-box {
            background-color: lightpink;
            width: 100%;
            height: 50px;
            position: sticky;
            top: 0;
        }

        p {
            height: 2000px;
        }
    </style>
</head>

<body>
    <div class="sticky-box"></div>
    <p>滚动页面查看效果</p>
</body>

</html>

6. z-index属性

z-index属性用于控制元素的堆叠顺序,值越大的元素会显示在值较小的元素之上。只有定位元素(position值不为static)的z-index属性才有效。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Z-index Example</title>
    <style>
        .box1 {
            background-color: lightblue;
            width: 100px;
            height: 100px;
            position: relative;
            top: 20px;
            left: 20px;
            z-index: 1;
        }

        .box2 {
            background-color: lightgreen;
            width: 100px;
            height: 100px;
            position: relative;
            top: -20px;
            left: 40px;
            z-index: 2;
        }
    </style>
</head>

<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值