当前位置: 首页 > article >正文

CSS学习14[重点]--定位、边偏移、定位模式

定位

  • 前言
  • 一、定位
  • 二、定位模式
    • 1. 静态定位 static
    • 2. 相对定位 relative
    • 3. 绝对定位 absolute
    • 4. 子绝父相
    • 5. 绝对定位的盒子水平居中
  • 6. 固定定位(fixed)
  • 7. 叠放次序(z)
  • 三、四种定位总结
  • 四、定位模式转换

前言

为什么学习定位?
应用场景:图片上移动的物体、突出的部分、导航栏…

一、定位

  1. 边偏移

    top: 100px;
    bottom: ;
    left: ;
    right: ;

  2. 定位模式

    选择器{position: absolute;}
    值包含static|relative|absolute|fixed

  3. 完整定位=边偏移+定位模式

二、定位模式

1. 静态定位 static

(1)静态定位是所有元素默认的定位方式,即html的标准流。
(2)对于边偏移无效。
(3)一般用来清除定位

2. 相对定位 relative

(1)以自己的左上角为基准移动
(2)可以通过边偏移继续占有原来的位置
(3)相对定位的元素仍在原来的位置,相对定位不脱标,目的在于移动位置

<html>
<head>
    <style>
        div {
            width: 100px;
            height: 100px;
            background-color: pink;
        }
        div:nth-child(2) {
            position: relative;
            top: 10px;
            background-color: purple;
        }
    </style>
</head>
<body>
    <div></div>
    <div></div>
</body>
</html>

3. 绝对定位 absolute

绝对定位完全脱标,不占有位置

(1)父级没有定位
如果父级没有定位,则以浏览器为标准定位。

