CSS中position属性总结

ops/2024/9/22 22:47:45/

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/ops/9105.html

相关文章

OpenStack 入门体验

目录 一、云计算概述 1.1、什么是云计算 1.2、云计算的服务模型 1&#xff09;IaaS 2&#xff09;PaaS 3&#xff09;SaaS 1.3、OpenStack 概述 1&#xff09;OpenStack 起源 2&#xff09;什么是 OpenStack 3&#xff09;OpenStack 优势 二、OpenStack 一…

基于SpringBoot的“体质测试数据分析及可视化”的设计与实现(源码+数据库+文档+PPT)

基于SpringBoot的“体质测试数据分析及可视化”的设计与实现&#xff08;源码数据库文档PPT) 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SpringBoot 工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 体质测试数据分析及可视化设计结构图…

探索CSS clip-path属性:创新设计、实用技巧与权衡考量

在网页设计的广阔天地里&#xff0c;CSS clip-path 属性犹如一把精致的剪刀&#xff0c;赋予开发者以无限创意裁剪元素形状的能力。它突破了传统矩形布局的束缚&#xff0c;让图像、文本和其他内容呈现出丰富多彩的非规则形态。然而&#xff0c;如同任何强大工具一样&#xff0…

centos开机自启的方式总结

centos管理服务的开机自启的方式有这些&#xff1a; 1. systemctl systemctl使用enable/disable来开机自启和禁用开机自启&#xff1a; systemctl enable sshd 开机自动的配置文件存储在/etc/systemd下。 2.chkconfig 这个命令是用于简化服务的管理&#xff0c;和systemct…

PS入门|蒙版到底是个什么样的功能?看完就明白了

前言 前段时间一直说蒙版蒙版什么的&#xff0c;很多小伙伴估计都听得一头雾水。 抠个图要加蒙版&#xff0c;调个色要加蒙版。 小白感觉这个蒙版就像调味剂一样&#xff0c;啥都需要加一个蒙版。 动不动就加个蒙版&#xff0c;究竟是干啥用的&#xff1f; 今天咱们就深入来…

【uniapp / vue】中动态添加绑定style 或 class

一、style样式动态设置 1.普通对象动态添加&#xff08;比较常见&#xff09; <template><view><view :style"{color:fontColor}"> </view><view :style"{ paddingTop: num px }"></view><view :style"{bac…

Leetcode 1047:删除字符串中的所有相邻重复项

给出由小写字母组成的字符串 S&#xff0c;重复项删除操作会选择两个相邻且相同的字母&#xff0c;并删除它们。 在 S 上反复执行重复项删除操作&#xff0c;直到无法继续删除。 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。 import java.util.Stack;public…

java spring security oauth2 动态 修改当前登录用户的基础信息以及权限2.0(无需重新登录)

两年前写过一篇动态修改角色和权限当时表述的不是很全面 比如如何修改其他用户权限 修改用户信息没有效果等 再写一篇 如何修改其他用户权限 不管如何改变实际需求就是获取用户token,不管是当前用户还是所有用户 当前用户的token可以通过上下文拿到,而其他用户的token该如何拿…