it编程 > 网页制作 > html5

HTML5 定位大全之相对定位、绝对定位和固定定位

45人参与 2025-05-14 html5

  在html5和css中,定位(positioning)是控制元素在页面上位置的重要机制。主要有四种定位方式:静态定位(static)、相对定位(relative)、绝对定位(absolute)和固定定位(fixed)。下面我将详细讲解这三种非静态定位方式,并提供相应的源代码示例。

1. 相对定位 (relative positioning)

特点:

<!doctype html>
<html>
<head>
<style>
.relative-box {
  position: relative;
  left: 50px;
  top: 20px;
  width: 200px;
  height: 100px;
  background-color: lightblue;
  border: 2px solid blue;
}
</style>
</head>
<body>
<h2>相对定位示例</h2>
<p>这是一个普通段落。</p>
<div class="relative-box">这个div使用了相对定位,向右移动50px,向下移动20px。</div>
<p>注意相对定位元素原来的空间仍然保留。</p>
</body>
</html>

2. 绝对定位 (absolute positioning)

特点:

<!doctype html>
<html>
<head>
<style>
.container {
  position: relative;
  width: 400px;
  height: 200px;
  background-color: #f0f0f0;
  border: 2px solid gray;
}
.absolute-box {
  position: absolute;
  right: 20px;
  bottom: 10px;
  width: 150px;
  height: 80px;
  background-color: lightcoral;
  border: 2px solid red;
}
</style>
</head>
<body>
<h2>绝对定位示例</h2>
<div class="container">
  这是一个相对定位的容器
  <div class="absolute-box">这个div使用了绝对定位,相对于容器定位在右下角。</div>
</div>
</body>
</html>

3. 固定定位 (fixed positioning)

特点:

<!doctype html>
<html>
<head>
<style>
.fixed-box {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: 100px;
  height: 50px;
  background-color: lightgreen;
  border: 2px solid green;
  text-align: center;
  line-height: 50px;
}
</style>
</head>
<body>
<h2>固定定位示例</h2>
<p>向下滚动页面,右下角的按钮会固定在相同位置。</p>
<div style="height: 2000px;">很多内容...</div>
<div class="fixed-box">固定按钮</div>
</body>
</html>

三种定位方式的主要区别

特性相对定位 (relative)绝对定位 (absolute)固定定位 (fixed)
参照物自身原始位置最近的已定位祖先浏览器窗口
文档流保留原空间脱离文档流脱离文档流
滚动影响随页面滚动随祖先元素滚动不随页面滚动
常见用途微调元素位置、作为定位上下文弹出层、浮动元素导航栏、固定按钮
z-index可应用可应用可应用
<!doctype html>
<html>
<head>
<style>
body {
  font-family: arial, sans-serif;
  margin: 0;
  padding: 20px;
}
.container {
  position: relative;
  width: 80%;
  height: 300px;
  margin: 30px auto;
  background-color: #f5f5f5;
  border: 2px dashed #333;
  padding: 20px;
}
.relative-box {
  position: relative;
  left: 50px;
  top: 20px;
  width: 200px;
  height: 100px;
  background-color: rgba(173, 216, 230, 0.7);
  border: 2px solid blue;
}
.absolute-box {
  position: absolute;
  right: 30px;
  top: 50px;
  width: 150px;
  height: 80px;
  background-color: rgba(240, 128, 128, 0.7);
  border: 2px solid red;
}
.fixed-box {
  position: fixed;
  left: 20px;
  top: 20px;
  width: 120px;
  height: 60px;
  background-color: rgba(144, 238, 144, 0.7);
  border: 2px solid green;
  text-align: center;
  line-height: 60px;
}
.sticky-box {
  position: sticky;
  top: 10px;
  width: 100%;
  height: 50px;
  background-color: rgba(255, 255, 0, 0.7);
  border: 2px solid orange;
  text-align: center;
  line-height: 50px;
  margin-top: 20px;
}
.long-content {
  height: 1500px;
  margin-top: 30px;
  padding: 20px;
  background-color: #eee;
}
</style>
</head>
<body>
<div class="fixed-box">固定定位</div>
<h1>css定位方式演示</h1>
<div class="sticky-box">粘性定位(sticky)</div>
<div class="container">
  <div class="relative-box">相对定位</div>
  <div class="absolute-box">绝对定位</div>
  <p>这是一个相对定位的容器,包含相对定位和绝对定位的元素。</p>
</div>
<div class="long-content">
  <p>向下滚动页面,观察不同定位元素的行为...</p>
  <p>固定定位元素始终在窗口固定位置。</p>
  <p>粘性定位元素在到达指定位置时会粘住。</p>
</div>
</body>
</html>

到此这篇关于html5 定位详解:相对定位、绝对定位和固定定位的文章就介绍到这了,更多相关html5相对定位、绝对定位和固定定位内容请搜索代码网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

HTML5无插件拖拽图片上传功能实现过程

05-16

Ubuntu 25.10将启用两款新默认应用: Loupe图像查看器和Ptyxis终端

05-17

html5的响应式布局的方法示例详解

04-21

HTML5表格语法格式详解

04-21

HTML5中的Microdata与历史记录管理详解

04-21

任天堂真有你的!部分游戏卡带不能插上即玩!还得下载!

04-08

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论