CSS学习笔记-06-定位

笔记来源:
【狂神说Java】CSS3最新教程快速入门通俗易懂

6、定位


6.1、相对定位

  • 相对定位:

    • 当元素的position属性值设置为relative时则开启了元素的相对定位
    • 相对定位的特点:
      • 1、元素开启相对定位后,如果不设置偏移量元素不会发生任何的变化
      • 2、相对定位是参照于元素在文档流中的位置进行定位的
      • 3、相对定位会提升元素的层级
      • 4、相对定位不会使元素脱离文档流
      • 5、相对定位不会改变元素的性质快还是块,行内还是行内
  • 偏移量

    • 当元素开启的定位以后,可以通过偏移量来设置元素的位置
    • top:定位元素和定位位置上边的距离
    • bottom:定位元素和定位位置下边的距离
    • 通常top和bottom只用其中之一
    • left:定位元素和定位位置左边的距离
    • right:定位元素和定位位置右边的距离
    • 通常left和right只用其中之一
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    相对定位
        想对于自己原来的位置进行偏移
-->
    <style>
        div{
      
      
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
        }
        #father{
      
      
            border: 1px solid #666;
            padding: 0px;
        }
        #first{
      
      
            border: 1px dashed #b61d1d;
            background: #14f1b6;
            position: relative;/*相对定位  上下左右*/
            top: -20px;
            left: 20px;
        }
        #second{
      
      
            border: 1px dashed #1b3f81;
            background: #c03ee5;
        }
        #third{
      
      
            border: 1px dashed #14f1b6;
            background: #34ea19;
            position: relative;
            bottom: -10px;
            right: 20px;
        }
    </style>
</head>
<body>
<div id="father">
    <div id="first">第一个盒子</div>
    <div id="second">第二个盒子</div>
    <div id="third">第三个盒子</div>

</div>
</body>
</html>

相对定位:position:relative

相对于原来的位置进行指定的偏移,相对定位的话,它仍然在标准文档流中,原来的位置会被保留

		   bottom: -10px;
            right: 20px;
            top: -20px;
            left: 20px;		

6.2、绝对定位

绝对定位:

  • 当元素的position属性值设置为absolute时,则开启了元素的绝对定位

  • 绝对定位的特点

    • 1、开启绝对定位后,如果不设置偏移量,==元素的位置==不会发生变化
    • 2、开启绝对定位后,元素会从文档流中脱离
    • 3、绝对定位会改变元素的性质,行内变成块,块的宽高会被内容撑开
    • 4、绝对定位会使元素提升一个层级
    • 5、绝对定位元素是相对于其包含块进行定位的
  • 包含块(containing block)

    • 正常情况下:

      包含块就是离当前元素最近的祖先块元素

    • 绝对定位的包含块:

      包含块就是离他最近的开启了定位的祖先元素,如果所有的祖先元素都没有开启定位则根元素就是它的包含块

    • html(根元素、初始包含块)

原先水平布局的公式为:

​ margin-left + border-left + padding-left +width +padding-right + border-right +margin-right

  • 当我们开启了绝对定位后:

    水平方向的布局等式就需要添加left和right两个值(left + margin-left + border-left + padding-left +width +padding-right + border-right +margin-right +right

    • 当发生过度约束时
      • 如果9个值中没有auto则自动调整right值以使等式满足
      • 如果有auto,则自动调整auto的值使等式满足
    • 可设置auto的值:margin width left right
    • 因为left和right的值默认是auto,所以如果不知道left和right,则不等式不满足时,会自动调整这两个值

垂直方向布局的等式也必须要满足

top + margin-top + border-top + padding-ltop +width +padding-bottom + border-bottom +margin-bottom +bottom

//定位:基于xxx定位,上下左右

1、没有父级元素定位的前提下,相对于浏览器定位

2、假设父级元素存在定位,我们通常会相对于父级元素进行偏移~

3、在父级元素范围内移动

相对于父级或浏览器的位置,进行指定的偏移,绝对定位的话,它不在标准文档流中,原来的位置不会被保留


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div{
      
      
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
        }
        #father{
      
      
            border: 1px solid #666;
            padding: 0px;
            position: relative;
        }
        #first{
      
      
            border: 1px dashed #b61d1d;
            background: #14f1b6;
        }
        #second{
      
      
            border: 1px dashed #1b3f81;
            background: #c03ee5;
            position: absolute;
            left: 20px;
        }
        #third{
      
      
            border: 1px dashed #14f1b6;
            background: #34ea19;

        }
    </style>
</head>
<body>
<div id="father">
    <div id="first">第一个盒子</div>
    <div id="second">第二个盒子</div>
    <div id="third">第三个盒子</div>

</div>
</body>
</html>

在这里插入图片描述

6.3、固定定位 fixed

固定定位:

  • 当元素的position属性值设置为fixed时,则开启了元素的固定定位
  • 固定定位也是一种绝对定位,所以固定定位的大部分特点都和绝对定位一样,唯一不同的是固定定位永远参照于浏览器的视口进行定位,固定定位的元素不会随网页的滚动条滚动
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
      
      
            height: 1000px;
        }
        div:nth-of-type(1){
      
      /*绝对定位:相对于浏览器*/
            width: 100px;
            height: 100px;
            background: #14f1b6;
            position: absolute;
            right: 0;
            bottom: 0;
        }
        div:nth-of-type(2){
      
      /*fixed,固定定位*/
            width: 50px;
            height: 50px;
            background: #7a08e3;
            position: fixed;
            right: 0;
            bottom: 0;
        }
    </style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>

6.4、粘滞定位

粘滞定位:

  • 当元素的position属性值设置为sticky时,则开启了元素的粘滞定位,不同的是粘滞定位可以在元素到达某个位置时将其固定

6.5、元素的层级

  • 对于开启了定位元素,可以通过z-index属性来指定元素的层级
    • z-index需要一个整数作为参数,值越大的元素层级越高,元素的层级越高越优先显示
    • 如果元素的层级一样,则优先显示靠下的元素
    • 祖先的元素层级再高也不会盖住后代元素
.box1{
    
    
    width:20px;
    height:20px;
    position:absolute;
    z-index:3;
}

猜你喜欢

转载自blog.csdn.net/weixin_53249168/article/details/128159380