当前位置: 首页 > news >正文

【前端】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:水平居中导航栏

<!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:自适应两栏布局

<!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:响应式相册布局

<!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:动态排序卡片

<!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:圣杯布局(页眉+主体+页脚)

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

http://www.lryc.cn/news/606757.html

相关文章:

  • CSS组件化样式新篇章:@scope
  • SystemVerilog的系统函数和任务
  • 无图形界面的CentOS 7网络如何配置
  • 【0基础PS】PS工具详解--仿制图章工具
  • OpenGL Camera
  • socket编程-UDP(2)-设计翻译系统
  • 中英混合的语音识别XPhoneBERT 监督的音频到音素的编码器结合 f0 特征LID
  • 【LeetCode】算法详解#11 ---相交链表
  • 《Java 程序设计》核心知识点梳理与深入探究
  • 深入理解C语言指针:从回调函数到数组指针笔试题全解析(下)
  • Canny边缘检测算法-个人记录
  • 【世纪龙科技】3D交互深度赋能-汽车整车维护仿真教学软件
  • 汽车供应链PPAP自动化审核指南:如何用AI实现规则精准匹配与文件智能校验
  • 【世纪龙科技】汽车整车维护仿真教学软件-智构整车维护实训
  • 目标检测检出率,误检率,ap,map等评估python代码
  • 防火墙安全策略实验一
  • 分类预测 | Matlab实现CPO-PNN冠豪猪算法优化概率神经网络多特征分类预测
  • Redis学习-----Redis的基本数据类型
  • 数学与应用数学的区别是什么
  • CSS font-weight:500不生效
  • Mysql join语句
  • 智慧能源管理平台的多层协同控制架构研究
  • ansible 在EE 容器镜像中运行
  • 在SQL SERVER 中,用SSMS 实现存储过程的每日自动调用
  • 守护数字核心:主机安全的重要性与全方位防护指南
  • Java 根据多个 MM-dd 日期计算总时长(包含当日和次日)
  • 新手小白如何快速检测IP 的好坏?
  • OSPF笔记整理
  • JavaWeb(苍穹外卖)--学习笔记16(定时任务工具Spring Task,Cron表达式)
  • 电子电气架构 --- 加速48V技术应用的平衡之道