0基础进大厂,第22天 : CSS中的定位布局,你的.container还找不到位置吗?

引言

今天来聊聊CSS中一个超级实用的功能------定位布局。废话不多说,直接进入正题!

基础篇------定位类型

CSS中有5种定位方式,但最常用的就3种:staticrelativeabsolute。还有两个特殊成员:fixedsticky

1. 静态定位(static)

这是默认值,元素按照正常文档流排列。

css 复制代码
.box {
  position: static; /* 可以省略不写 */
}

注意:设置了static定位的元素,toprightbottomleftz-index属性都无效!

2. 相对定位(relative)

元素先放在正常位置,然后相对于自己原来的位置进行偏移。

css 复制代码
.box {
  position: relative;
  top: 20px;
  left: 30px;
}

看个实际例子:

html 复制代码
<div class="box">我是相对定位的盒子</div>
<div>我是普通盒子</div>
css 复制代码
.box {
  position: relative;
  top: 20px;
  left: 30px;
  background: pink;
}

效果:粉色的盒子会向下移动20px,向右移动30px,但原来的位置仍然保留着,不会影响其他元素。

3. 绝对定位(absolute)

元素脱离文档流,相对于最近的已定位祖先元素(非static)定位。

css 复制代码
.box {
  position: absolute;
  top: 0;
  right: 0;
}

看个案例:

html 复制代码
<div class="parent">
  <div class="child">我是绝对定位的子元素</div>
</div>
css 复制代码
.parent {
  position: relative; /* 关键! */
  width: 300px;
  height: 200px;
  border: 1px solid #ccc;
}

.child {
  position: absolute;
  bottom: 10px;
  right: 10px;
  background: lightblue;
}

效果:浅蓝色的子元素会定位在父元素的右下角,距离底部和右边各10px。

4. 固定定位(fixed)

元素相对于视口定位,滚动页面时位置不变。

css 复制代码
.header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  background: white;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

5. 粘性定位(sticky)

元素在滚动到特定位置时变成固定定位。

css 复制代码
.nav {
  position: sticky;
  top: 0;
  background: white;
}

进阶篇------定位实战

案例一:模态框

html 复制代码
<div class="modal">
  <div class="modal-content">
    <h2>我是模态框</h2>
    <p>点击外部区域关闭</p>
  </div>
</div>
css 复制代码
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  background: white;
  padding: 20px;
  border-radius: 5px;
  width: 80%;
  max-width: 500px;
}

案例二:悬浮按钮

html 复制代码
<button class="float-btn">+</button>
css 复制代码
.float-btn {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: #2196F3;
  color: white;
  font-size: 24px;
  border: none;
  box-shadow: 0 2px 5px rgba(0,0,0,0.3);
  cursor: pointer;
}

案例三:粘性导航栏

html 复制代码
<header class="sticky-header">
  <nav>
    <a href="#">首页</a>
    <a href="#">产品</a>
    <a href="#">关于</a>
  </nav>
</header>
<div class="content">
  <!-- 很多内容 -->
</div>
css 复制代码
.sticky-header {
  position: sticky;
  top: 0;
  background: white;
  padding: 15px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

常见问题

1. 绝对定位的元素宽度问题

绝对定位的元素默认宽度是内容宽度,不是100%父元素宽度。如果需要撑满父元素:

css 复制代码
.box {
  position: absolute;
  left: 0;
  right: 0; /* 这样宽度就是100%了 */
}

2. z-index堆叠问题

定位元素可以使用z-index控制堆叠顺序,但要注意:

  • 只对定位元素(非static)有效
  • 父元素的z-index会影响子元素

3. 绝对定位的居中技巧

css 复制代码
.box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

总结

  • static:默认值,不定位
  • relative:相对自身定位,不脱离文档流
  • absolute:相对于最近定位祖先定位,脱离文档流
  • fixed:相对于视口定位,脱离文档流
  • sticky:滚动到阈值时变为固定定位

记住这些,你的页面布局能力就能提升一大截!

相关推荐
天平6 小时前
油猴脚本创建webworker踩坑记录
前端·javascript·typescript
原则猫7 小时前
前端基础大厦
前端
陈随易8 小时前
编程语言级别的Skill市场,AI Agent 的未来形态
前端·后端·程序员
SoaringHeart9 小时前
Flutter进阶:基于 EasyRefresh 的下拉刷新封装 n_easy_refresh_mixin.dart
前端·flutter
IT_陈寒11 小时前
Vite的热更新突然不香了,排查三小时差点砸键盘
前端·人工智能·后端
子兮曰11 小时前
Agency-Agents 深度解析:400+ AI 专家的"梦之队"如何重塑开发工作流
前端·后端·vibecoding
山河木马12 小时前
渲染管线-计算得到gl_Position(顶点着色器)之后续GPU流程
javascript·webgl·图形学
竹林81812 小时前
用 The Graph 查询链上数据实战:从手搓 RPC 到 Subgraph,我的 NFT 项目数据加载快了 10 倍
前端·javascript
妙码生花12 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十九):点选验证码代码逐行目检
前端·后端·go
Awu122713 小时前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude