【前端】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可实现完整的页面布局体系。

相关推荐
小信丶9 分钟前
解决 pnpm dev 报错:系统禁止运行脚本的问题
前端·vue.js·windows·npm
૮・ﻌ・16 分钟前
Vue3:组合式API、Vue3.3新特性、Pinia
前端·javascript·vue3
前端不太难17 分钟前
RN + TypeScript 项目越写越乱?如何规范架构?
前端·javascript·typescript
神算大模型APi--天枢64617 分钟前
全栈自主可控:国产算力平台重塑大模型后端开发与部署生态
大数据·前端·人工智能·架构·硬件架构
苏打水com18 分钟前
第十五篇:Day43-45 前端性能优化进阶——从“可用”到“极致”(对标职场“高并发场景优化”需求)
前端·css·vue·html·js
@大迁世界25 分钟前
08.CSS if() 函数
前端·css
Moment32 分钟前
小米不仅造车,还造模型?309B参数全开源,深度思考完胜DeepSeek 🐒🐒🐒
前端·人工智能·后端
苏打水com35 分钟前
第十六篇:Day46-48 前端安全进阶——从“漏洞防范”到“安全体系”(对标职场“攻防实战”需求)
前端·javascript·css·vue.js·html
5C2437 分钟前
从思想到实践:前端工程化体系与 Webpack 构建架构深度解析
前端·前端工程化
咕噜企业分发小米43 分钟前
如何平衡服务器内存使用率和系统稳定性?
java·服务器·前端