跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客我的书AI学习GitHub 精选镜像AI 生图工具UI配色美学关于
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
HTML / CSS大前端

CSS 定位属性详解:static、relative、fixed 与 absolute

CSS position 属性控制元素定位方式,包含 static、relative、fixed、absolute 四种模式。static 为默认流布局;relative 基于自身原位置偏移;fixed 相对于视口固定;absolute 相对于最近的定位父级。配合 top/bottom/left/right 使用。重叠元素通过 z-index 控制层级,数值大的在上。若无设置,HTML 后出现的元素覆盖在前。

kaikai发布于 2025/2/5更新于 2026/9/1170 浏览
CSS 定位属性详解:static、relative、fixed 与 absolute

CSS 定位属性详解

position 属性决定了元素的定位模式,它控制元素如何被放置在文档流中。常见的值有四种:static、relative、fixed 和 absolute。配合 top、bottom、left、right 使用,可以精确控制元素位置。

position: static

这是默认值。HTML 元素默认就是 static 定位的,不受 top、bottom、left、right 影响,始终按照正常文档流排列。

<!DOCTYPE html>
<html>
<head>
<style>
div.static {
    position: static;
    border: 3px solid #73AD21;
}
</style>
</head>
<body>
<div class="static">This &lt;div&gt; element has position: static;</div>
</body>
</html>

对应的样式定义如下:

div.static {
    position: static;
    border: 3px solid #73AD21;
}

position: relative

相对定位的元素相对于其自身原始位置进行偏移。设置 、、、 后,元素会离开原来的位置,但原来占据的空间依然保留,不会影响其他元素布局。

top
bottom
left
right
<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
    position: relative;
    left: 30px;
    border: 3px solid #73AD21;
}
</style>
</head>
<body>
<div class="relative">This div element has position: relative;</div>
</body>
</html>

样式代码如下:

div.relative {
    position: relative;
    left: 30px;
    border: 3px solid #73AD21;
}

position: fixed

固定定位的元素相对于**浏览器视口(viewport)**定位。无论页面是否滚动,元素都会停留在屏幕的同一位置。它不会在文档流中预留空间。

看右下角那个固定定位的盒子,即使页面滚动,它也会一直钉在那里。

<!DOCTYPE html>
<html>
<head>
<style>
div.fixed {
    position: fixed;
    bottom: 0;
    right: 0;
    width: 300px;
    border: 3px solid #73AD21;
}
</style>
</head>
<body>
<h2>position: fixed;</h2>
<p>An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled:</p>
<div class="fixed">This div element has position: fixed;</div>
</body>
</html>

position: absolute

绝对定位的元素相对于最近的已定位父级元素(即 position 不为 static 的祖先)定位。如果没有这样的父级,则相对于初始包含块(通常是 <body>),并随页面滚动而移动。

这里有个关键点:'已定位'的元素是指除了 static 以外的所有元素。

下面是一个典型的父子定位示例:

<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
    position: relative;
    width: 400px;
    height: 200px;
    border: 3px solid #73AD21;
}

div.absolute {
    position: absolute;
    top: 80px;
    right: 0;
    width: 200px;
    height: 100px;
    border: 3px solid #73AD21;
}
</style>
</head>
<body>
<div class="relative">
    This div element has position: relative;
    <div class="absolute">This div element has position: absolute;</div>
</div>
</body>
</html>

样式部分:

div.relative {
    position: relative;
    width: 400px;
    height: 200px;
    border: 3px solid #73AD21;
}

div.absolute {
    position: absolute;
    top: 80px;
    right: 0;
    width: 200px;
    height: 100px;
    border: 3px solid #73AD21;
}

元素重叠与 z-index

定位元素时,它们可能会互相覆盖。这时就需要用到 z-index 属性来控制层叠顺序。

  • z-index 数值越大,层级越高,越显示在上层。
  • 可以是负数。

如果两个已定位元素没有设置 z-index 且发生重叠,那么在 HTML 代码中后出现的元素会覆盖在前。这一点在实际开发中经常需要留意,尤其是动态渲染的内容。

注意:z-index 只对定位元素(position 不为 static)生效。

实战:图片上叠加文字

利用绝对定位,我们可以轻松实现图片上的文字标注效果。核心思路是父容器设为 relative,内部文字设为 absolute。

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<style>
.w3-example {
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
    margin: 20px 0;
    padding: 0.01em 16px;
    background-color: #f1f1f1;
}
</style>
</head>
<body>
<div class="w3-example">
    <h3>Example</h3>
    <div style="position: relative; text-align: center;">
        <img src="http://www.w3schools.com/css/trolltunga.jpg" alt="Norway" style="width: 100%; height: auto; opacity: 0.3">
        <div style="position: absolute; bottom: 8px; left: 16px; font-size: 18px">Bottom Left</div>
        <div style="position: absolute; top: 8px; left: 16px; font-size: 18px">Top Left</div>
        <div style="position: absolute; top: 8px; right: 16px; font-size: 18px">Top Right</div>
        <div style="position: absolute; bottom: 8px; right: 16px; font-size: 18px">Bottom Right</div>
        <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 18px">Centered</div>
    </div>
</div>
</body>
</html>

这个例子展示了如何利用 top、bottom、left、right 以及 transform 将文字精准地放在图片的各个角落或正中间。

目录

  1. CSS 定位属性详解
  2. position: static
  3. position: relative
  4. position: fixed
  5. position: absolute
  6. 元素重叠与 z-index
  7. 实战:图片上叠加文字

更多推荐文章

查看全部
  • SALMONN:支持语音音频与音乐的开源大语言模型
  • 阿里云 Moltbot 机器人钉钉 Stream 流式接入配置
  • C# 项目 Git 版本控制最佳实践:.gitignore 与依赖管理
  • Android 开发环境兼容性指南:API、JDK、AGP 与 Gradle 版本匹配
  • DeepSeek 各版本说明与优缺点分析
  • 《Agent Runtime 工程化》第一章 Agent Runtime 全景:1.6 动手任务
  • OpenClaw 安装后 Gateway 服务无法启动故障排查
  • C++ 与 Linux 基础:进程打开磁盘文件的内核实现与源码解析
  • OpenClaw 接入飞书机器人配置指南
  • AI 辅助编程全面指南:技巧、策略与最佳实践
  • MySQL 8.0.x 跨平台安装实战:Windows、CentOS、Ubuntu 配置详解
  • 2026 年 6 款主流免费 AI 写作工具实测与去 AI 味方案
  • 龙虾 AI(OpenClaw)部署与日常使用教程
  • 基于无人机搭载摄像头网络的交互式监控分布式方法
  • C/C++按位取反操作详解与代码示例
  • PyCharm 中 Copilot 无法调用 Claude 模型的解决方案
  • OpenClaw 联网工具配置指南:最大化 AI 实时信息获取
  • VSCode 远程 Copilot Claude 模型连接与 OpenRouter 断联修复
  • OpenClaw 接入飞书机器人与 Kimi2.5 配置指南
  • AI 产品经理与 AIGC 产品经理的区别及职业选择建议

相关免费在线工具

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online

  • Markdown转HTML

    将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online

  • HTML转Markdown

    将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online

  • JSON 压缩

    通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online

  • JSON美化和格式化

    将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online