目录
在盒子模型中,可以用定位的手段达到修改边界的效果,更准确来说,可以达到在父子类元素、浏览器中随意定位的效果
不同于修改边界、浮动,定位手段仅仅影响当前元素,而不会排挤其它元素
(1)在元素中打开position,默认值为static,没有定位效果,而其他值才有定位效果
(2)坐标修改值有top、right、bottom、left,通常对位元素只用其一,例如left和right,因为二维坐标轴只需要两个坐标就够了
(1)开启后不设置偏移量所有元素不会发生改变
(2)不会超出文档流
(3)不会影响元素性质(块元素、行内元素仍保持属性)
(4)层级会升高(会覆盖以前的元素)
(1)概念
-相对元素(需要移动的元素)
-参照物(理解中的坐标原点)
(2)定位标准
-相对原文档流中原本的坐标进行移动(通常默认为原定元素的左上角)
(3)例子

在本例中,橙色元素本来是块元素,它本来在黄色元素下方,在相对定位中,它的坐标原点就是黄色元素的左下角
(1)超脱文档流
(2)如果不设置偏移量,则不设置偏移量的“元素位置”不会发生改变(意思就是,在绝对定位中,位置是不会变化的,但其它元素会受影响,上面是原因)
(3)层级提升
(4)元素性质会被改变(因为超脱了文本流)
绝对定位元素是根据其“包含块”进行定位的
(1)常规情况
-离当前元素最近的祖先块元素
(2)绝对定位情况
-离当前绝对定位元素最近的开启定位的祖先元素
-如果最近的都没有开启定位,则以根元素作为定位标准
(3)根元素(html,初始包含块)
有元素到达某个位置时固定的独特特点
粘滞定位特点和相对定位基本一致

这里的例子不太好看出效果,最好是自己跑代码体会一下效果
(1)唯一不同的是,固定定位永远参照于浏览器的视口定位
(2)固定定位元素不会跟随网页滚动条滚动,多用于弹窗广告
(3)但因为版本兼容性问题,不常用
是一种特殊的绝对定位,定位标准与绝对定位差不多
这里效果不太好通过截图展示,请运行代码体会
- 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>定位-相对定位title>
-
- <style>
- .box1 {
- width: 100px;
- height: 100px;
- background-color: #bfa;
- }
- .box2 {
- width: 100px;
- height: 100px;
- background-color: red;
- position: relative;
- top: -100px;
- left: 100px;
- }
- .box3 {
- width: 100px;
- height: 100px;
- background-color: yellow;
- }
- .box4 {
- width: 100px;
- height: 100px;
- background-color: orange;
- position: relative;
- top: -100px;
- left: 50px;
- }
- style>
- head>
- <body>
- <div class="box1">div>
- <div class="box2">div>
- <div class="box3">div>
- <div class="box4">div>
- body>
- html>
- 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>定位-绝对定位title>
-
- <style>
- .box1 {
- margin-top: 100px;
- width: 300px;
- height: 300px;
- background-color: #bfa;
- /* position: relative; */
- }
- .box2 {
- width: 200px;
- height: 200px;
- background-color: red;
- /* position: relative; */
- }
- .box3 {
- width: 100px;
- height: 100px;
- background-color: yellow;
- position: absolute;
- top: 100px;
- left: 100px;
- }
- .box4 {
- width: 100px;
- height: 100px;
- background-color: orange;
- }
- style>
- head>
- <body>
- <div class="box1">
- <div class="box2">
- <div class="box3">div>
- div>
- div>
- <div class="box4">div>
- body>
- html>
- 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>定位-粘滞定位title>
-
- <style>
- * {
- padding: 0px;
- margin: 0px;
- }
- body {
- height: 2000px;
- }
- .box1 {
- height: 400px;
- width: 400px;
- background-color: #bfa;
- }
- ul li {
- list-style: none;
- float: left;
- }
- ul {
- width: 492px;
- height: 48px;
- background-color: #ffe;
- margin: 40px auto;
- position: sticky;
- top: 10px;
- }
- li a {
- color: #999;
- line-height: 48px;
- display: block;
- text-decoration: none;
- padding: 0px 10px;
- }
- li a:hover {
- background-color: #444;
- color: #eee;
- }
- style>
- head>
- <body>
- <div class="box1">div>
- <ul>
- <li>
- <a href="https://www.w3school.com.cn/h.asp">HTML/CSSa>
- li>
- <li>
- <a href="https://www.w3school.com.cn/b.asp">Brower Sidea>
- li>
- <li>
- <a href="https://www.w3school.com.cn/s.asp">Sever Sidea>
- li>
- <li>
- <a href="https://www.w3school.com.cn/p.asp">Programminga>
- li>
- <li>
- <a href="https://www.w3school.com.cn/x.asp">XMLa>
- li>
- ul>
- body>
- html>
- 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>定位-固定定位title>
-
- <style>
- body {
- height: 2000px;
- }
- .box1 {
- width: 100px;
- height: 100px;
- background-color: #bfa;
- position: fixed;
- right: 0px;
- }
- .box2 {
- width: 100px;
- height: 100px;
- background-color: red;
- }
- .box3 {
- width: 100px;
- height: 100px;
- background-color: yellow;
- }
- .box4 {
- width: 100px;
- height: 100px;
- background-color: orange;
- }
- style>
- head>
- <body>
- <div class="box1">div>
- <div class="box2">div>
- <div class="box3">div>
- <div class="box4">div>
- body>
- html>