CSS相对定位和绝对定位的区别

CSS相对定位和绝对定位的区别

区别1:相对的对象不同
  • 相对定位是相对于自己
  • 绝对定位是相对于离自己最近有定位的祖先
区别2:是否会脱离文档流
  • 相对定位不会脱离文档流,不会影响其他元素的位置
  • 绝对定位会脱离文档流,会影响其他元素的布局
代码演示

这是没有任何定位的代码

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        .main{
            position: relative;
            width: 300px;
            height: 400px;
            margin: auto;
            margin-top: 30px;
            background-color: palegoldenrod;
        }
        .box1{
            width: 200px;
            height: 100px;
            margin: auto;
            margin-top: 10px;
            background-color: palegreen;
        }
        .box2{
            width: 200px;
            height: 100px;
            margin: auto;
            margin-top: 10px;
            background-color: palevioletred;
        }
    </style>
</head>
<body>
    <div class="main">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
</body>
</html>

运行结果:
在这里插入图片描述给绿色容器加上相对定位

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        .main{
            position: relative;
            width: 300px;
            height: 400px;
            margin: auto;
            margin-top: 30px;
            background-color: palegoldenrod;
        }
        .box1{
            position: relative;
            left: 50px;
            width: 200px;
            height: 100px;
            margin: auto;
            margin-top: 10px;
            background-color: palegreen;
        }
        .box2{
            width: 200px;
            height: 100px;
            margin: auto;
            margin-top: 10px;
            background-color: palevioletred;
        }
    </style>
</head>
<body>
    <div class="main">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
</body>
</html>

在这里插入图片描述

给绿色容器加入相对定位:

<!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>
        *{
            margin: 0;
            padding: 0;
        }
        .main{
            position: relative;
            width: 300px;
            height: 400px;
            margin: auto;
            margin-top: 30px;
            background-color: palegoldenrod;
        }
        .box1{
            position: absolute;
            left: 50px;
            width: 200px;
            height: 100px;
            margin: auto;
            margin-top: 10px;
            background-color: palegreen;
        }
        .box2{
            width: 200px;
            height: 100px;
            margin: auto;
            margin-top: 10px;
            background-color: palevioletred;
        }
    </style>
</head>
<body>
    <div class="main">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
</body>
</html>

在这里插入图片描述

相关推荐

  1. CSS绝对定位百分比问题(CSS小细节)

    2024-07-10 16:58:04       14 阅读
  2. CSS中更加高级布局手段——定位绝对定位

    2024-07-10 16:58:04       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 16:58:04       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 16:58:04       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 16:58:04       4 阅读
  4. Python语言-面向对象

    2024-07-10 16:58:04       5 阅读

热门阅读

  1. 用户特征和embedding层做Concatenation

    2024-07-10 16:58:04       13 阅读
  2. opencv 设置超时时间

    2024-07-10 16:58:04       12 阅读
  3. Nginx Websocket 协议配置支持

    2024-07-10 16:58:04       10 阅读
  4. Perl语言入门到高级学习

    2024-07-10 16:58:04       10 阅读
  5. 【 HTML基础知识】

    2024-07-10 16:58:04       9 阅读
  6. Vue3框架搭建3:配置说明-prettier配置

    2024-07-10 16:58:04       9 阅读