一、相对定位

二、绝对定位

三、固定定位

四、z-index

前言

  定位有三种:1、相对定位  2、绝对定位  3、固定定位

  这三种定位,每种都暗藏玄机,所以要每个单独剖析。

1️⃣   相对定位

  1、三大特性  

  1. 1、不脱标 (遵循标准流)
  2. 2、形影分离
  3. 3、老家留坑,占着茅坑不拉屎,很恶心

  4. 所以说相对定位没什么太大的用处,还影响页面布局。不建议使用相对定位来做压盖效果

  2、好处 

  1. 微调元素信息
  2. 做绝对定位的参考(父相子地)

  3、示例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>相对定位</title>
  6. <style type="text/css">
  7. *{
  8. padding:0;
  9. margin:0;
  10. }
  11. .box1{
  12. width:300px;
  13. height: 50px;
  14. background-color: dodgerblue;
  15. /*如果仅对当前元素设置相对定位,那么与标准流下的盒子没有什么区别*/
  16. /*position: relative;*/
  17. /*!*设置相对定位,我们可以使用四个方向的属性 top left right bottom*/
  18. /*相对定位:相对于自己原本的本身定位 top:30px;*/
  19. /*那么盒子相对原来的位置向下移动30px*!*/
  20. /*top:30px;*/
  21. /*left:30px;*/
  22. }
  23. .box2{
  24. width:300px;
  25. height: 200px;
  26. margin:100px;
  27. }
  28. .user{
  29. font-size: 20px;
  30. }
  31.  
  32. .btn{
  33. position: relative;
  34. top: 1px;
  35. left:-5px;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div class="box1"></div>
  41.  
  42. <div class="box2">
  43. <input type="text" name="username" class="user">
  44. <input type="button" name="" value="点我" class="btn">
  45. </div>
  46. <div class="box3"></div>
  47. </body>
  48. </html>

2️⃣  绝对定位

  1、特性: 

  1. 脱标(脱离标准流),
  2. 做遮盖效果,
  3. 设置绝对定位后,不区分行内元素和块状元素,都能设置宽高(相比设display:block,设置绝对定位会脱标)

   示例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>绝对定位</title>
  6. <style type="text/css">
  7. *{
  8. padding:0;
  9. margin:0;
  10. }
  11.  
  12. .box3{
  13. width:300px;
  14. height:200px;
  15. background-color: #5cb85c;
  16. position: absolute; /*遮盖下一个盒子*/
  17. }
  18. .box4{
  19. width:300px;
  20. height:200px;
  21. background-color: #b3d7ff;
  22. /*position: absolute; !*遮盖下一个盒子*!*/
  23. }
  24. .box5{
  25. width:300px;
  26. height:200px;
  27. background-color:indianred;
  28. }
  29. span{
  30. width:200px;
  31. height:100px;
  32. background-color: #7d74ff;
  33. position: absolute; /*相当于display:block,将行内元素转成了块状元素*/
  34. }
  35. </style>
  36. </head>
  37. <body>
  38.  
  39. <div class="box3"></div>
  40. <div class="box4"></div>
  41. <div class="box5"></div>
  42. <span>你好,明天</span>
  43. </body>
  44. </html>

 2、绝对定位参考点

  1. 设置属性top时,定位点在页面左上角,不是浏览器页面,一定要区分
  2. 设置属性为bottom时,定位点在页面左下,页面滑动缩放时也能随之滑动缩放

  示例: 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>绝对定位参考点</title>
  6. <style type="text/css">
  7. *{
  8. padding:0;
  9. margin:0;
  10. }
  11. body{
  12. width:1000px;
  13. height: 1000px;
  14. border: 2px solid yellowgreen;
  15. }
  16. .box6{
  17. width:300px;
  18. height:200px;
  19. background-color: #b3d7ff;
  20. position: absolute;
  21. /*top:100px; !*设置属性top时,定位点在页面左上角,不是浏览器页面,一定要区分*!*/
  22. left: 2px;
  23. bottom: 100px;/*设置属性为bottom时,定位点在页面左下,页面滑动缩放时也能随之滑动缩放*/
  24. }
  25.  
  26. </style>
  27. </head>
  28. <body>
  29. <div class="box6"></div>
  30. </body>
  31. </html>

  绝对定位以父辈元素作为参考点

    分三种:父相子绝,父绝子绝,父固子绝,都是以父辈元素为参考点。

    注意:

  1. “父绝子绝”(即父辈设为绝对定位,子辈设为绝对定位),没有实战意义,做站的时候不会出现父绝子绝。
  2. 因为绝对定位脱离标准流,影响页面的布局。与之相反,“父相子绝”(即父辈设为相对定位,子辈设为绝对定位),
  3. 因为父辈设为了相对定位,不脱离标准流,子元素设置绝对定位,仅仅是在当前父辈元素内调整位置信息。

    “父相子绝”主要有两种情况:

    ①爷爷设置相对定位,父亲没有设置定位,儿子设置绝对定位,那么以爷爷的左顶点为参考点来调整位置。

    ②爷爷设置相对定位,父亲设置相对定位,儿子设置绝对定位,那么以父亲的左顶点为参考点来调整位置。

    总而言之就是:

  1.      父元素设置相对定位,子元素设置绝对定位,此时子元素的参考点为父元素的左顶点。
       情况①示例如下:   
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>爷爷设置相对定位,父亲没有设置定位,儿子设置绝对定位,那么以爷爷的左顶点为参考点来调整位置(父相自绝)</title>
  6. <style type="text/css">
  7.  
  8. .box2{
  9. width:300px;
  10. height:200px;
  11. border: 2px solid pink;
  12. margin: 100px;
  13. position: relative;
  14. padding: 100px;
  15. }
  16. .box2-son{
  17. width: 200px;
  18. height: 150px;
  19. background-color: #b2e567;
  20.  
  21. }
  22. span{
  23. width:100px;
  24. height: 80px;
  25. background-color: #428bca;
  26. position: absolute;
  27. top:50px;
  28. left:50px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="box2">
  34. <div class="box2-son">
  35. <span></span>
  36. </div>
  37. </div>
  38. </body>
  39. </html>

    情况②示例如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>绝对定位参考点-以父辈元素作为参考点,父辈相对定位,子辈绝对定位(父相自绝)</title>
  6. <style type="text/css">
  7. *{
  8. padding:0;
  9. margin:0;
  10. }
  11. /*爷爷设置相对定位,父亲设置相对定位,儿子设置绝对定位,那么以父亲的左顶点为参考点来调整位置*/
  12. *{
  13. padding:0;
  14. margin:0;
  15. }
  16. .box2{
  17. width:300px;
  18. height:200px;
  19. border: 2px solid pink;
  20. margin: 100px;
  21. position: relative;
  22. padding: 100px;
  23. }
  24. .box2-son{
  25. width: 200px;
  26. height: 150px;
  27. background-color: #b2e567;
  28. position: relative;
  29. }
  30. span{
  31. width:100px;
  32. height: 80px;
  33. background-color: #428bca;
  34. position: absolute;
  35. top:40px;
  36. left:50px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41.  
  42. <div class="box2">
  43. <div class="box2-son">
  44. <span></span>
  45. </div>
  46. </div>
  47. </body>
  48. </html>

    总结成一种就是: 

  1. <style type="text/css">
  2. *{
  3. padding:0;
  4. margin:0;
  5. }
  6. /*父元素设置相对定位,子元素设置绝对定位,此时子元素的参考点为父元素的左顶点*/
  7. .box1{
  8. width:400px;
  9. height: 300px;
  10. border: 2px solid palegreen;
  11. margin: 100px;
  12. /*父元素设置相对定位*/
  13. position: relative;
  14. }
  15. p{
  16. width:200px;
  17. height: 100px;
  18. background-color:indianred;
  19. /*子元素设置绝对定位*/
  20. position: absolute;
  21. top: 10px;
  22. left: 20px;
  23. }
  24.  
  25. </style>

  3、绝对定位的盒子无视父辈的padding 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>绝对定位参考点-以父辈元素作为参考点,父辈相对定位,子辈绝对定位(父相自绝)</title>
  6. <style type="text/css">
  7. *{
  8. padding:0;
  9. margin:0;
  10. }
  11. *{
  12. padding:0;
  13. margin:0;
  14. }
  15.  
  16. .box{
  17. width:200px;
  18. height:200px;
  19. border: 2px solid pink;
  20. margin: 100px;
  21. position: relative;
  22. padding: 30px;
  23. }
  24. .box-son{
  25. width: 100px;
  26. height: 100px;
  27. background-color: #b2e567;
  28. position:absolute;
  29. top:0;
  30. left:0;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="box">
  36. <div class="box-son">
  37. </div>
  38. </div>
  39. </body>
  40. </html>

  4、绝对定位盒子居中

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>绝对定位参考点-以父辈元素作为参考点,父辈相对定位,子辈绝对定位(父相自绝)</title>
  6. <style type="text/css">
  7. *{
  8. padding:0;
  9. margin:0;
  10. }
  11. /*绝对定位盒子居中*/
  12. .box{
  13. width:100%;
  14. height: 50px;
  15. background-color: #7d74ff;
  16.  
  17. }
  18. .box-son{
  19. width: 1000px;
  20. height:50px;
  21. background-color: #b3d7ff;
  22. /*设置绝对定位,使盒子水平居中,必须要写下面三句*/
  23. position: absolute;
  24. left:50%;
  25. margin-left: -500px; /*数值取盒子宽度的一半(取负值,表示向左移动)*/
  26. }
  27.  
  28. </style>
  29. </head>
  30. <body>
  31.  
  32. <div class="box">
  33. <div class="box-son">
  34. </div>
  35. </div>
  36. </body>
  37. </html>

3️⃣  固定定位

  1、何谓固定定位?

    固定定位即固定当前的元素不会随着页面滚动而滚动。

  2、特性:

  1. 1、脱标;
  2. 2、提升层级;
  3. 3、固定不变,不会随着页面滚动而滚动。

  3、参考点:

  1. 设置固定定位,用top描述时,那么是以浏览器的左上角为参考点,如果用bottom描述,那么以浏览器的左下角为参考点

  4、作用:

  1. 1、返回顶部栏
  2. 2、固定导航栏
  3. 3、小广告

  5、示例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>固定定位</title>
  6. <style type="text/css">
  7. /* *{}、ul{}和a{}用于页面初始化设置 */
  8. *{
  9. padding:0;
  10. margin:0;
  11. }
  12. ul{
  13. list-style: none;
  14. }
  15. a{
  16. text-decoration: none;
  17. }
  18. body{
  19. /*给body设置导航栏的高度,使下方的图片能够完全显示出来*/
  20. padding-top:50px;
  21. }
  22. p{
  23. width:200px;
  24. height:180px;
  25. background-image: url("./images/lufei.jpg");
  26. background-repeat: no-repeat;
  27. border-radius: 5px;
  28. position: fixed;
  29. top:100px;
  30. /*bottom: 100px;*/
  31. left:80px;
  32. }
  33. /* 固定导航栏 */
  34. .wrap{
  35. width:100%;
  36. height:50px;
  37. background-color: #b3d7ff;
  38. position:fixed;
  39. top:0;
  40. left:0;
  41. }
  42. .wrap .nav{
  43. width:1000px;
  44. height:50px;
  45. margin:0 auto;
  46. background-color:yellowgreen;
  47. }
  48. .wrap .nav ul li{
  49. width:200px;
  50. height:50px;
  51. float:left;
  52. text-align: center;
  53. }
  54. .wrap .nav ul li a{
  55. width: 200px;
  56. height:50px;
  57. font-size: 16px;
  58. font-family: "Microsoft YaHei UI";
  59. line-height: 50px;
  60. background-color: #7d74ff;
  61. display: inline-block;
  62. }
  63. .wrap .nav ul li a:hover{
  64. font-size: 20px;
  65. color:mediumspringgreen;
  66. background-color: #5bc0de;
  67. }
  68.  
  69. /*返回顶部栏*/
  70. .return_top{
  71. width:40px;
  72. height: 60px;
  73. background-color: #428bca;
  74. font-size: 18px;
  75. color: white;
  76. position: fixed;
  77. right: 60px;
  78. bottom: 60px;
  79. padding: 10px;
  80. border-radius: 6px;
  81. }
  82. .return_top a{
  83. text-decoration: none;
  84. color:yellowgreen;
  85. }
  86. </style>
  87. </head>
  88. <body>
  89. <div class="wrap">
  90. <div class="nav">
  91. <ul>
  92. <li><a href="">目录1</a></li>
  93. <li><a href="">目录2</a></li>
  94. <li><a href="">目录3</a></li>
  95. <li><a href="">目录4</a></li>
  96. <li><a href="">目录5</a></li>
  97.  
  98. </ul>
  99. </div>
  100. </div>
  101. <div>
  102. <img src="data:images/pic1.jpg" alt="">
  103. <img src="data:images/pic1.jpg" alt="">
  104. <img src="data:images/pic1.jpg" alt="">
  105. <img src="data:images/pic1.jpg" alt="">
  106. <img src="data:images/pic1.jpg" alt="">
  107. <img src="data:images/pic1.jpg" alt="">
  108. <p></p>
  109. </div>
  110. <div class="return_top">
  111. <a href="#">返回顶部</a>
  112. </div>
  113. </body>
  114. </html>

4️⃣  z-index 

  1. 1z-index值仅表示谁压着谁。数值大的压盖数值小的。
  2. 2、只有定位了的元素,才能有z-index,也就是说,不管相对定位、绝对定位还是固定定位,
  3. 都可以使用z-index,而浮动元素不能使用z-index.
  4. 3z-indexz值没有单位,就是一个正整数,默认的z-index值为0.
  5. 4、如果元素都没有z-index值,或者z-index的值一样,那么谁写在HTML之后,
  6. 谁在上面压着别人,定位了的元素,永远压住没有定位的元素。
  7. 5、从父现象:父亲怂了,儿子再牛逼也没有。
  1. <title>z-index实例</title>
  2. <style type="text/css">
  3. *{
  4. padding: 0;
  5. margin:0;
  6. }
  7. .father1{
  8. width: 300px;
  9. height: 200px;
  10. background-color: #b2e567;
  11. position: relative;
  12. z-index: 120;
  13. }
  14. .son1{
  15. width:110px;
  16. height: 100px;
  17. background-color: #b3d7ff;
  18. position: absolute;
  19. top:600px;
  20. left: 360px;
  21. z-index: 30;
  22. }
  23. .father2{
  24. width:300px;
  25. height: 200px;
  26. background-color: #7d74ff;
  27. position: relative;
  28. z-index: 100;
  29. }
  30. .son2{
  31. width: 120px;
  32. height:120px;
  33. background-color: burlywood;
  34. position: absolute;
  35. top:360px;
  36. left:360px;
  37. z-index:20;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div class="father1">
  43. <div class="son1"></div>
  44. </div>
  45. <div class="father2">
  46. <div class="son2"></div>
  47. </div>
  48. </body>
  49. </html>

前端开发之CSS篇四的更多相关文章

  1. 前端开发之css篇

    一.css简介 css(Cascading Style Sheets)层叠样式表,是一种为html文档添加样式的语言,主要有两个功能:渲染和布局.使用css主要关注两个点:查找到标签,属性操作 二.c ...

  2. 前端开发之CSS篇三

    主要内容:  一.CSS布局之浮动     二.清除浮动带来的问题     三.margin塌陷问题和水平居中     四.善用父级的的padding取代子级的margin     五.文本属性和字体 ...

  3. 前端开发之CSS篇二

    主要内容: 一.CSS的继承性和层叠性 二.盒模型 三.padding属性 四.border属性 五.margin属性 六.标准文档流 七.行内元素和块状元素转换 1️⃣  CSS的继承性和层叠性 1 ...

  4. 前端开发之CSS入门篇

    一.CSS介绍和语法 二.CSS引入方式 三.基本选择器 四.高级选择器 五.伪类选择器 六.伪元素选择器 1️⃣  CSS介绍和语法 1. CSS的介绍 (1)为什么需要CSS? 使用css的目的就 ...

  5. 前端开发之JavaScript篇

    一.JavaScript介绍  前端三剑客之JavaScript,简称js,可能是这三个里面最难的一个了.很早以前,市面上流通着三种js版本,为了统一,ECMA(欧洲计算机制造协会)定义了规范的版本, ...

  6. 前端开发之HTML篇一

    主要内容:     一.HTML简介 二.HTML标签        三.HTML文档结构和注释        四.head标签及相关内容        五.body标签及相关内容 1️⃣   HTM ...

  7. 前端开发之jQuery篇--选择器

    主要内容: 1.jQuery简介 2.jQuery文件的引入 3.jQuery选择器 4.jQuery对象与DOM对象的转换 一.jQuery简介 1.介绍 jQuery是一个JavaScript库: ...

  8. 前端开发之HTML篇二

    主要内容: 一.表格标签 -- table 二.表单标签 -- form 三.常用标签属性和分类 四.标签嵌套规则 1️⃣  表格标签 -- table 表格由<table> 标签来定义. ...

  9. 前端开发之html篇

    一.什么是html? 1.我们说socket网络编程的时候,提到过一个cs模型,就是客户端—服务端模型,前端开发也是基于网络编程,但是这时就应该是bs模型了,是浏览器与服务端的通信. 我们可以模拟一个 ...

随机推荐

  1. PowerDesigner生成Oracle表名带有引号的解决方法

    PowerDesigner生成表名带有引号,如下: /*==============================================================*/ /* Tabl ...

  2. 《DSP using MATLAB》示例Example 9.7

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  3. 网站使用QQ登录问题小结

    关于网站如何使用QQ登陆这个问题就不多说了,很简单,登陆connect.qq.com找到相应的SDK,下载下来,里面会有demo,将相应的appid,appkey和回调地址callback改成自己的就 ...

  4. python 中的异常处理

    refer to: http://www.runoob.com/python/python-exceptions.html http://www.pythondoc.com/pythontutoria ...

  5. java I/O进程控制,重定向 演示样例代码

    java I/O进程控制,重定向 演示样例代码 package org.rui.io.util; import java.io.*; /** * 标准I/O重定向 */ public class Re ...

  6. poj 1637 Sightseeing tour——最大流+欧拉回路

    题目:http://poj.org/problem?id=1637 先给无向边随便定向,如果一个点的入度大于出度,就从源点向它连 ( 入度 - 出度 / 2 ) 容量的边,意为需要流出去这么多:流出去 ...

  7. jq 合并json对象

    一,保存object1和2合并后产生新对象,若2中有与1相同的key,默认2将会覆盖1的值 1 var object = $.extend({}, object1, object2); 二,将2的值合 ...

  8. Java编程打印出1000以内所有的完数

    /*如果一个数等 于其所有因子之和,我们就称这个数为"完数" * 例如6的因子为1,2,3, 6=1+2+3, 6就是一一个完数. * 请编程打印出1000以内所有的完数*/ pu ...

  9. Spring不支持静态注入

    在springframework里,我们不能@Autowired一个静态变量,使之成为一个spring bean,例如下面这样: @Autowired private static YourClass ...

  10. 【BZOJ】1911: [Apio2010]特别行动队(斜率优化dp)

    题目 传送门:QWQ 分析 用$ dp[i] $ 表示前 i 个人组成的战斗力之和 然后显然$ dp[i]=Max (  dp[j]+a*(sum[i]-sum[j])^2+b*(sum[i]-sum ...