1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 【css】CSS position(定位)属性

【css】CSS position(定位)属性

时间:2022-04-14 21:12:26

相关推荐

【css】CSS position(定位)属性

一、CSS position(定位)属性

absolute:生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。fixed:生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 “left”, “top”,“right” 以及 “bottom” 属性进行规定。relative:生成相对定位的元素,相对于其正常位置进行定位。因此,“left: 20px” 会向元素的 left 位置移动 20 像素。static:默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。inherit:规定应该从父元素继承 position 属性的值。

关于CSS position,来自MDN的描述:

CSS position属性用于指定一个元素在文档中的定位方式。top、right、bottom、left 属性则决定了该元素的最终位置。

什么是文档流(normal flow)

normal flow 国内多译为文档流;窗体自上而下分成一行一行,并在每行中按从左至右的顺序排放元素;每个非浮动块级元素都独占一行, 浮动元素则按规定浮在行的一端,若当前行容不下,则另起一行再浮动;内联元素也不会独占一行,几乎所有元素(包括块级,内联和列表元素)均可生成子行,用于摆放子元素;有三种情况将使得元素脱离normal flow而存在,分别是 float,absolute ,fixed,但是在IE6中浮动元素也存在于normal flow中。

二、position: static

MDN的描述:

该关键字指定元素使用正常的布局行为,即元素在文档常规流中当前的布局位置。此时 top、right、bottom、left 属性无效。

static是position的默认值

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>CSS-position-static</title><style>.container{background-color: #868686;width: 100%;height: 300px;}.content{background-color: yellow;width: 100px;height: 100px;position: static;left: 10px;/* 这个left没有起作用 */}</style></head><body><div class="container"><div class="content">static是position的默认值</div></div></body></html>

对 content 的 position 设定 static 后,left就失效了,而元素(content)就以正常的 normal flow 形式呈现。

三、position: relative

MDN的描述:

该关键字下,元素先放置在未添加定位时的位置,再在不改变页面布局的前提下调整元素位置(因此会在此元素未添加定位时所在位置留下空白)。position:relative 对 table-*-group, table-row, table-column, table-cell, table-caption 元素无效。

相对于normal flow中的原位置来定位。

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>CSS-position-relative</title><style>.container {background-color: #868686;width: 100%;height: 300px;}.content {background-color: yellow;width: 100px;height: 100px;}.content1 {background-color: red;width: 100px;height: 100px;position: relative;/* 这里使用了relative */}.content2 {background-color: black;width: 100px;height: 100px;}</style></head><body><div class="container"><div class="content"></div><div class="content1"></div><div class="content2"></div></div></body></html>

这是没有设置left、top等属性时,正常出现在normal flow中的位置。

接着添加left、top:

.content {background-color: yellow;width: 100px;height: 100px;position: relative;/* 这里使用了relative */left: 20px;top: 20px;/* 这里设置了left和top */}

可以看到,元素(content1)的位置相对于其原位置(normal flow中的正常位置)进行了移动。

四、position: absolute

MDN的描述

不为元素预留空间,通过指定元素相对于最近的非 static 定位祖先元素的偏移,来确定元素位置。绝对定位的元素可以设置外边距(margin),且不会与其他边距合并。

生成绝对定位的元素,其相对于 static 定位以外的第一个父元素进行定位,会脱离normal flow。注意:是除了static外

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>CSS-position-absolute</title><style>.container {background-color: #868686;width: 100%;height: 300px;margin-top: 50px;}.content {background-color: red;width: 100px;height: 100px;position: absolute;top: 10px;}</style></head><body><div class="container"><div class="content"></div></div></body></html>

因为 content 的父元素 container 没有设置 position,默认为 static,所以找到的第一个父元素是 body(< body></ body>),可以看成是元素(content)相对于 body 向下移动10px。

五、position: fixed

MDN的描述

不为元素预留空间,而是通过指定元素相对于屏幕视口(viewport)的位置来指定元素位置。元素的位置在屏幕滚动时不会改变。打印时,元素会出现在的每页的固定位置。fixed属性会创建新的层叠上下文。当元素祖先的 transform 属性非 none 时,容器由视口改为该祖先。

fixed相对于window固定,滚动浏览器窗口并不会使其移动,会脱离normal flow。

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>CSS-position-fixed</title><style>.container {background-color: #868686;width: 100%;height: 300px;margin-top: 50px;}.content {background-color: red;width: 100px;height: 100px;position: fixed; /* 这里使用了fixed */left: 200px}</style></head><body><div class="container"><div class="content"></div></div></body></html>

六、position: sticky

MDN的描述

盒位置根据正常流计算(这称为正常流动中的位置),然后相对于该元素在流中的 flow root(BFC)和 containing block(最近的块级祖先元素)定位。在所有情况下(即便被定位元素为 table时),该元素定位均不对后续元素造成影响。当元素 B 被粘性定位时,后续元素的位置仍按照 B 未定位时的位置来确定。position: sticky对 table元素的效果与 position: relative 相同。

因为各大浏览器对于sticky的兼容问题,而且JS也可以实现这个功能,在这里就不进行深入了,了解一下就好。

七、position: inherit

的 描述

规定应该从父元素继承 position 属性的值。

八、position 以及参数总结

position: relative;不会脱离文档流,position: fixedposition: absolute会脱离文档流position: relative; 相对于自己在文档流中的初始位置偏移定位。position: fixed; 相对于浏览器窗口定位。position: absolute; 是相对于父级非position:static浏览器定位。 如果没有任何一个父级元素是非position:static属性,则会相对于文档定位。这里它的父级元素是包含爷爷级元素、祖爷爷级元素、祖宗十八代级元素的。任意一级都可以。如果它的父级元素和爷爷级元素都是非position:static属性,则,它会选择距离最近的父元素。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。