• relative相对定位

将定位元素偏离正常文档流,但占用正常文档流的位置。如下图中:

1.relative相对定位是相对于自身的定位,relative元素不加position定位,位置是在虚框位置。

relative元素加了position: relative;将根据top/bottom/left/right,基于虚框的margin左上外角定位的。

2.虚框为relative元素占用的正常文档流的位置,所以正常元素到下面了。

<style>* {padding: 0;margin: 0;}#sub1 {height: 100px;width: 100px;position: relative;top: 100px;left: 100px;background-color: red;}#sub2 {height: 100px;width: 100px;
            background-color: green;}</style><body style="border:1px black solid"><div id="sub1">relative元素</div><div id="sub2">正常元素</div>
</body>

  • absolute绝对定位

将定位元素偏离正常文档流,不占用正常文档流的位置。如下图中:

  <style>* {padding: 0;margin: 0;}#sub1 {height: 100px;width: 100px;position: absolute;/* margin: 10px;border: 10px;padding: 10px; */top: 100px;left: 100px;background-color: red;}#sub2 {height: 100px;width: 100px;/* position: relative;top: 100px;left: 100px; */background-color: green;}</style>
<body style="border:1px black solid"><div id="sub1">absolute元素</div><div id="sub2">正常元素</div>
</body>

1.absolute元素的父元素没有position定位,absolute元素相对于body定位

<style>* {padding: 0;margin: 0;}#parent {height: 100px;width: 100px;background-color: yellow;/* position: absolute; */margin: 100px;padding: 100px;}#child {height: 100px;width: 100px;position: absolute;/* margin: 10px;border: 10px;padding: 10px; */top: 100px;left: 100px;background-color: red;}</style>
<body style="border:1px black solid"><div id="parent">父元素<div id="child">absolute元素</div></div>
</body>

2.absolute元素的父元素有position定位(absolute或relative),absolute元素相对于父元素的padding左上外角进行定位

 <style>* {padding: 0;margin: 0;}#parent {height: 100px;width: 100px;background-color: yellow;position: absolute;margin: 100px;padding: 100px;}#child {height: 100px;width: 100px;position: absolute;/*或者positi:relative*//* margin: 10px;border: 10px;padding: 10px; */top: 100px;left: 100px;background-color: red;}</style>
<body style="border:1px black solid"><div id="parent">父元素<div id="child">absolute元素</div></div>
</body>

  • fixed固定定位

脱离文档流,不占用正常文档流,相对于body定位

转载于:https://www.cnblogs.com/chrisghb8812/p/9909668.html

css position relative absolute fixed相关推荐

  1. html中的绝对定位怎么写,CSS position绝对定位absolute relative

    DIV CSS position绝对定位absolute relative教程篇 常常使用position用于层的绝对定位,比如我们让一个层位于一个层内具体什么位置,为即可使用position:abs ...

  2. html本文框百分比定位,HTML中三种定位relative,absolute,fixed后,盒子的百分比宽度及位置易错点...

    1 . 相对定位relative:顾名思义,相对定位是相对于自己的位置来进行偏移,如下图: 以盒子中心为基准,为每条边的正方向,例: 向右移动20px : 代码为left:20px;或者right:- ...

  3. CSS中position属性(relative,absolute,fixed)和display中的inline-block

    前言 平时很少写CSS,用的多也就是Bootstrap来堆样式,写起来快一点,但是有时候需要自己写,很久没写容易忘记,这里记录下这两个常用属性position和display 这里贴一条CSDN设置图 ...

  4. CSS定位总结:position=static/relative/absolute/fixed时的区别、top/bottom/left/right与margin外边距的运用

    准备布局: <!DOCTYPE html> <html> <head> <style> *{ margin:0; padding:0; } .base- ...

  5. CSS position属性absolute relative等五个值的解释

    目前几乎所有主流的浏览器都支持position属性("inherit"除外,"inherit"不支持所有包括IE8和之前版本IE浏览器,IE9.IE10还没测试 ...

  6. CSS position绝对定位absolute relative

    常常使用position用于层的绝对定位,比如我们让一个层位于一个层内具体什么位置,为即可使用position:absolute和position:relative实现. 一.position语法与结 ...

  7. CSS:position:relative;的认识

    CSS定位的原理 把浏览器窗口想象成一个坐标系统: CSS定位的原理是:你可以将任何盒子(box)放置在坐标系统的任何位置上. 采用CSS定位技术来放置元素是非常精确的.相对于使用表格.透明图像或其他 ...

  8. HTML中三种定位relative,absolute,fixed后,盒子的百分比宽度及位置易错点

    1 . 相对定位relative:顾名思义,相对定位是相对于自己的位置来进行偏移,如下图: 以盒子中心为基准,为每条边的正方向,例: 向右移动20px : 代码为left:20px;或者right:- ...

  9. html绝对定位向下,HTML中三种定位relative,absolute,fixed使用后出现的一些问题

    1 . 相对定位relative:顾名思义,相对定位是相对于自己的位置来进行偏移,如下图: 以盒子中心为基准,为每条边的正方向,例: 向右移动20px : 代码为left:20px;或者right:- ...

  10. CSS的定位 position属性的 absolute relative static fixed的区别及用法

    CSS中position一般是跟盒子结合做出效果的,浮动可以实现盒子在横向上的排列,但是如何实现在盒子上重叠另一个盒子的效果呢?就需要CSS的position来实现 position属性有四个,分别有 ...

最新文章

  1. VS2005 常用快捷键
  2. 吐血整理:手拿几个大厂 Offer 的秘密武器!
  3. JAVA调用R语言之Rserve
  4. cf-Global Round2-C. Ramesses and Corner Inversion(思维)
  5. 不可不知的:iOS开发的22个诡异技巧
  6. ML.NET 推荐引擎中一类矩阵因子分解的缺陷
  7. Visual Studio 2017 15.8 正式发布,测试速度提高 82%
  8. Job for slapd.service failed because the control process exited with error code. See systemctl stat
  9. 跨平台的PHP+MySQL_跨平台的PHP+MySQL
  10. java outputstrea_java的InputStream和OutputStream的理解【转】
  11. windows下安装wget
  12. maxscale mysql_安装maxscale MySql读写分离
  13. 怎么看电脑支持多少兆网速_电脑网速怎么看(电脑怎么看网速多少兆)
  14. c语言快捷键的使用方法,电脑快捷键的使用方法
  15. 陶哲轩(Terence Tao)对从事数学职业的建议
  16. 苹果电脑python制作圣诞树的教程
  17. 双目立体匹配修炼之路
  18. PCI-PCIE中断机制之一
  19. USB TYPE -A -B -C 接口
  20. mysql inner join 链接三个或多个表

热门文章

  1. Spring读书笔记(一)
  2. webStrom 开始你的第一个React应用
  3. (已拿offer)2017腾讯暑期实习生从笔试到面试总结(附带华为、阿里面试经历)...
  4. many to many mysql_mysql “Too many connections” 解决办法
  5. python代码怎么运行_如何让Python代码加速运行?
  6. python变量存为matlab,将matlab变量导出为python用法的文本
  7. java案例代码2-素数判断测试
  8. 帆软FineMobile 自适应
  9. esxi 需要整合 空间不足_太炫酷了!10月微信新花样!微信情侣空间怎么设置如何弄微信情侣空间在哪里开...
  10. html单元格选中状态,UITableViewCell 设置单元格选中后只显示一个打勾的状态