css基础之常用定位

本文深入探讨了网页布局中的定位技术,包括相对定位、绝对定位、固定定位和静态定位。详细阐述了各种定位的特点、应用场景,并通过实例演示了它们的工作原理和效果,帮助读者理解如何利用CSS定位实现复杂网页设计。
摘要由CSDN通过智能技术生成

1、何为定位

定位指的是指定一个元素在网页上的位置,网页中行内元素在一行从左往右依次排列,块级元素垂直摆放,而定位呢就可以将其位置发生变化,上下左右均可移动,还可以脱离文档流,固定某个位置。

2、定位有哪些

2.1、相对定位

特点

  1. 使用属性position:relative表示
  2. 参考元素:是自己,偏移量和最初的自己做对比
  3. 不会脱离文档流,最初的位置,不会被其他元素填充
  4. 尝尝和使用绝对定位的孩子子元素搭配使用
  5. 并且常常和属性z-index进行网页元素层级划分

案例演示1

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            padding: 0;
            margin: 0;
        }

        div {
            background-color: antiquewhite;
            width: 200px;
            height: 200px;
            position: relative;
            left: 200px;
            top: 200px;
        }
    </style>
</head>

<body>
    <div>

    </div>
</body>

</html>

效果

请添加图片描述
在这里插入图片描述
在这里插入图片描述

案例演示2 子绝父相

1、源码实现

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            padding: 0;
            margin: 0;
        }

        #father {
            background-color: antiquewhite;
            width: 200px;
            height: 200px;
            position: relative;
            /* left: 200px;
            top: 200px; */
        }

        #son {
            background-color: purple;
            width: 60px;
            height: 60px;
            position: absolute;
            left: 60px;
            top: 60px;
        }
    </style>
</head>

<body>
    <div id="father">
        <div id="son"></div>
    </div>
    <span>可能会填充的元素</span>
</body>

</html>

2、效果图
在这里插入图片描述

案例演示3 搭配z-index使用

1、源码实现

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            padding: 0;
            margin: 0;
        }

        #father {
            background-color: antiquewhite;
            width: 200px;
            height: 200px;
            position: relative;
            left: 200px;
            top: 200px;
        }
        
        #son {
            background-color: purple;
            width: 60px;
            height: 60px;
            position: absolute;
            left: 160px;
            z-index: -2;
            top: 160px;
        }
        #grandson {
            z-index: -1;
            background-color: red;
            width: 20px;
            height: 20px;
            position: absolute;
            left: 50px;
            top: 50px;
        }
    </style>
</head>

<body>
    <div id="father">
        <div id="son">

            <div id="grandson"></div>
        </div>
    </div>
    <span>可能会填充的元素</span>
</body>

</html>

2、效果图
在这里插入图片描述

特性简要说明

第二点:如图所示
在这里插入图片描述

第三点:span标签并未占据定位之后div位置
第四点:如案例演示2
第五点:如案例演示3

2.2、绝对定位

特点

  1. 元素设置样式通过position:absolute
  2. 参考对象是离自己最近的父元素
  3. 脱离文档流,原来的位置被填充,好像不存在似的
  4. 若是父元素没有设置定位的话,以HTML为参考对象
  5. 常和z-index搭配使用,提升层级

案例演示1

1、源码实现

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            padding: 0;
            margin: 0;
        }

        div {
            background-color: antiquewhite;
            width: 200px;
            height: 200px;
            position: absolute;
            left: 200px;
            top: 200px;
        }
    </style>
</head>

<body>
    <div> </div>
    <span>可能会占据定位元素的位置</span>
</body>

</html>

2、效果图

请添加图片描述

案例演示2 搭配z-index使用

1、源码实现

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            padding: 0;
            margin: 0;
        }

        #father {
            background-color: antiquewhite;
            width: 200px;
            height: 200px;
            position: absolute;
            left: 200px;
            top: 200px;
        }
        
        #son {
            background-color: purple;
            width: 60px;
            height: 60px;
            position: absolute;
            left: 160px;
            z-index: -2;
            top: 160px;
        }
        #grandson {
            z-index: -1;
            background-color: red;
            width: 20px;
            height: 20px;
            position: absolute;
            left: 50px;
            top: 50px;
        } 
    </style>
</head>

<body>
    <div id="father">
        <div id="son">
            <div id="grandson"></div>
        </div>
    </div>
    <span>可能会占据定位元素的位置</span>
</body>

</html>

2、效果图
在这里插入图片描述

2.3、固定定位

特点

  1. 元素设置样式通过position:fixed
  2. 参考对象是以浏览器左上角
  3. 脱离文档流,好似不存在
  4. 不会随滚动条滚动
  5. 常和z-index搭配使用,提升层级
  6. 和绝对定位区别:是否随着滚动条滚动而滚动

案例演示1

1、源码实现

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            padding: 0;
            margin: 0;
        }

        #father {
            background-color: antiquewhite;
            width: 200px;
            height: 200px;
            position: fixed;
            left: 200px;
            top: 200px;
        }

        #son {
            background-color: purple;
            width: 60px;
            height: 60px;
            position: fixed;
            left: 160px;
            z-index: -2;
            top: 160px;
        }

        #grandson {
            z-index: -1;
            background-color: red;
            width: 20px;
            height: 20px;
            position: fixed;
            left: 50px;
            top: 50px;
        }
    </style>
</head>



<body>
    <div id="father">
        <div id="son">
            <div id="grandson"></div>
        </div>
    </div>
    <span>可能会占据定位元素的位置</span>
</body>

</html>

2、效果图
在这里插入图片描述

简要说明

第六点:如图所示
请添加图片描述

2.4、静态定位

  • 元素默认不设置position属性就是 position:static
  • 行内元素左右排序,块级元素垂直摆放

3、仓库地址

欢迎来骚扰

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值