【CSS】position(定位)属性
关于CSS position,来自MDN的描述:
CSS position属性用于指定一个元素在文档中的定位方式。top、right、bottom、left 属性则决定了该元素的最终位置。
然后来看看什么是文档流(normal flow),下面是 www.w3.org 的描述:
Normal flow
Boxes in the normal flow belong to a formatting context, which may be block or inline, but not both simultaneously. Block-level boxes participate in a block formatting context. Inline-level boxes participate in an inline formatting context.
- normal flow直译为常规流、正常流,国内不知何原因大多译为文档流;
- 窗体自上而下分成一行一行,并在每行中按从左至右的顺序排放元素;
- 每个非浮动块级元素都独占一行, 浮动元素则按规定浮在行的一端,若当前行容不下,则另起新行再浮动;
- 内联元素也不会独占一行,几乎所有元素(包括块级,内联和列表元素)均可生成子行,用于摆放子元素;
- 有三种情况将使得元素脱离normal flow而存在,分别是 float,absolute ,fixed,但是在IE6中浮动元素也存在于normal flow中。
一、position: static
MDN的描述:
该关键字指定元素使用正常的布局行为,即元素在文档常规流中当前的布局位置。此时 top、right、bottom、left 属性无效。
个人补充:static是position的默认值。

1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>CSS-position-static</title>
6 <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
7 <style>
8 .container{
9 background-color: #868686;
10 width: 100%;
11 height: 300px;
12 }
13 .content{
14 background-color: yellow;
15 width: 100px;
16 height: 100px;
17 position: static;
18 left: 10px;/* 这个left没有起作用 */
19 }
20 </style>
21 </head>
22 <body>
23 <div class="container">
24 <div class="content">
25 </div>
26 </div>
27 </body>
28 </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中的原位置来定位。

1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <title>CSS-position-relative</title>
8 <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
9 <style>
10 .container{
11 background-color: #868686;
12 width: 100%;
13 height: 300px;
14 }
15 .content_0{
16 background-color: yellow;
17 width: 100px;
18 height: 100px;
19 }
20 .content_1{
21 background-color: red;
22 width: 100px;
23 height: 100px;
24 position: relative;/* 这里使用了relative */
25 }
26 .content_2{
27 background-color: black;
28 width: 100px;
29 height: 100px;
30 }
31 </style>
32 </head>
33 <body>
34 <div class="container">
35 <div class="content_0">
36 </div>
37 <div class="content_1">
38 </div>
39 <div class="content_2">
40 </div>
41 </div>
42 </body>
43 </html>

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

1 .content_1{
2 background-color: red;
3 width: 100px;
4 height: 100px;
5 position: relative;/* 这里使用了relative */
6 left: 20px;/* 这里设置了left和top */
7 top: 20px;
8 }

可以看到,元素(content_1)的位置相对于其原位置(normal flow中的正常位置)进行了移动。
三、position: absolute
MDN的描述
不为元素预留空间,通过指定元素相对于最近的非 static 定位祖先元素的偏移,来确定元素位置。绝对定位的元素可以设置外边距(margin),且不会与其他边距合并。
个人理解:生成绝对定位的元素,其相对于 static 定位以外的第一个父元素进行定位,会脱离normal flow。注意:是除了static外

1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <title>CSS-position-static</title>
8 <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
9 <style>
10 .container{
11 background-color: #868686;
12 width: 100%;
13 height: 300px;
14 margin-top: 50px;
15 }
16 .content{
17 background-color: red;
18 width: 100px;
19 height: 100px;
20 position: absolute;
21 top: 10px;
22 }
23 </style>
24 </head>
25 <body>
26 <div class="container">
27 <div class="content">
28 </div>
29 </div>
30 </body>
31 </html>

因为 content 的父元素 container 没有设置 position,默认为 static,所以找到的第一个父元素是 body(<body></body>),可以看成是元素(content)相对于 body 向下移动10px。
四、position: fixed
MDN的描述
不为元素预留空间,而是通过指定元素相对于屏幕视口(viewport)的位置来指定元素位置。元素的位置在屏幕滚动时不会改变。打印时,元素会出现在的每页的固定位置。fixed属性会创建新的层叠上下文。当元素祖先的 transform 属性非 none 时,容器由视口改为该祖先。
个人理解:fixed相对于window固定,滚动浏览器窗口并不会使其移动,会脱离normal flow。

1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <title>CSS-position-static</title>
8 <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
9 <style>
10 .container{
11 background-color: #868686;
12 width: 100%;
13 height: 1000px;
14 }
15 .content{
16 background-color: yellow;
17 width: 100px;
18 height: 100px;
19 position: fixed;/* 这里使用了fixed */
20 }
21 </style>
22 </head>
23 <body>
24 <div class="container">
25 <div class="content">
26 </div>
27 </div>
28 </body>
29 </html>

