css:position / static默认属性 / fixed固定定位 / relative相对定位 / absolute绝对定位 / sticky粘性定位

一、默认属性static

当一个元素具有static定位时,它将按照文档的正常流程进行渲染。这意味着它将按照在HTML代码中出现的顺序进行显示,并且不会受到其他定位属性(如top、right、bottom或left)的影响。
以下是关于position: static;属性的几个关键点:
1、具有position: static;的元素是文档流的一部分,忽略其他定位属性,如top、right、bottom或left。
2、默认情况下,块级元素占据其父容器的整个宽度,并垂直堆叠。
3、另一方面,内联元素则不会创建换行符,并与其他元素在同一行内显示。
4、具有position: static;的元素不受z-index属性影响。它们的堆叠顺序由它们在文档树中的位置决定,并且无法改变。

二、相对定位relative

相对定位,元素的位置相对于它在文档中的正常位置进行调整。可以使用top、bottom、left、right属性来指定定位的偏移量。
1、具有position: relative;的元素仍然遵循文档流,并且不会脱离文档流。相对定位的元素仍然占据其原始位置,其他元素的布局不会受到影响。
2、可以使用top、right、bottom和left属性来定义相对于元素原始位置的偏移量。这些偏移量是相对于元素自身进行计算的。
3、当使用相对定位时,元素不会对其他相对定位或绝对定位的元素产生影响。其他元素的定位不会受到影响,它们仍会按照正常流程进行显示。

与position:absolute配合使用

三、绝对定位absolute

绝对定位,元素的位置相对于其最近的已经定位的父元素,如果最近的父元素没有定位则相对于文档进行定位。可以使用top、bottom、left、right属性来指定定位的偏移量。

<template>
  <div class="content">
    <div class="content-box1">normal</div>
    <div class="content-box2">absolute</div>
  </div>
</template>

<script lang="ts" setup>
</script>
<style scoped lang="less">
.content{
  background: #99FFCC;
  width: 100vw;
  height: 100vh;
  position: relative;
  &-box1{
    width: 100px;
    height: 100px;
    background: #99FF66;
  }
  &-box2{
    width: 100px;
    height: 100px;
    background: #99CCFF;
    position: absolute;
    top: 200px;
    left: 200px;
  }
}
</style>

四、固定定位fixed

固定定位,元素的位置相对于浏览器窗口进行定位,不会随页面滚动而改变。可以使用top、bottom、left、right属性来指定定位的偏移量。

<template>
  <div class="content">
    <div class="content-box1">normal</div>
    <div class="content-box2">fixed</div>
  </div>
</template>

<script lang="ts" setup>
</script>
<style scoped lang="less">
.content{
  background: #99FFCC;
  width: 100vw;
  height: 100vh;
  position: relative;
  &-box1{
    width: 100px;
    height: 100px;
    background: #99FF66;
  }
  &-box2{
    width: 100px;
    height: 100px;
    background: #99CCFF;
    position: fixed;
    top: 200px;
    left: 200px;
  }
}
</style>

五、粘性定位sticky

粘性定位,元素的定位根据滚动位置来改变。当元素滚动到指定位置时,元素将固定在屏幕上,否则会按照正常文档流进行布局。

<template>
  <div class="content">
    <div class="content-box1">normal</div>
    <div class="content-box2">sticky</div>
  </div>
</template>

<script lang="ts" setup>
</script>
<style scoped lang="less">
.content{
  background: #99FFCC;
  width: 100vw;
  height: 1000px;
  &-box1{
    width: 100px;
    height: 100px;
    background: #99FF66;
  }
  &-box2{
    width: 100px;
    height: 100px;
    background: #99CCFF;
    position: sticky;
    top: 50px;
    left: 200px;
  }
}
</style>

过程记录

position: relative; 可以在 position fixed 上边

相当于添加了背景

参考链接

position定位的属性_position: static 属性可防止 left 产生影响。 不妨尝试将 position 设-CSDN博客

黏性定位【渡一教育】_哔哩哔哩_bilibili

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里是CSS中五种常见的定位方式的介绍和用法: 1. static(静态定位):元素的位置遵循正常的文档流,不受top、bottom、left、right影响。 2. relative相对定位):元素的位置相对于它在文档流中的初始位置进行定位,可以通过top、bottom、left、right属性进行微调。 3. absolute绝对定位):元素的位置相对于最近的已定位祖先元素(position属性不为static)进行定位,如果没有已定位的祖先元素,则相对于文档的body元素进行定位。 4. fixed固定定位):元素的位置相对于浏览器窗口进行定位,不随页面滚动而改变。 5. sticky粘性定位):元素在跨越特定阈值前为相对定位,之后为固定定位。可以通过top、bottom、left、right属性进行微调。 下面是一个例子,展示了这五种定位方式的用法: ```html <!DOCTYPE html> <html> <head> <title>定位方式示例</title> <style type="text/css"> .container { height: 300px; position: relative; border: 1px solid black; } .box { width: 100px; height: 100px; background-color: red; color: white; text-align: center; line-height: 100px; font-size: 20px; } .static { position: static; } .relative { position: relative; top: 50px; left: 50px; } .absolute { position: absolute; top: 50px; left: 50px; } .fixed { position: fixed; top: 50px; left: 50px; } .sticky { position: sticky; top: 50px; } </style> </head> <body> <div class="container"> <div class="box static">static</div> <div class="box relative">relative</div> <div class="box absolute">absolute</div> <div class="box fixed">fixed</div> <div class="box sticky">sticky</div> </div> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值