11-CSS定位

news/2024/10/5 5:14:22

01 CSS定位概念理解

01 标准流布局概念的理解

image

02 position属性

image

02 相对定位

依然在标准流中
应用场景: 在不影响其它元素的情况下,对当前元素进行微调

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.text {/* 相对定位: 相对元素自己原来的位置 */position: relative;left: 30px;top: 50px;}</style></head><body><span>span元素</span><strong class="text">strong元素</strong><img src="../images/gouwujie01.jpg" alt=""><div>div元素</div></body>
</html>
image

2.1 案例1

image
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>span {position: relative;font-size: 10px;top: -8px;}</style>
</head>
<body><div>3<span>2</span> + 2<span>3</span> = 17</div>
</body>
</html>

2.2 案例2

梦幻西游使用背景的方法让图片的重要内容始终居中

<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>body {margin: 0;padding: 0;}.box {height: 489px;overflow: hidden;}.box img {position: relative;/* 先把图片移到最左边 */left: -960px;/* 再把图片移动包含块的一半{marigin-left的百分比是相对于包含块的} */margin-left: 50%;}</style>
</head>
<body><div class="box"><img src="./imgs/mhxy.jpg" alt=""></div>
</body>
</html>
image 但是这种方案图片向左移的距离写死了,如果图片的宽度发生了变化,还需要我们去手动修改这是不方便的 修改代码 ```htmlDocument
```

03 固定定位

脱离标准流(相对于视口即可见区域)
使用固定定位前

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><span>span元素</span><img src="./imgs/gouwujie01.jpg" alt=""><strong>strong元素</strong>
</body>
</html>
image

使用固定定位后(相当于把原来的元素拿走了,自己再决定放在哪里[用left/right/top/bottom等属性来决定])

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>strong {position: fixed;}</style>
</head>
<body><span>span元素</span><img src="./imgs/gouwujie01.jpg" alt=""><strong>strong元素</strong>
</body>
</html>
image

3.1 案例1

image
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.handle {position: fixed;right: 30px;bottom: 30px;}.handle .item {text-align: center;width: 80px;height: 40px;color: #fff;line-height: 40px;background-color: brown;border-radius: 8px;cursor: pointer;}.handle .item:hover {background-color: red;}.handle .top {margin-bottom: 5px;}</style>
</head>
<body><div class="handle"><div class="item top">顶部</div><div class="item bottom">底部</div></div>
</body>
</html>

04-绝对定位

脱离标准流(参考的是最临近的定位祖先元素如果没有找到这样的祖先元素,参考的是视口)

4.1 基本使用

image
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box .item1 {position: relative;width: 400px;height: 400px;background-color: green;}.box .item1 > .item2 {position: absolute;right: 0;bottom: 0;width: 200px;height: 200px;background-color: red;}.box .item1 > .item2 strong {position: absolute;left: 0;bottom: 0;}</style>
</head>
<body><div class="box"><div class="item1"><div class="item2"><strong>strong元素</strong></div></div></div>
</body>
</html>

05-绝对定位

脱离标准流(参考的是最临近的定位祖先元素如果没有找到这样的祖先元素,参考的是视口)

5.1 基本使用

image
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box .item1 {position: relative;width: 400px;height: 400px;background-color: green;}.box .item1 > .item2 {position: absolute;right: 0;bottom: 0;width: 200px;height: 200px;background-color: red;}.box .item1 > .item2 strong {position: absolute;left: 0;bottom: 0;}</style>
</head>
<body><div class="box"><div class="item1"><div class="item2"><strong>strong元素</strong></div></div></div>
</body>
</html>

06-position为absolute或fixed的元素的特点

image

代码案例

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {background-color: red;}/* 可以看到box这个元素直接在页面消失了,所以当设置元素为absolute它就不会向父元素汇报自己的宽度和高度了 */.box .container {position: absolute;}    </style>
</head>
<body><div class="box"><div class="container">div元素</div></div>
</body>
</html>

07-绝对定位让元素水平居中

image
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {position: relative;width: 800px;height: 300px;background-color: red;}.container {position: absolute;width: 200px;height: 100px;left: 0;right: 0;bottom: 0;margin: auto;background-color: green;}</style>
</head>
<body><!-- 水平方向的公式: red box的宽度=绿色盒子的宽度+left+right+margin left + margin right --><div class="box"><div class="container"></div></div>
</body>
</html>
image

08-绝对定位让元素垂直居中

image
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.container {position: relative;width: 800px;height: 300px;background-color: green;}.box {position: absolute;width: 200px;height: 100px;background-color: red;left: 0;right: 0;top: 0;bottom: 0;margin: auto;}</style>
</head>
<body><div class="container"><div class="box"></div></div>
</body>
</html>
image

09-粘性定位

image

代码示例
image

10-z-index

image

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/44218.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Combining Recurrent, Convolutional, and Continuous-time Models with Linear State-Space Layers

目录概符号说明LSSL和其它方法的联系代码Gu A., Johnson I., Goel K., Saab K., Dao T., Rudra A., and Re C. Combining recurrent, convolutional, and continuous-time models with linear state-space layers. NeurIPS, 2021.State space representaion-wiki.概 Mamba 系列…

堆基础知识

arenachunk通俗地说,一块由分配器分配的内存块叫做一个 chunk,包含了元数据和用户数据。具体一点,chunk 完整定义如下: struct malloc_chunk {INTERNAL_SIZE_T mchunk_prev_size; /* Size of previous chunk (if free). */INTERNAL_SIZE_T mchunk_size; …

【Azure Spring Apps】Spring App部署上云遇见 502 Bad Gateway nginx

问题描述 在部署Azure Spring App应用后,访问应用,遇见了502 Bad Gateway Nginx。问题解答 502 Bad Gateway, 并且由Nginx返回。而自己的应用中,并没有定义Nginx相关内容,所以需要查看问题是否出现在Azure Spring App服务的设置上。 根据Spring App的通信模型图判断,502的…

学生管理系统的CRUD

include using namespace std; typedef struct Studnet { //初始化结构体变量 int ID; double math_scores; double english_scores; double computer_scores; double total_scores;}Student; void Input_student_score(int size, Student* stu); //输入所有学生信息 void Out…

C语言中关于Base64编码的基础原理

Base64编码简述: 1.Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。 2.Base64,就是包括小写字母a-z、大写字母A-Z、数字0-9、符号"+"、"/"一共64个字符的字符集,(任何符号都可以转…

09-盒子模型

盒子模型01 认识盒子模型02 盒子模型的四边03 盒子边框04 盒子内边距-padding 通常用于设置边框和内容之间的间距 <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible&quo…

试了下ocr

pdf能看了,拓展的驱动下,想着是否可以ORC呢,识别到文字内容更有帮助。 按网搜的顺序,开始是用pytesseract,pip安装顺利,但运行不了,提示找不到pytesseract,按网上的帮助下载win安装包,选上中文包,再试,可以运行了,就是中文基本识别不了,也不知哪里改善,只得作罢。…

fastjson1

@目录前言分析复制文件清空文件出现问题和分析问题解决分析问题再次出现问题再次分析最终结果读取文件分析poc拓宽场景极限环境poc优化修改再次优化poc的分析写入文件SafeFileOutputStream写文件java8无依赖读文件在commons-io库下的写入文件原因利用链分析组合poc出现问题和分…