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 的位置进行偏移,从而实现精确定位。

相关推荐
just小千13 分钟前
重学React(一):描述UI
前端·react.js·ui
fakaifa31 分钟前
【最新版】沃德代驾源码全开源+前端uniapp
前端·小程序·uni-app·开源·php·沃德代驾·代驾小程序
清羽_ls1 小时前
leetcode-位运算
前端·算法·leetcode·位运算
李菠菜1 小时前
利用Nginx实现高性能的前端打点采集服务(支持GET和POST)
linux·前端·nginx
lilye661 小时前
精益数据分析(6/126):深入理解精益分析的核心要点
前端·人工智能·数据分析
Apifox1 小时前
Apifox 4月更新|Apifox在线文档支持LLMs.txt、评论支持使用@提及成员、支持为团队配置「IP 允许访问名单」
前端·后端·ai编程
Jolyne_1 小时前
搭建公司前端脚手架
前端·架构·前端框架
hang_bro1 小时前
el-tree的动态加载问题与解决
前端·javascript·vue.js
天天扭码1 小时前
一杯珍珠奶茶的时间吃透一道高频面试算法题——搜索二位矩阵Ⅱ
前端·算法·面试
il1 小时前
Deepdive into Tanstack Query - 1.0
前端