CSS定位详解

打印 上一主题 下一主题

主题 914|帖子 914|积分 2742

1. 相对定位

   
     1.1    如何设置相对定位?        给元素设置    position:relative    即可实现相对定位。        可以使用    left    、    right    、    top    、    bottom    四个属性调整位置。        1.2    相对定位的参考点在那里?        相对自己原来的位置        1.3    相对定位的特点:        1. 不会离开文档流,元素位置的厘革,只是视觉效果上的厘革,不会对其他元素产生任何影响。        2. 定位元素的表现层级比普通元素高,无论什么定位,表现层级都是一样的。        默认规则是:        定位的元素会盖在普通元素之上。都发生定位的两个元素,后写的元素会盖在先写的元素之上。        3.    left    不能和    right    一起设置,    top    和    bottom    不能一起设置。        4.    相对定位的元素,也能继续浮动,但不推荐这样做。        5.    相对活动的元素,也能通过    margin    调整位置,但不推荐这样做。   
  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>01_相对定位</title>
  7.     <style>
  8.         .outer{
  9.             width: 500px;
  10.             background-color: aquamarine;
  11.             border: 1px solid black;
  12.             padding: 20px;
  13.         }
  14.         .box{
  15.             width: 100px;
  16.             height: 100px;
  17.             font-size: 20px;
  18.         }
  19.         .box1 {
  20.             background-color: red;
  21.         }
  22.         .box2 {
  23.             background-color: green;
  24.             position: relative;
  25.             left: 10px;
  26.         }
  27.         .box3 {
  28.             background-color: hotpink;
  29.         }
  30.     </style>
  31. </head>
  32. <body>
  33.     <div class="outer">
  34.         <div class="box box1">1</div>
  35.         <div class="box box2">2</div>
  36.         <div class="box box3">3</div>
  37.     </div>
  38. </body>
  39. </html>
复制代码

 留意:绝大多数环境下,相对定位,会与绝对定位共同使用。
2. 绝对定位

      2.1    如何设置绝对定位?        给元素设置    position: absolute    即可实现绝对定位。        可以使用    left    、    right    、    top    、    bottom    四个属性调整位置。      绝对定位的参考点是参考它的  包罗块  。  
      什么是包罗块?        1. 对于没有离开文档流的元素:包罗块就是父元素;        2. 对于离开文档流的元素:包罗块是第一个拥有定位属性的先人元素(如果全部先人都        没定位,那包罗块就是整个页面)   







口诀:子绝父相(子元素开启绝对定位,父元素就要开启相对定位)



鼠标浮动在父元素上,盒子2向右移动200px
  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>02_绝对定位</title>
  7.     <style>
  8.         .outer {
  9.             width: 500px;
  10.             background-color: aquamarine;
  11.             border: 1px solid black;
  12.             padding: 20px;
  13.             position: relative;
  14.         }
  15.         .box {
  16.             width: 100px;
  17.             height: 100px;
  18.             font-size: 20px;
  19.         }
  20.         .box1 {
  21.             background-color: red;
  22.         }
  23.         .box2 {
  24.             background-color: green;
  25.             /* 给2开启了绝对定位,他就脱离了文档流 3就占领了2的位置 */
  26.             position: absolute;
  27.    
  28.         }
  29.         .box3 {
  30.             background-color: hotpink;
  31.         }
  32.         .outer:hover .box2{
  33.             right: 200px;
  34.         }
  35.     </style>
  36. </head>
  37. <body>
  38.     <div class="outer">
  39.         <div class="box box1">1</div>
  40.         <div class="box box2">2</div>
  41.         <div class="box box3">3</div>
  42.     </div>
  43. </body>
  44. </html>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

慢吞云雾缓吐愁

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表