2022年8月4日(定位模式4种、定位的叠放顺序、定位的拓展、元素的显示与隐藏)

目录

 一、定位

1.1 为什么需要定位

1.2 定位

1)定位模式

        (1)静态定位——默认定位方式,无定位的意思

        (2)相对定位——在移动时是相对于它自身原来的位置来移动(自恋型)

        (3)绝对定位——在移动时是相对于它的祖先元素的位置来移动

        (4)固定定位——是元素固定于浏览器可视区的位置

        (5)粘性定位sticky (了解)

        2)边偏移

        3)定位的总结

        4)定位的叠放顺序(z-index)

        5)定位的拓展

       (1) 让绝对定位的盒子垂直、水平居中(在浏览器可视区)

       (2) 定位的特殊特性

        6)定位的综合案例

       7) 网页布局总结

      (1) 标准流

      (2) 浮动

      (3) 定位

二、元素的显示与隐藏

2.1 display属性 ——用于设置一个元素应如何显示

2.2 visibility——用于指定一个元素应可见还是隐藏

2.3 overflow——指定了如果内容溢出一个元素的框(超过其指定高度及宽度) 时,会发生什么

2.4 对比总结

2.5 综合案例——土豆网


 一、定位

1.1 为什么需要定位

        1某个盒子可以自由地在一个盒子内移动位置,并压在其他盒子上面;

        2当滚动页面时,有的模块固定到在页面中不动;

以上效果,标准流或浮动都无法快速实现,此时需要定位来实现。

 1.2 定位

        定位:将盒子定在某一个位置(摆盒子)= 定位模式 + 边偏移

1)定位模式

(1) 静态定位——默认定位方式,无定位的意思

(2)相对定位——在移动时是相对于它自身原来的位置来移动(自恋型)

例子:给pink盒子加相对定位

<title>相对定位:相对原来的自己移动、保留原来位置、不会对后面标准流产生影响</title>
    <style>
        .box1 {
            position: relative;
            top: 100px;
            left: 100px;
            width: 200px;
            height: 200px;
            background-color: pink;
        }

        .box2 {
            width: 200px;
            height: 200px;
            background-color: deeppink;
        }
    </style>
</head>

<body>
    <div class="box1">

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

    </div>

</body>

效果

(3)绝对定位——在移动时是相对于它的祖先元素的位置来移动

例1没有父级

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>绝对定位-无父亲</title>
    <style>
        .son {
            position: absolute;
            right: 0;
            top: 0;
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="son"></div>
</body>

</html>

效果

例2父级没有定位

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>绝对定位-无父亲或者父亲无定位</title>
    <style>
        .father {
            width: 500px;
            height: 500px;
            background-color: skyblue;
        }

        .son {
            position: absolute;
            left: 0;
            bottom: 0;
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="father">
        <div class="son"></div>
    </div>

</body>

</html>

效果

例3父级有定位

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>绝对定位-父级有定位</title>
    <style>
        .yeye {
            position: relative;
            width: 800px;
            height: 800px;
            background-color: hotpink;
            padding: 50px;
        }

        .father {

            width: 500px;
            height: 500px;
            background-color: skyblue;
        }

        .son {
            position: absolute;
            left: 30px;
            bottom: 10px;
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="yeye">
        <div class="father">
            <div class="son"></div>
        </div>
    </div>


</body>

</html>

效果

※ 子绝父相

        子级是绝对定位的话,父级用相对定位。

思想:在小li里加上em-img,将em子级设为绝对定位,将li父级设为相对定位

html代码

<div class="box-bd">
            <ul class="clearfix">
                <li>
                    <em>
                        <img src="images/hot.png" alt="">
                    </em>
                    <img src="images/pic.png" alt="">
                    <h4>
                        Think PHP 5.0 博客系统实战项目演练
                    </h4>
                    <div class="info">
                        <span>高级</span> • 1125人在学习
                    </div>
                </li>

css代码: 

.box-bd ul li em {
   /* 子绝 */
    position: absolute;
    top: 4px;
    right: -4px;
}
.box-bd ul li {
    /* 父相 */
    position: relative;
    float: left;
    width: 228px;
    height: 270px;
    background-color: #fff;
    margin-right: 15px;
    margin-bottom: 15px;
   
}
.box-bd ul li > img { 
  /* 选择亲儿子,就不会把em里的图片变宽  */
    width: 100%;
}

(4)固定定位——是元素固定于浏览器可视区的位置

        主要使用场景: 可以在浏览器页面滚动时元素的位置不会改变。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天一定要早睡

你的鼓励,我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值