【前端】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: flexflex-directionjustify-contentalign-items
  2. 项目属性flexorderalign-self
  3. 响应式 :结合flex-wrap和媒体查询
  4. 实用技巧:等分布局、垂直居中、间距控制

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

相关推荐
夏幻灵32 分钟前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_1 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝1 小时前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions1 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发1 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_1 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞051 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、1 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao1 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
杨超越luckly1 小时前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强