CSS常用的两种定位方式

在CSS中,absoluterelative 是两种常用的定位方式,分别通过 position 属性进行设置。它们用于控制元素在页面中的位置。理解这两种定位方式对于布局和设计响应式页面非常重要。

position: relative

定义

relative 定位是相对自身原始位置进行偏移。

特性
  • 元素仍然占据其在正常文档流中的空间,但可以通过 top, right, bottom, left 属性进行偏移。
  • 偏移量是相对于元素本来的位置进行的。
  • 不会影响其他元素的布局,因为它仍然保留在文档流中。
示例
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Relative Position Example</title>
  <style>
    .relative-box {
      position: relative;
      top: 20px; /* 相对于其原始位置向下偏移20像素 */
      left: 30px; /* 相对于其原始位置向右偏移30像素 */
      width: 100px;
      height: 100px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="relative-box">Relative Box</div>
</body>
</html>

position: absolute

定义

absolute 定位是相对于最近的已定位(relative, absolute, fixed, sticky)祖先元素进行偏移。如果没有已定位的祖先元素,则相对于初始包含块(通常是视口)进行定位。

特性
  • 元素脱离正常文档流,不再占据空间,其他元素会忽略它的位置。
  • 通过 top, right, bottom, left 属性进行偏移,相对于最近的已定位祖先元素。
  • 如果没有找到已定位的祖先元素,则相对于视口进行定位。
示例
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Absolute Position Example</title>
  <style>
    .container {
      position: relative;
      width: 200px;
      height: 200px;
      background-color: lightgray;
    }

    .absolute-box {
      position: absolute;
      top: 20px; /* 相对于最近的已定位祖先元素向下偏移20像素 */
      left: 30px; /* 相对于最近的已定位祖先元素向右偏移30像素 */
      width: 100px;
      height: 100px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="absolute-box">Absolute Box</div>
  </div>
</body>
</html>

比较与总结

  • position: relative

    • 相对自身原始位置进行偏移。
    • 元素仍占据原始位置的空间。
    • 用于微调元素位置,不会影响其他元素的布局。
  • position: absolute

    • 脱离文档流,相对于最近的已定位祖先元素进行定位。
    • 不占据空间,其他元素会忽略它的位置。
    • 用于精确定位元素,通常与 relative 定位的祖先元素配合使用。

结合使用

relativeabsolute 定位经常结合使用,以实现复杂的布局效果。典型的用法是将容器元素设置为 relative 定位,而内部的子元素设置为 absolute 定位,这样子元素可以相对于容器元素进行定位。

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Combined Position Example</title>
  <style>
    .relative-container {
      position: relative;
      width: 300px;
      height: 300px;
      background-color: lightgray;
    }

    .absolute-child {
      position: absolute;
      top: 50px;
      left: 50px;
      width: 100px;
      height: 100px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="relative-container">
    <div class="absolute-child">Child</div>
  </div>
</body>
</html>

在这个例子中,.absolute-child 会相对于 .relative-container 的位置进行偏移,从而实现精确定位。

相关推荐
袁煦丞15 分钟前
2025.8.18实验室【代码跑酷指南】Jupyter Notebook程序员的魔法本:cpolar内网穿透实验室第622个成功挑战
前端·程序员·远程工作
Joker Zxc20 分钟前
【前端基础】flex布局中使用`justify-content`后,最后一行的布局问题
前端·css
无奈何杨23 分钟前
风控系统事件分析中心,关联关系、排行、时间分布
前端·后端
Moment29 分钟前
nginx 如何配置防止慢速攻击 🤔🤔🤔
前端·后端·nginx
晓得迷路了34 分钟前
栗子前端技术周刊第 94 期 - React Native 0.81、jQuery 4.0.0 RC1、Bun v1.2.20...
前端·javascript·react.js
前端小巷子36 分钟前
Vue 自定义指令
前端·vue.js·面试
玲小珑41 分钟前
Next.js 教程系列(二十七)React Server Components (RSC) 与未来趋势
前端·next.js
Mike_jia42 分钟前
UptimeRobot API状态监控:零成本打造企业级业务健康看板
前端
江城开朗的豌豆43 分钟前
React状态更新踩坑记:我是这样优雅修改参数的
前端·javascript·react.js
CodeSheep1 小时前
Stack Overflow,轰然倒下了!
前端·后端·程序员