定位

定位的概念:

  1. 除非专门指定,否则所有框都在普通流中定位。

  2. 也就是说,普通流中的元素的位置由元素在 HTML 中的位置决定。

  3. 定位的基本思想很简单,它允许你定义元素框相对于其正常位置应该出现的位置,或者相对于父元素甚至浏览器窗口本身的位置

  4. 通过使用 position 属性,我们可以选择 4 种不同类型的定位

    position属性是把元素放置到一个静态的、相对的、绝对的、或固定的位置中

    position属性的四个值分别对应static、relative、absolute、fixed

  5. left、right、top、bottom可以设置定位的位置

    单位为 px 或 百分比都可以

    left 和 right 同时设置时只有left有效

    top 和bottom一起设置时只有top有效

相对定位

  • position:relative;

  • 让元素相对定位:

    元素先放置在未添加定位时候的区域

    然后再不改变页面布局的情况下(

    ​ 1、其他元素没有受到任何影响

    ​ 2、自身原来的位置也保留

    ​ 3、和浮动可以一起使用

    )进行移动

  • 位置设置

    left: 设置元素 左边缘 到 原来左边缘位置 的距离

    right: 设置元素 右边缘 到 原来右边缘位置 的距离

    top: 设置元素 上边缘 到 原来上边缘位置 的距离

    bottom: 设置元素 下边缘 到 原来下边缘位置 的距离

    在相对定位中:left为正 元素向右走

    在相对定位中:top为正 元素向下走

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>相对定位</title>
    <style>
        .outer div{
            width: 200px;
            height: 200px;
        }
        .con1{
            background-color: orangered;
            position: relative;
            left: 0px;
            right: 0px;
            float: left;
        }
        .con2{
            background-color: #5df2ff;
            float: left;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="con1"></div>
        <div class="con2"></div>
    </div>
</body>
</html>

包含块:

  • 绝对定位的元素 相对于它的包含块进行定位

  • 如何确定一个元素的包含块,完全取决于它自身的position属性:

    • 如果一个元素自身的position属性是 static或者是relative:它的包含块就是离他最近的祖先元素或者是格式化上下文。

    • 如果一个元素自身的position属性是absolute,

      它的包含块就是离他最近的 拥有定位属性(值不为static)的元素

    • 如果一个元素自身的position属性是fixed

      它的包含块就是viewport(视口)

    • 补充:如果一个元素的position属性是absolute 或者是 fixed 在下边几种情况下,包含块会发生改变

      1、当祖先元素的 拥有 transform 或 perspective 属性 并且值不为none的时候 它也是被当做包含块

      2、当祖先元素 拥有 filter属性的时候(值不为none) 它也可以被当做包含块

    • 如果由内向外找不到包含块条件的元素,那么html(根元素)被称作为初始包含块

绝对定位:

  1. 不预留任何的空间(脱离页面流)
  2. 通过指定当前元素 相对于其包含块偏移的量 来确定当前元素的位置
  3. 绝对定位以后,浮动失效。margin padding仍然可以使用
    • left: 0; 让定位元素的左边距离包含块左边为0
    • top: 0;让定位元素的上边距离包含块上边为0
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>绝对定位</title>
    <style>
        .outer{
            width: 800px;
            height: 500px;
            background-color: red;
            margin: 0 auto;
            overflow: hidden;

            /*transform: rotateX(0deg);*/
            /*perspective: 300px;*/
            position: relative;
        }
        .inner{
            width: 600px;
            height: 300px;
            background-color: deeppink;
            margin: 50px;
            overflow: hidden;
            position: relative;
        }
        .con{
            width: 300px;
            height: 200px;
            background-color: greenyellow;
            margin: 30px;
            position: relative;

        }
        .box{
            width: 100px;
            height: 100px;
            background-color: yellow;
            position: absolute;

            right: 0;
            bottom: 0;
        }
    </style>
</head>
<body>
 <div class="outer">
        <div class="inner">
            <div class="con">
                con
                <div class="box"></div>
            </div>
        </div>
    </div>
</body>
</html>

固定定位

固定定位:

  • 不为元素预留空间(脱离页面流)
  • 相对于视口(viewport)的位置来定位元素的
  • 滚动页面滚动条的时候,视口不发生改变,元素位置也不会改变
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>固定定位</title>
    <style>
        .con{
            height: 4000px;
            background-color: #3c3c3c;
        }
        .left{
            position: fixed;
            width: 150px;
            height: 300px;
            left: 0;
            top: 100px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            font-size: 30px;
            color: #fff;

            animation: change .2s linear infinite;

        }
        .right{
            position: fixed;
            width: 150px;
            height: 300px;
            right: 0;
            top: 100px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            font-size: 30px;
            color: #fff;
            animation: change .2s linear infinite;
        }

        @keyframes change {
            0%{
                background-color: red;
            }
            20%{
                background-color: green;
            }
            40%{
                background-color: pink;
            }
            60%{
                background-color: yellowgreen;
            }
            80%{
                background-color: grey;
            }
            100%{
                background-color: red;
            }
        }
    </style>
</head>
<body>
    
    <div class="con"></div>
    <div class="gg">
        <div class="left">
            开局一条狗
            <br>
            装备全靠爆
        </div>
        <div class="right">
            我是砸砸辉
            <br>
            是兄弟就来
            <br>
            砍我吧
        </div>
    </div>
</body>
</html>

定位位置的设置

  • 定位位置是通过 left right top bottom来设置的

  • 值可以是

    像素

轮播图布局练习

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图片轮播布局</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .outer{
            width: 520px;
            border: 1px solid #5df2ff;
            margin: 100px auto 0;
            position: relative;
        }
        .inner{
            width: 520px;
            overflow-x: scroll;
        }
        .con{
            width: 2600px;
            overflow: hidden;
        }
        .con img{
            width: 520px;
            height: 280px;
            float: left;
        }
        .left{
            width: 30px;
            height: 30px;
            font-size: 24px;
            line-height: 30px;
            text-align: center;
            background-color: rgba(0,0,0,.5);
            color: #fff;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
        }
        .right{
            width: 30px;
            height: 30px;
            font-size: 24px;
            line-height: 30px;
            text-align: center;
            background-color: rgba(0,0,0,.5);
            color: #fff;
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
        }
        .page{
            position: absolute;
            bottom: 20px;
            left: 50%;
            /*让元素向左移动自身的一半*/
            transform: translateX(-50%);
        }
        .page li{
            width: 20px;
            height: 20px;
            background-color: rgba(255,255,255,.4);
            border-radius: 50%;
            float: left;
            margin: 3px;
        }
        .page .active{
            background-color: rgba(255,255,255,1);
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner">
            <div class="con">
                <img src="../images/01.jpg" alt="">
                <img src="../images/02.jpg" alt="">
                <img src="../images/03.jpg" alt="">
                <img src="../images/04.jpg" alt="">
                <img src="../images/05.jpg" alt="">
            </div>
            <p class="left">&lt;</p>
            <p class="right">&gt;</p>
            <ul class="page">
                <li class="active"></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>

</body>
</html>

z-index属性:

  1. 指定了一个定位属性的元素 及其后代 的层叠顺序

  2. 只有定位元素(非static值)拥有 z-index属性

  3. z-index的值没有单位 理论上来说 z-index的值大的元素 会覆盖小的元素

  4. 定位元素默认的z-index 的值 是 auto

  5. 如果一个拥有z-index属性的定位元素中 子元素也设置了z-index

    那么子元素会重新创建一个层叠上下文,子元素的z-index只能在当前的层叠上下文中对比排列

  • 元素层叠顺序:

​ z-index为负< background< border< 块级元素 <浮动元素 <内联元素 <没有设置z-index的定位元素 < z-index为正

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>z-index属性</title>
    <style>
        .outer{
            width: 500px;
            height: 300px;
            background-color: rgba(34,45,124,.5);
            position: absolute;
            z-index: 1;
        }
        .outer .con{
            width: 300px;
            height: 200px;
            background-color: rgba(111,43,21,.5);
            position: absolute;
            z-index: 1000;
        }
        .inner{
            width: 300px;
            height: 200px;
            background-color: rgba(234,124,35,1);
            position: absolute;
            top: 180px;
            z-index: 4;
        }
    </style>
</head>
<body>
<div class="outer">
        <div class="con">

        </div>
    </div>
    <div class="inner"></div>
</body>
</html>

z-index练习

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>z-index练习</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .outer{
            overflow: hidden;
            background-color: #ccc;
        }
        .outer li{
            float: left;
            width: 300px;
            height: 200px;

            margin: 100px;
            position: relative;
        }
        .outer li .con{
            width: 300px;
            height: 200px;
            background-color: pink;
            position: relative;
            z-index: 4;
        }
        .outer li .show{
            width: 300px;
            height: 50px;
            background-color: #5df2ff;
            position: absolute;
            bottom: -25px;
            right: -25px;
            z-index: 3;
        }
    </style>
</head>
<body>
    <!--
        如果想让一个元素拥有z-index属性,并且还不想影响它的其他正常布局
        我们可以给这个元素一个 position:relative;
    -->
    <ul class="outer">
        <li>
            <div class="con"></div>
            <div class="show"></div>
        </li>
        <li>
            <div class="con"></div>
            <div class="show"></div>
        </li>
        <li>
            <div class="con"></div>
            <div class="show"></div>
        </li>
        <li>
            <div class="con"></div>
            <div class="show"></div>
        </li>
        <li>
            <div class="con"></div>
            <div class="show"></div>
        </li>
        <li>
            <div class="con"></div>
            <div class="show"></div>
        </li>
    </ul>
</body>
</html>

定位练习2

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定位练习</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .outer{

        }
        .outer li{
            position: relative;
            float: left;
        }
        .outer li img{
            display: block;
            width: 313px;
            height: 220px;
        }
        .outer li p{
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 30px;
            line-height: 30px;
            text-align: center;
            color: #fff;
            /*背景半透明解决方案1:*/
            /*background-color: rgba(0,0,0,.5);*/


            /*背景半透明解决方案2:opacity*/
            /*opacity: .5;
            background-color: black;*/

            /*背景半透明解决方案3:*/
            background: url("../images/black.png") 0 0 repeat;
        }
    </style>
</head>
<body>
    <ul class="outer">
        <li>
            <img src="../images/01.jpg" alt="">
            <p>今天天气真好啊啊</p>
        </li>
    </ul>
</body>
</html>
posted @ 2023-01-09 23:10  z_bky  阅读(79)  评论(0)    收藏  举报