网站首页 > 技术文章 正文
定义
position 属性用来指定一个元素在网页上的位置,下午主要通过3个维度来讲述:基准点、是否脱标与占有位置、是否需要设置边距,一共有5种定位方式:static、relative、absolute、fixed、sticky。在讲定位之前先讲几个概念:正常模式与脱标:
正常模式:
所谓正常模式,也就是正常占有位置,不影响下一个元素的布局,也就是说没有漂浮起来。常见的有块级元素与内联元素:
- 块级元素(典型的如div),在浏览器窗口(view point)中垂直布局——每个都将显示在上一个元素下面的新行上,并且它们的外边距将分隔开它们,常见的如多个div:
// html
<body>
<div>I am div1</div>
<div>I am div2</div>
<div>I am div3</div>
</body>
// css
div {
background-color: lightgrey;
width: 100px;
height: 50px;
}
div:nth-child(2) {
background-color: yellow;
width: 100px;
height: 50px;
}
div:last-child {
background-color: red;
width: 100px;
height: 50px;
}
- 内联元素表现不一样——它们不会出现在新行上;相反,它们互相之间以及任何相邻(或被包裹)的文本内容位于同一行上,只要在父块级元素的宽度内有空间可以这样做。如果没有空间,那么溢流的文本或元素将向下移动到新行。
// html
<body>
<span>I am span1</span>
<span>I am span2</span>
<span>I am span3</span>
</body>
// css
span {
background-color: lightgrey;
width: 100px;
height: 50px;
}
span:nth-child(2) {
background-color: yellow;
width: 100px;
height: 50px;
}
span:last-child {
background-color: red;
width: 100px;
height: 50px;
}
脱标
所谓脱标,就是脱离了”标准流“(有的叫”正常流“,英文是”normal flow“),本来该占有的位置就不再占有了,下一个元素会占有它的位置,此时元素会出现重叠现象,通过设置z-index大小来显示元素重叠顺序。
static 定位
static 是浏览器的默认定位方式,如果没有给元素 style 添加 position,那么就是 static 定位。该定位的特征是: * 基准点:按代码的顺序来决定元素的布局,正常显示模式,即所谓的”标准流“。 * 边偏移:通过设置 top right bottom left 无效。 * 脱标:不脱标,正常占有该有的位置,不影响下一个元素布局。 * 使用场景:清除定位,即一个设置了非static定位的box,不要定位了,就使用static清除,在浏览器调试过程中非常重要,比如,可通过static查看原有的位置应该在哪。
// html
<body>
<div>test static position</div>
</body>
// css
div {
background-color: pink;
top: 100px;
}
relative 定位
relative 相对定位方式,该定位方式的特征是: * 基准点:自己在static定位模式下的位置作为基准点,俗称元素的默认位置。 * 边偏移:必须通过设置 top / right / bottom / left 来准确定位。 * 脱标:不脱标,正常占有该有的位置,不影响下一个元素布局,下一个元素仍然以”标准流“看待它。 * 使用场景:一个很常用的口诀”子绝父相“,如果子元素需要设置absolute定位的时候,父元素可设置relative,当然还有其他场景了,这里不一一列举。
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
// css
.father {
background-color: lightgrey;
width: 300px;
height: 200px;
}
.son {
background-color: yellow;
position: relative;
top: 20px;
width: 200px;
height: 100px;
}
absolute 定位
absolute 绝对定位方式,该定位方式的特征是: * 基准点:一般是父元素,但是前提是父元素设置了一个非非非static定位,如果父元素没有设置定位,那么就以浏览器窗口作为基点。 * 边偏移:必须通过设置 top / right / bottom / left 来准确定位。 * 脱标:完全脱标,不占有该有的位置,影响下一个元素布局,下一个元素就当该元素不存在。 * 使用场景:如果一个元素需要以父元素的(0,0)坐标点对齐的时候,可以马上想到 absolute ,还有需要转化为inline-block模式也可以使用absolute。
//html
<body>
<div class="father">
<div class="son"></div>
<div class="son2"></div>
</div>
</body>
// css
.father {
background-color: lightgrey;
width: 300px;
height: 200px;
}
.son {
background-color: yellow;
position: absolute;
top: 20px;
width: 100px;
height: 100px;
}
.son2 {
background-color: red;
top: 20px;
width: 200px;
height: 150px;
}
fixed 定位
fixed 固定定位方式,该定位方式的特征是: * 基准点:浏览器窗口为基点,不管页面怎么布局与滚动,该位置就固定不动。 * 边偏移:必须通过设置 top / right / bottom / left 来准确定位。 * 脱标:完全脱标,不占有该有的位置,影响下一个元素布局,下一个元素就当该元素不存在。 * 使用场景:比如页面可恶的广告,你怎么滑动就停在那里不动。
// html
<body>
<div class="father">
<div class="son"></div>
<div class="son2"></div>
</div>
</body>
// css
.father {
background-color: lightgrey;
width: 300px;
height: 200px;
}
.son {
background-color: yellow;
position: fixed;
right: 10px;
width: 100px;
height: 100px;
}
.son2 {
background-color: red;
top: 20px;
width: 200px;
height: 150px;
}
sticky 定位
sticky 粘性定位方式,该定位其实包含了 relative 与 fixed 这两种定位模式,但不是同时存在,需要一个触发条件,即边偏移 top / right / bottom / left 的值达到后就会切换 fixed 方式,不同定位方式,就分别显示该方式的定位特征。 * 基准点:relative 方式以自身位置为基准点; fixed 方式以浏览器窗口为基点。 * 边偏移:如果设置 top / right / bottom / left 就会同时具备relative 与 fixed 这两种定位模式,如果没设置就默认 relative, * 脱标:relative 不脱标,fixed 脱标 * 使用场景:比如页面可恶的广告,你怎么滑动就停在那里不动。
// html
<body>
<div class="father">
<div class="son"></div>
<div class="son2"></div>
</div>
</body>
// css
.father {
background-color: lightgrey;
position: relative;
left: 200px;
width: 300px;
height: 1000px;
}
.son {
background-color: yellow;
position: sticky;
top: 30px;
width: 90px;
height: 60px;
}
.son2 {
background-color: red;
top: 20px;
width: 200px;
height: 150px;
}
结语
以上就是对各种定位的解释,在实际工作中也许会很复杂,但基本都是这些定位的巧妙运用,如果讲述有什么错误,欢迎留言评论,码子码图不易,码gif图更不容易,转载请注明出处,谢谢。
猜你喜欢
- 2025-01-07 AUTOCAD——弧形文字排列
- 2025-01-07 CSS3页面布局方式详细介绍
- 2025-01-07 探索CSS position属性
- 2025-01-07 干货 | web前端入门基础知识
- 2025-01-07 前端入门——css Grid网格基础知识
- 2025-01-07 Markdown更改字体、颜色、大小,插入表格等方法
- 2025-01-07 伪元素的妙用2 - 多列均匀布局及title属性效果
- 2025-01-07 CSS-定位
- 2025-01-07 图解 CSS Grid 布局
- 2025-01-07 批处理自动生成图片自适应大小、以图片文件名为描述的图片网页
- 05-15总结雅虎前端性能优化技巧(16条)
- 05-15日常生活中吃雪莲果有养生功效也有危害
- 05-15API 安全之认证鉴权
- 05-15Chaosblade: 阿里一个超级牛逼的混沌实验实施工具
- 05-15膨来仙岛丨搞电竞的都是什么成分?
- 05-15大事全知晓!2022年新闻日历来了!
- 05-15你是有多久没看过麦田圈了?一篇文章全面回顾2015麦田圈季
- 05-15魔兽世界9.1 刻希亚寻找宝箱、稀有WA(转自nga)
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- js数组插入 (83)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)