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

相关推荐
剪刀石头布啊1 分钟前
js常见的单例
前端·javascript
剪刀石头布啊1 分钟前
数据口径
前端·后端·程序员
剪刀石头布啊5 分钟前
http状态码大全
前端·后端·程序员
剪刀石头布啊7 分钟前
iframe通信、跨标签通信的常见方案
前端·javascript·html
宇之广曜16 分钟前
搭建 Mock 服务,实现前端自调
前端·mock
yuko093118 分钟前
【手机验证码】+86垂直居中的有趣问题
前端
用户15129054522022 分钟前
Springboot中前端向后端传递数据的几种方式
前端
阿星做前端22 分钟前
如何构建一个自己的 Node.js 模块解析器:node:module 钩子详解
前端·javascript·node.js
用户15129054522026 分钟前
Web Worker:让前端飞起来的隐形引擎
前端
司宸26 分钟前
学习笔记十 —— 自定义hooks设计原则 笔试实现
前端