相对定位绝对定位

打印 上一主题 下一主题

主题 989|帖子 989|积分 2967


  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. <title>Positioning Example</title>
  7. <style>
  8.   /* 相对定位的父元素 */
  9.   .relative-parent {
  10.     position: relative;
  11.     width: 200px;
  12.     height: 200px;
  13.     background-color: lightblue;
  14.     margin-bottom: 20px; /* 增加一些间距用于区分不同块 */
  15.   }
  16.   /* 绝对定位的子元素 */
  17.   .absolute-child {
  18.     position: absolute;
  19.     top: 10px;
  20.     left: 10px;
  21.     width: 100px;
  22.     height: 100px;
  23.     background-color: coral;
  24.   }
  25.   /* 绝对定位的父元素 */
  26.   .absolute-parent {
  27.     position: absolute;
  28.     top: 1050px; /* 距离顶部250px */
  29.     left: 200px; /* 距离左侧20px */
  30.     width: 200px;
  31.     height: 200px;
  32.     background-color: lightcoral;
  33.   }
  34.   /* 绝对定位的子元素,相对于绝对定位的父元素 */
  35.   .absolute-child-inside-absolute {
  36.     position: absolute;
  37.     top: 10px;
  38.     left: 10px;
  39.     width: 100px;
  40.     height: 100px;
  41.     background-color: lightgoldenrodyellow;
  42.   }
  43.   /* 固定定位的父元素 */
  44.   .fixed-parent {
  45.     position: fixed;
  46.     top: 20px;
  47.     right: 20px; /* 固定在视口右上角 */
  48.     width: 200px;
  49.     height: 200px;
  50.     background-color: lightgreen;
  51.   }
  52.   /* 绝对定位的子元素,相对于固定定位的父元素 */
  53.   .absolute-child-inside-fixed {
  54.     position: absolute;
  55.     bottom: 10px;
  56.     right: 10px;
  57.     width: 100px;
  58.     height: 100px;
  59.     background-color: lightpink;
  60.   }
  61. </style>
  62. </head>
  63. <body>
  64. <div class="relative-parent">
  65.   相对定位的父元素
  66.   <div class="absolute-child">绝对定位的子元素</div>
  67. </div>
  68. <div class="absolute-parent">
  69.   绝对定位的父元素
  70.   <div class="absolute-child-inside-absolute">绝对定位的子元素</div>
  71. </div>
  72. <div class="fixed-parent">
  73.   固定定位的父元素
  74.   <div class="absolute-child-inside-fixed">绝对定位的子元素</div>
  75. </div>
  76. </body>
  77. </html>
复制代码
也可以多层嵌套
  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. <title>Nested Positioning Example</title>
  7. <style>
  8.   /* 外层相对定位的父元素 */
  9.   .grandparent {
  10.     position: relative;
  11.     width: 400px;
  12.     height: 400px;
  13.     background-color: lightblue;
  14.     padding: 20px;
  15.   }
  16.   /* 中间绝对定位的父元素 */
  17.   .parent {
  18.     position: absolute;
  19.     top: 20px;
  20.     left: 20px;
  21.     width: 300px;
  22.     height: 300px;
  23.     background-color: lightcoral;
  24.     padding: 20px;
  25.   }
  26.   /* 内层绝对定位的子元素 */
  27.   .child {
  28.     position: absolute;
  29.     top: 20px;
  30.     left: 20px;
  31.     width: 200px;
  32.     height: 200px;
  33.     background-color: lightgreen;
  34.     padding: 20px;
  35.   }
  36.   /* 孙子元素 */
  37.   .grandchild {
  38.     position: absolute;
  39.     bottom: 20px;
  40.     right: 20px;
  41.     width: 100px;
  42.     height: 100px;
  43.     background-color: lightpink;
  44.     padding: 20px;
  45.   }
  46. </style>
  47. </head>
  48. <body>
  49. <div class="grandparent">
  50.   外层相对定位的父元素
  51.   <div class="parent">
  52.     中间绝对定位的父元素
  53.     <div class="child">
  54.       内层绝对定位的子元素
  55.       <div class="grandchild">
  56.         孙子元素
  57.       </div>
  58.     </div>
  59.   </div>
  60. </div>
  61. </body>
  62. </html>
复制代码


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

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

民工心事

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