相对定位绝对定位

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Positioning Example</title>
<style>
  /* 相对定位的父元素 */
  .relative-parent {
    position: relative;
    width: 200px;
    height: 200px;
    background-color: lightblue;
    margin-bottom: 20px; /* 增加一些间距用于区分不同块 */
  }

  /* 绝对定位的子元素 */
  .absolute-child {
    position: absolute;
    top: 10px;
    left: 10px;
    width: 100px;
    height: 100px;
    background-color: coral;
  }

  /* 绝对定位的父元素 */
  .absolute-parent {
    position: absolute;
    top: 1050px; /* 距离顶部250px */
    left: 200px; /* 距离左侧20px */
    width: 200px;
    height: 200px;
    background-color: lightcoral;
  }

  /* 绝对定位的子元素,相对于绝对定位的父元素 */
  .absolute-child-inside-absolute {
    position: absolute;
    top: 10px;
    left: 10px;
    width: 100px;
    height: 100px;
    background-color: lightgoldenrodyellow;
  }

  /* 固定定位的父元素 */
  .fixed-parent {
    position: fixed;
    top: 20px;
    right: 20px; /* 固定在视口右上角 */
    width: 200px;
    height: 200px;
    background-color: lightgreen;
  }

  /* 绝对定位的子元素,相对于固定定位的父元素 */
  .absolute-child-inside-fixed {
    position: absolute;
    bottom: 10px;
    right: 10px;
    width: 100px;
    height: 100px;
    background-color: lightpink;
  }
</style>
</head>
<body>

<div class="relative-parent">
  相对定位的父元素
  <div class="absolute-child">绝对定位的子元素</div>
</div>

<div class="absolute-parent">
  绝对定位的父元素
  <div class="absolute-child-inside-absolute">绝对定位的子元素</div>
</div>

<div class="fixed-parent">
  固定定位的父元素
  <div class="absolute-child-inside-fixed">绝对定位的子元素</div>
</div>

</body>
</html>

也可以多层嵌套

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nested Positioning Example</title>
<style>
  /* 外层相对定位的父元素 */
  .grandparent {
    position: relative;
    width: 400px;
    height: 400px;
    background-color: lightblue;
    padding: 20px;
  }

  /* 中间绝对定位的父元素 */
  .parent {
    position: absolute;
    top: 20px;
    left: 20px;
    width: 300px;
    height: 300px;
    background-color: lightcoral;
    padding: 20px;
  }

  /* 内层绝对定位的子元素 */
  .child {
    position: absolute;
    top: 20px;
    left: 20px;
    width: 200px;
    height: 200px;
    background-color: lightgreen;
    padding: 20px;
  }

  /* 孙子元素 */
  .grandchild {
    position: absolute;
    bottom: 20px;
    right: 20px;
    width: 100px;
    height: 100px;
    background-color: lightpink;
    padding: 20px;
  }
</style>
</head>
<body>

<div class="grandparent">
  外层相对定位的父元素
  <div class="parent">
    中间绝对定位的父元素
    <div class="child">
      内层绝对定位的子元素
      <div class="grandchild">
        孙子元素
      </div>
    </div>
  </div>
</div>

</body>
</html>
相关推荐
开开心心就好4 小时前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
Setsuna_F_Seiei6 小时前
前端转型 Agent 开发 05 之 Agent Hooks 与 Checkpointer(让 Agent 从全自动转变人为可掌控)
前端·agent·ai编程
百万蹄蹄向前冲8 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙8 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
linux_cfan10 小时前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
kyriewen10 小时前
我扒了 10,221 条 JD:腾讯技术岗 75% 在要 AI
前端·人工智能·ai编程
郑州光合科技余经理11 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
IT_陈寒11 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端
Blanche150012 小时前
利用 RAG 为答疑机器人扩展知识范围
前端
天若有情67312 小时前
【纯前端小工具】公历生日转农历,批量查询每年农历生日对应的公历日期(GitHub Pages在线直接用)
前端·javascript·github pages·农历转换·lunisolar·网页小工具