<html>
<head>
    <style>
        .father {
            width: 100px;
            height: 100px;
            background-color: pink;
            margin: 100px;
        }
        .son {
            width: 100px;
            height: 100px;
            position: absolute;
            top: 10px;
            left: 10px;
            background-color: purple;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>

(2)父级有定位
如果父级有定位,则以父级为基准。

<html>
<head>
    <style>
        .father {
            width: 100px;
            height: 100px;
            background-color: pink;
            margin: 100px;
            position: relative;
        }
        .son {
            width: 100px;
            height: 100px;
            position: absolute;
            top: 10px;
            left: 10px;
            background-color: purple;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>

4. 子绝父相

子级是绝对定位,父级只要是定位即可。
相对定位 占有位置 不脱标
绝对定位 不占有位置 脱标

<html>
<head>
    <style>
        div {
            width: 310px;
            height: 190px;
            border: 1px solid #fff;
            margin: 50px;
            padding: 10px;
        }
        .top {
            position: absolute;
            top: 0;
            left: 0; 

        }
    </style>
</head>
<body>
    <div><!--绝对定位,下面的会跑上去-->
        <img src="#.jpg" alt="">
        <img src="#.jpg" alt="" class="top">
    </div>
</body>
</html>

5. 绝对定位的盒子水平居中

(1)加了绝对定位的盒子,margin: 0 auto; 无效
(2)如何使其水平居中?

	水平:left: 50%; margin-left: -w;
	垂直:top: 50%; margin-top: -h;
<html>
<head>
    <style>
        .father {
            width: 500px;
            height: 300px;
            background-color: palegoldenrod;
            position: relative;
            margin: 0 auto;
        }
        .son {
            width: 100px;
            height: 100px;
            position: absolute;
            background-color: purple;
            margin: 0 auto; /*无效*/
            left: 250px;
            left: 50%; /*父级盒子一半*/
            margin-left: -50px;
        }
    </style>
</head>
<body>
    <div class="father"><!--绝对定位,下面的会跑上去-->
        <div class="son"></div>
    </div>
</body>
</html>

(3)淘宝图例子

<html>
<head>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
        .slider {
            width: 500px;
            height: 300px;
            background-color: palegoldenrod;
            position: relative;
            margin: 50px auto;
        }
        .arrow-l,
        .arrow-r {
            width: 20px;
            height: 30px;
            position: absolute;
            background-color: purple;
            display: block;
            top: 50%;
            margin-top: -10px;
        }
        .arrow-l {
            left: 0;
        }
        .arrow-r {
            right: 0;
        }
        /*小圆点*/
        .circle {
            width: 65px;
            height: 20px;background-color: rgba(0,0,0,0.3);
            position: absolute;
            bottom: 15px; /*靠下对齐*/
            left: 50%;
            margin-left: -32px;
            border-radius: 10px 10px;
        }
        .circle li {
            width: 10px;
            height: 10px;
            background-color: #ccc;
            float: left;
            margin: 5px 3px;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div class="slider"><!--绝对定位,下面的会跑上去-->
        <img src="#.png">
        <a href="" class="arrow-l"><img src="#.png"></a>
        <a href="" class="arrow-r"><img src="#.png"></a>
        <ul class="circle">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>
</html>

6. 固定定位(fixed)

(1)固定定位是绝对定位的特殊形式
(2)固定定位的元素和父亲没有任何关系,只认浏览器
(3)固定定位完全脱标,不占有位置,不随滚动条滚动
(4)例子

<html>
<head>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .a {
            width: 100%;
            height: 10px;
            background-color: palegoldenrod;
            position: fixed;
        }
        .box {
            width: 100px;
            height: 1000px;
            background-color: purple;
            padding-top: 10px;
        }   
    </style>
</head>
<body>
    <div class="a"></div>
    <div class="box">123</div>
</body>
</html>

7. 叠放次序(z)

叠放次序用于调整重叠定位元素的堆叠顺序,可以对定位元素应用z-index层叠等级属性,可以取值正整数、负整数、0.

<html>
<head>
    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: pink;
            position: absolute;
            top: 0;
            left: 0;
        }
        div:first-child {
            z-index: 1;
        }
        div:nth-child(2) {
            background-color: purple;
            top: 30px;
            left: 30px;
            z-index: 2;
        }
        div:nth-child(3) {
            background-color: skyblue;
            top: 60px;
            left: 60px;
        }
    </style>
</head>
<body>
    <div></div>
    <div></div>
    <div></div>
</body>
</html>

注意:

  • z-index默认属性是0,取值越大,定位元素在层叠元素中越居上
  • 如果取值相同,按照书写顺序后来居上
  • 后面的数字不能加单位
  • 只有相对定位、绝对定位、固定定位有这个属性,其余标准流、浮动、静态定位没有该属性

三、四种定位总结

定位模式
静态定位static:不脱标,正常模式
相对定位relative:不脱标,占有位置,相对自身移动
绝对定位absolute:完全脱标,不占有位置,相对于定位父级移动
固定定位fixed:完全脱标,不占有位置,相对于浏览器移动

四、定位模式转换

(1)和浮动一样,元素添加了绝对定位和固定定位后,元素模式会转换为行内块模式
(2)行内元素如果添加了绝对定位或者固定定位后,不用转换直接给高度和宽度。

<html>
<head>
    <style>
        div {
            height: 200px;
            background-color: pink;
            /*float: left; 没给盒子宽度,浮动的盒子模式转换*/
            /*position: fixed; relative不转换*/
        }
        span {
            background-color: aquamarine;
            display: block;
            width: 100px;
            height: 100px;
            float: left; /*如果盒子本身需要添加浮动、绝对定位,则不需要转换*/
        }
    </style>
</head>
<body>
    <div>12345</div>
    <span>行内元素</span>
</body>
</html>

http://www.kler.cn/news/293614.html

相关文章:

  • linux-性能优化命令
  • Css:属性选择器、关系选择器及伪元素
  • 【项目二】C++高性能服务器开发——日志系统(日志器,日志级别,日志事件)
  • 2024高教杯数学建模B题思路
  • shell编程--正则表达式
  • SAP ABAP 程序迁移工具 SAPLINK ABAP GIT
  • Oracle 19c数据库:Windows详细安装与配置指南
  • 【操作系统】进程同步之共享内存
  • [dp]答疑
  • 0.ffmpeg面向对象oopc
  • 进程间通信与管道
  • 如何在Excel中创建一个VBA宏,并设置一个按钮来执行这个宏
  • AWS账号关闭后的影响:您需要知道的一切
  • AWTK HTML View 控件更新
  • 机器学习如何用于音频分析?
  • Unity中使用四元数限制旋转
  • 在目标检测模型中使用正样本和负样本组成的损失函数。
  • 区块链技术介绍
  • 消息可靠投递
  • 数据赋能(199)——开发:数据开发管理——概述、关注焦点
  • 批量文件编码转换用python实现的utf8转gb2312,vscode设置特殊文件的默认打开编码
  • 数据赋能(198)——开发:数据应用——技术方法、主要工具
  • DAY69
  • vue , 微信小程序 , uni-app绑定变量属性
  • 【2024】MySQL库表基本操作
  • 算法:图片压缩算法【Z字行扫描】(Java实现)
  • 相亲交友系统商业开发
  • 【最新华为OD机试E卷-支持在线评测】分糖果(100分)-多语言题解-(Python/C/JavaScript/Java/Cpp)
  • 用ACF和PACF计算出一堆数据的周期个数以及周期时长,数据分析python
  • Linux系统练习笔记【完整版】