css定位及方式

在这里插入图片描述

一、定位

目标:能够说出 定位 的常见应用场景,并且能够说出 不同定位方式 的特点
在这里插入图片描述
在这里插入图片描述
定位的常见应用场景是什么?

  1. 解决盒子与盒子之间的层叠问题 → 定位之后元素层级最高,可以层叠在其他盒子上
  2. 可以让盒子始终固定在屏幕中的某个位置

在这里插入图片描述

 <style>
        *{
            margin: 0;
            padding: 0;
        }
        div {
            width: 300px;
            height: 300px;
        }
        .red{
            background-color: red;
        }
        .blue{
            /* 设置定位方式 */
            position: absolute;
            /* 设置偏移值 */
            top: 150px;
            left: 150px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="red"></div>
    <div class="blue"></div>
</body>

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

 .green{
            /* 设置定位发展 */
            position: relative;
            top: 100px;
            left: 100px;
            background-color: green;
        }

相对自己移动
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<style>
    * {
   
      margin: 0;
      padding: 0;
    }

    div {
   
      width: 300px;
      height: 300px;
    }

    .red {
   
      background-color: red;
    }

    .green {
   
      /* 1、定位方式-绝对定位 */
      position: absolute;

      /* 2、偏移值 */
      top: 100px;
      left: 100px;

      background-color: green;
    }

    .blue {
   
      background-color: blue;
    }

  </style>
</head>
<body>
  <div class="red"></div>
  <div class="green"></div>
  <div class="blue"></div>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

<style>
    .father {
   
      /* 设置定位 */
      position: relative;

      width: 600px;
      height: 600px;
      background-color: pink;
    }

    .son {
   
      /* 设置定位 */
      position: relative;

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

    .sun {
   
      /* 绝对定位 */
      position: absolute;
      right: 0;
      bottom: 0;

      width: 200px;
      height: 200px;
      background-color: blue;
    }

  </style>
</head>
<body>
  <div class="father">
    <div class="son">
      <div class="sun"></div>
    </div>
  </div>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

<style>
    .father {
   
      /* 父相 */
      position: relative;

      /* 父绝 */
      /* position: absolute; */

      width: 600px;
      height: 600px;
      background-color: pink;
    }

    .son {
   
      /* 子绝 */
      position: absolute;
      right: 0;
      bottom: 0;

      width: 400px;
      height: 400px;
      background-color: blue;
    }
  </style
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值