【前端】CSS Flexbox布局示例介绍

CSS Flexbox(弹性盒子)简介

Flexbox 是一种一维布局模型,用于高效处理元素在容器 内的空间分配、对齐和排序。它通过父容器(flex container)和子元素(flex items)的配合实现灵活响应式布局。


核心概念

  1. 容器属性(作用于父元素)

    • display: flex | inline-flex;
    • flex-direction: 主轴方向(row | column | row-reverse | column-reverse)
    • flex-wrap: 换行(nowrap | wrap | wrap-reverse)
    • justify-content: 主轴对齐(center | space-between | space-around 等)
    • align-items: 交叉轴对齐(stretch | center | flex-start 等)
  2. 项目属性(作用于子元素)

    • order: 排序优先级(数值越小越靠前)
    • flex-grow: 放大比例(默认 0 不放大)
    • flex-shrink: 缩小比例(默认 1 可缩小)
    • flex-basis: 初始尺寸(auto | 长度值)
    • align-self: 覆盖父容器的 align-items

关键优势

  • 自适应布局:元素自动填充/收缩适应容器。
  • 简化对齐:无需浮动或定位即可实现居中/均分。
  • 响应式友好:结合媒体查询轻松适配不同屏幕。

💡 提示:Flexbox 适合局部布局(如导航、卡片列表),复杂网格布局建议使用 CSS Grid。

以下是整合后的Flexbox示例代码,每个示例均为完整HTML+CSS文件,可直接运行:

示例1:水平居中导航栏

html 复制代码
<!DOCTYPE html>
<html>
<head>
<style>
.navbar {
  display: flex;
  justify-content: center;
  gap: 20px;
  background: #333;
  padding: 10px;
}
.navbar > div {
  color: white;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background 0.3s;
}
.navbar > div:hover {
  background: #555;
  cursor: pointer;
}
</style>
</head>
<body>
<div class="navbar">
  <div>首页</div>
  <div>产品</div>
  <div>关于</div>
  <div>联系</div>
</div>
</body>
</html>

示例2:自适应两栏布局

html 复制代码
<!DOCTYPE html>
<html>
<head>
<style>
.container {
  display: flex;
  height: 200px;
  border: 1px solid #ccc;
  margin-top: 20px;
}
.sidebar {
  flex: 0 0 200px;
  background: #3498db;
  color: white;
  padding: 20px;
  font-weight: bold;
}
.content {
  flex: 1;
  background: #ecf0f1;
  padding: 20px;
}
</style>
</head>
<body>
<div class="container">
  <div class="sidebar">侧边栏导航</div>
  <div class="content">
    <h3>主要内容区</h3>
    <p>自适应填充剩余空间</p>
  </div>
</div>
</body>
</html>

示例3:响应式相册布局

html 复制代码
<!DOCTYPE html>
<html>
<head>
<style>
.gallery {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 15px;
  padding: 20px;
  background: #f5f5f5;
  margin-top: 20px;
}
.gallery > div {
  width: 120px;
  height: 120px;
  background: #e74c3c;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  transition: transform 0.3s;
}
.gallery > div:hover {
  transform: scale(1.05);
}
@media (max-width: 600px) {
  .gallery > div {
    width: 100px;
    height: 100px;
  }
}
</style>
</head>
<body>
<div class="gallery">
  <div>风景</div>
  <div>建筑</div>
  <div>人像</div>
  <div>美食</div>
  <div>动物</div>
  <div>夜景</div>
</div>
</body>
</html>

示例4:动态排序卡片

html 复制代码
<!DOCTYPE html>
<html>
<head>
<style>
.card-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 400px;
  margin-top: 20px;
}
.card {
  padding: 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #f9f9f9;
}
.card:nth-child(1) { order: 3; background: #ffebee; }
.card:nth-child(2) { order: 1; background: #e8f5e9; }
.card:nth-child(3) { order: 2; background: #e3f2fd; }
.card:nth-child(4) { order: 4; background: #fff8e1; }
</style>
</head>
<body>
<div class="card-container">
  <div class="card">卡片3 (order:3)</div>
  <div class="card">卡片1 (order:1)</div>
  <div class="card">卡片2 (order:2)</div>
  <div class="card">卡片4 (order:4)</div>
</div>
</body>
</html>

示例5:圣杯布局(页眉+主体+页脚)

html 复制代码
<!DOCTYPE html>
<html>
<head>
<style>
body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
header {
  background: #2c3e50;
  color: white;
  padding: 15px;
  text-align: center;
}
main {
  flex: 1;
  display: flex;
  padding: 20px;
  gap: 20px;
}
.content {
  flex: 1;
  background: #ecf0f1;
  padding: 20px;
  border-radius: 8px;
}
.sidebar {
  width: 200px;
  background: #3498db;
  color: white;
  padding: 20px;
  border-radius: 8px;
}
footer {
  background: #34495e;
  color: white;
  text-align: center;
  padding: 10px;
}
</style>
</head>
<body>
<header><h1>网站标题</h1></header>
<main>
  <div class="sidebar">
    <h3>侧边栏</h3>
    <p>导航菜单</p>
  </div>
  <div class="content">
    <h2>主要内容</h2>
    <p>使用flex:1自动填充剩余空间</p>
  </div>
</main>
<footer>© 2023 版权信息</footer>
</body>
</html>

关键特性演示:

  1. 容器属性 :display: flex、flex-direction、justify-content、align-items
  2. 项目属性 :flex、order、align-self
  3. 响应式 :结合flex-wrap和媒体查询
  4. 实用技巧:等分布局、垂直居中、间距控制

💡 提示:复制单个示例到HTML文件直接运行,或组合使用创建复杂布局。Flexbox特别适合构建组件级布局(导航栏、卡片、表单等),结合CSS Grid可实现完整的页面布局体系。

相关推荐
子兮曰2 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰2 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万3 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝3 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
三十而立洋3 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁3 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
汉堡大王95273 天前
Jev:不是聊天机器人, 而是一个智能 if 语句
前端·人工智能·后端
梦想很大很大3 天前
从运行事实到回归证据:Workrun 的 Telemetry 与 Evaluation 实践
前端·人工智能·后端
计算机魔术师3 天前
Meta Muse agent 接入 Shopify 的 Shop Pay 实现代理式购物
前端
沙蒿同学3 天前
我用 Go 搭了一条 AI Agent 流水线:从 1 张商品图到一整套淘宝详情页
前端·javascript·后端