这里就不上图了,看一下代码或者自己动手码一下就能理解。
五、position: sticky
MDN的描述
盒位置根据正常流计算(这称为正常流动中的位置),然后相对于该元素在流中的 flow root(BFC)和 containing block(最近的块级祖先元素)定位。在所有情况下(即便被定位元素为 table
时
),该元素定位均不对后续元素造成影响。当元素 B 被粘性定位时,后续元素的位置仍按照 B 未定位时的位置来确定。position: sticky对 table元素的效果与 position: relative 相同。
因为各大浏览器对于sticky的兼容问题,而且JS也可以实现这个功能,在这里就不进行深入了,了解一下就好。
六、position: inherit
w3school.com的 描述
规定应该从父元素继承 position 属性的值。
inherit 继承父元素,这个用得不多,所以也不继续深入了。
转自:https://www.cnblogs.com/guolao/p/9048308.html
【CSS】position(定位)属性的更多相关文章
- css - Position定位属性与层级关系
今天同事发现一个有意思的问题,关于position的层级关系的,他要不说我也没注意过 测试后果然有趣,有待深入研究: <!DOCTYPE html> <html> <he ...
- CSS Position 定位属性
本篇文章主要介绍元素的Position属性,此属性可以设置元素在页面的定位方式. 目录 1. 介绍 position:介绍position的值以及辅助属性. 2. position 定位方式:介绍po ...
- CSS position(定位)属性
关于CSS position,来自MDN的描述: CSS position属性用于指定一个元素在文档中的定位方式.top.right.bottom.left 属性则决定了该元素的最终位置. 然后来看看 ...
- CSS position定位属性
css中的position属性是用于设置元素位置的定位方式 它有以下几种取值: static:默认定位方式,子容器在父容器中按照默认顺序进行摆放 absolute:绝对定位,元素不占据父容器空间,相当 ...
- 教你玩转CSS Position(定位)
CSS Position(定位) position 属性指定了元素的定位类型. position 属性的五个值: static relative fixed absolute sticky 元素可以使 ...
- Css Position定位(简易版本)
准备前的知识: 定位只对块级起作用.如div,p等元素是块级元素,如果是内联元素则可以先变成块级元素,display:block即可. 开始讲解: 定位共四种:static,fixed,relativ ...
- [CSS]position定位
CSS position 属性 通过使用 position 属性,我们可以选择 4 种不同类型的定位,这会影响元素框生成的方式. position 属性值的含义: static 元素框正常生成.块级元 ...
- <转载>DIV+CSS position定位方法总结
如何学习DIV+CSS布局之position属性 如果用position来布局页面,父级元素的position属性必须为relative,而定位于父级内部某个位置的元素,最好用 absolute. 任 ...
- CSS| position定位和float浮动
对基础知识再度做个巩固和梳理. 一.position定位 (一):position的属性 1.absolute:生成绝对定位的元素,相对于最近一级定位不是static的父元素来进行定位: 2.rela ...
随机推荐
- sublime text 3 快捷操作
快捷键:1.通用 ↑↓← → 上下左右移动光标 Alt 调出菜单 Ctrl + Shift + P 调出命令板(Command Palette) Ctrl + ` 调出控制台 2.编辑 Ctrl + ...
- Vue-cli 项目设置每个页面标题
页面标题 在vue-router页面配置中添加meta的title信息,配合vue-router的beforeEach注册一个前置守卫用户获取到页面配置的title const title = '移动 ...
- Flink State 有可能代替数据库吗?
有状态的计算作为容错以及数据一致性的保证,是当今实时计算必不可少的特性之一,流行的实时计算引擎包括 Google Dataflow.Flink.Spark (Structure) Streaming. ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
- linux网络接口,struct ifreq struct ifconf结构
网络相关的ioctl请求的request参数及arg地址必须指向的数据类型如下表所示: 接口 SIOCGIFCONF SIOCSIFADDR SIOCGIFADDR SIOCSIFBRDADDR SI ...
- JS中关于数组的操作
1.如何创建数组: var arr = []; //效率更高 var arr1 = new Array(); var arr2 = new Array(5); //数组的长度为5,当参数为一个时,将会 ...
- 用DELPHI中实现RAR文件解压到指定一目录
一个RAR压缩文件,用DELPHI编的程序打开它并解压到某一目录,怎么实现的?自己搞定了例子:winrar.exe e -y C:\WINDOWS\Desktop\ghost.rar d:\ 但新的问 ...
- 前端每日实战:84# 视频演示如何用纯 CSS 创作一个极品飞车 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/MBbEMo 可交互视频 此视频是可 ...
- php理解非对称SSL加密解密、验证及签名
加密方式分为对称加密和非对称加密,对称加密只使用一个秘钥,加密和解密都使用该秘钥:非对称加密则使用一对秘钥,使用公钥加密,私钥解密. 需要通过原生的openssl_public_encrypt加密,o ...
- (转载)解决vmware上安装ubuntu不能联网的问题
在vmware中安装Ubuntu之后,我们希望基本的功能如上网.传输文件等功能都是可用的,但是经常遇到不能上网的情况.使用笔记本时,我们经常希望能通过无线网卡上网,但是在做嵌入式开发时,我们还希望虚拟 ...