CSS中position属性总结

news/2024/10/21 13:39:44/

CSS中position属性的总结

如果我的文章看不懂,不要犹豫,请直接看阮一峰大佬写的文章
https://www.ruanyifeng.com/blog/2019/11/css-position.html

1 干嘛用的

用来定位HTML元素位置的,通过top、bottom、right、left定位元素
分别有这些值:

  • static 默认值 就页面上正常展示逻辑
  • absolute 相对于父元素定位 这个父元素要有postion:relative 否则会向上找到html元素定位
  • fixed 相对于浏览器视口定位
  • relative 相对于这个元素本来的位置(static时的位置)定位
  • sticky 只有设置了相对位置(top、bottom、right、left)才会有效 它的效果是当视口和元素到了相对位置时,元素会黏到这个位置上不动

注:视口就是当前屏幕浏览器展示的区域

2 举个栗子

2.1 relative

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>css">body {background-color: gray;margin: 0;}.relative-class {position: relative;right: 50px;bottom: 50px;background-color: skyblue;width: 100px;height: 100px;}</style>
</head>
<body><div class="relative-class"></div>
</body>
</html>

效果:
正常情况(及默认static时)蓝色的div应该展示在红色位置上
因为配置了position:relative,right: 50px; bottom: 50px;
所以实际这个蓝色div会距离这个红色框的位置右边50px,距离这个红色框的位置底部50px
在这里插入图片描述

2.2 absolute

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>css">body {background-color: gray;margin: 0;}.absolute-father {position: relative;background-color: skyblue;width: 200px;height: 200px;}.absolute-son{/**absolute的限制条件:定位基点(一般是父元素)不能是static定位,否则定位基点就会变成整个网页的根元素html。另外,absolute定位也必须搭配top、bottom、left、right这四个属性一起使用。注意,absolute定位的元素会被"正常页面流"忽略,即在"正常页面流"中,该元素所占空间为零,周边元素不受影响。*/position: absolute;top: 100px;/* left: 100px; */background-color: orange;width: 100px;height: 100px;}.normal-son{width: 100px;height: 100px;background-color: green;}</style>
</head>
<body><div class="absolute-father"><div class="absolute-son"></div><div class="normal-son"></div></div>
</body>
</html>

效果:
红框中的蓝色div是父,黄色、绿色的div是子
父类必须有position: relative; 子类的absolute,才会依据父类定位,否则会一直向上找,直到html标签为止。
黄色div开启了absolute,top:100px,所以会相距父div上边100px的距离。
又因为absolute定位的元素会被"正常页面流"忽略,所以绿色div,占据了黄色div本来的位置。
在这里插入图片描述

2.3 fixed

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>css">body {background-color: gray;margin: 0;}.fixed {position: fixed;bottom: 0;right: 0;width: 100px;height: 100px;background-color: red;}</style>
</head>
<body><div class="fixed"></div>
</body>
</html>

效果:
弹窗小广告,一直固定在视口的指定位置上
如下图所示,鼠标上下滚动,div固定在浏览器右下角
在这里插入图片描述

2.4 sticky

主要关注class=stick那个div就行。其他div是上面的例子,为了撑页面用的

<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>css">body {background-color: gray;margin: 0;}.relative-class {position: relative;right: 70px;bottom: 70px;background-color: skyblue;width: 100px;height: 100px;}.absolute-father {position: relative;background-color: skyblue;width: 200px;height: 200px;}.absolute-son{/**absolute的限制条件:定位基点(一般是父元素)不能是static定位,否则定位基点就会变成整个网页的根元素html。另外,absolute定位也必须搭配top、bottom、left、right这四个属性一起使用。注意,absolute定位的元素会被"正常页面流"忽略,即在"正常页面流"中,该元素所占空间为零,周边元素不受影响。*/position: absolute;top: 100px;/* left: 100px; */background-color: orange;width: 100px;height: 100px;}.normal-son{width: 100px;height: 100px;background-color: green;}.fixed {position: fixed;bottom: 0;right: 0;width: 100px;height: 100px;background-color: red;}.foo {width: 100px;height: 5000px;}.sticky {position:sticky;top: 0px;width: 100px;height: 100px;background-color: gold;}</style>
</head><body><div class="relative-class"></div><hr><div class="absolute-father"><div class="absolute-son"></div><div class="normal-son"></div></div><hr><div class="fixed"></div><div class="normal-son"></div><hr><div class="sticky"></div><div class="foo"></div></body></html>

效果:
一开始金色div正常展示,当滚动条滚动到它与视口指定的css位置时(top: 0px;)就粘住不动了。滚动条滚上去,它又恢复正常展示
在这里插入图片描述
完~

觉得不错的话点个赞呗


http://www.ppmy.cn/news/1432164.html

相关文章

目标检测——行人交通信号灯数据集

一、重要性及意义 行人交通信号灯检测的重要性及意义主要体现在以下几个方面&#xff1a; 首先&#xff0c;行人交通信号灯检测对于提高道路安全性至关重要。通过准确识别交通信号灯的状态&#xff0c;行人可以更加清晰地了解何时可以安全地过马路&#xff0c;从而避免与车辆…

微信公众号开发调试

1、首先要在微信公众号开发者后台添加开发权限 2、打开微信开发者工具&#xff0c;点击微信开发者工具--更换开发模式--公众号网页调试 3、上方地址栏可输入要调试的页面地址&#xff08;必须是发布过的&#xff08;测试或生产环境&#xff09;&#xff0c;本地不可以。本地会报…

el-form表单根据条件触发校验

<template><div><el-form :model"ruleForm" :rules"rules" ref"ruleForm"><el-form-item label"活动区域" prop"resource"><el-radio-group v-model"ruleForm.resource"><el-ra…

idea 通过maven构建无法使用@SpringBootApplication

问题描述 SpringBootApplication标红&#xff0c;没有提示&#xff0c;无法启动springboot使用maven构建。通过idea的标准版本构建 原因 springboot构建启动依赖spring-boot-maven-plugin idea的标准版本没有指定构建版本&#xff0c;然后在springboot-parent里面没有指定默…

c++ primer plus(1)

前言 本次博客将要通过文字以及代码来介绍 c primer plus 没有C语言基础的放心&#xff0c;我还是会把自己当成傻子&#xff0c;一步一步的来 主要是理解这本书的第二章重点内容&#xff0c;仅仅是笔者自己的鄙见&#xff0c;有问题请指出 OK&#xff0c;开始 第二章 首…

OCP Java17 SE Developers 复习题14

答案 C. Since the question asks about putting data into a structured object, the best class would be one that deserializes the data. Therefore, ObjectInputStream is the best choice, which is option C. ObjectWriter, BufferedStream, and ObjectReader are no…

胃癌ALKBH1单基因多组学(空间转录组+单细胞+TMA验证)

目录 1&#xff0c; ALKBH1的遗传改变 2&#xff0c;ALKBH1的临床病理意义及预后价值 3&#xff0c;空间转录组分析&#xff08;重点&#xff09; 4&#xff0c;单细胞分析 5&#xff0c; ALKBH1 与TME细胞浸润相关 6&#xff0c;TMA 验证 ALKBH1与CD163 &#xff08;巨噬…

美国教授对访问学者申请的几点忠告!

大家都知道申请信对于国外访问学者来说是非常关键的申请材料之一&#xff0c;能否与心仪的导师建立联系&#xff0c;关键是看这份申请信能否得到对方的重视。但是什么样的申请信能被导师阅读&#xff0c;什么样的申请信容易被忽略呢&#xff1f; 51访学网小编访问学者咨询老师…