css入门8:定位

本文通过实例展示了如何使用CSS的各种定位方式,包括固定定位、相对定位、绝对定位和粘性定位,并解释了如何设置top、bottom、left、right和z-index等属性来实现元素的精确布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、指定position属性(static/fixed/relative/absolute/sticky)

2、指定top,button,left,right属性

3、指定overflow属性

4、指定堆叠顺序z-index

<html>

<head>
  <style>
    p.pos_fix {
      position: fixed;
      top: 30px;
      right: 5px;
    }


    p.pos_rel {
      position: relative;
      left: 20px;
      top: -40px;
      color: red;

    }

    p.pos_abs {
      position: absolute;
      left: 100px;
      top: 150px;
    }

    div.pos_sticky {
      position: sticky;
      top: 0;
      padding: 5px;
      background-color: lightgrey;
      border: 2px solid red;
    }

    img{
      position:absolute;
      left:0px;
      right:0px;
      z-index: -1;

    }

  </style>



</head>


<body>

  <p class="pos_fix">
    this is position fixed
  </p>


  <p>
    this is position relative
  </p>
  <p class="pos_rel">
    this is position relative
  </p>


  <p class="pos_abs">
    this is position absolute
  </p>


  <div class="pos_sticky">
    this is position stick
  </div>


<img src="my_logo.png"/>

  <div style="height:2000px;background-color:grey;">
    需要滚动才能体现出效果。
  </div>






</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值