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

css之Flex响应式多列布局,根据容器宽度自动调整显示2列或3列布局,支持多行排列

display:flex时,要如何设置才可以自适应多列,答案来了,很简单,之前居然看到这个还有人要收费才能看,也真的是有才。

废话不多说,直接上代码。


<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Flex响应式多列布局</title><style>* {margin: 0;padding: 0;box-sizing: border-box;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}body {background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);color: #333;padding: 20px;min-height: 100vh;display: flex;flex-direction: column;align-items: center;}.container {max-width: 1200px;width: 100%;margin: 20px auto;padding: 20px;}header {text-align: center;margin-bottom: 40px;color: white;text-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);}h1 {font-size: 2.8rem;margin-bottom: 15px;}.description {font-size: 1.2rem;max-width: 600px;margin: 0 auto;opacity: 0.9;}.controls {display: flex;justify-content: center;gap: 15px;margin: 30px 0;flex-wrap: wrap;}.btn {padding: 12px 25px;background: white;color: #2575fc;border: none;border-radius: 30px;cursor: pointer;font-weight: 600;transition: all 0.3s;box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);}.btn:hover {transform: translateY(-3px);box-shadow: 0 6px 15px rgba(0, 0, 0, 0.2);}.btn.active {background: #ff4081;color: white;}/* Flex容器样式 */.flex-container {display: flex;flex-wrap: wrap;gap: 20px;justify-content: center;}/* 默认3列布局 */.flex-item {flex: 1 1 calc(33.333% - 20px);min-width: 280px;background: white;border-radius: 12px;overflow: hidden;box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);transition: transform 0.3s, box-shadow 0.3s;display: flex;flex-direction: column;}.flex-item:hover {transform: translateY(-5px);box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);}.item-header {background: #4a6ee0;color: white;padding: 20px;text-align: center;font-size: 1.4rem;font-weight: 600;}.item-content {padding: 25px;flex-grow: 1;}.item-content p {line-height: 1.6;margin-bottom: 15px;color: #555;}.item-footer {background: #f8f9fa;padding: 15px 25px;text-align: right;border-top: 1px solid #eee;color: #777;font-size: 0.9rem;}/* 2列布局 */.layout-2 .flex-item {flex: 1 1 calc(50% - 20px);max-width: calc(50% - 20px);}/* 3列布局 */.layout-3 .flex-item {flex: 1 1 calc(33.333% - 20px);max-width: calc(33.333% - 20px);}/* 响应式设计 */@media (max-width: 900px) {.flex-item {flex: 1 1 calc(50% - 20px);max-width: calc(50% - 20px);}.layout-3 .flex-item {flex: 1 1 calc(50% - 20px);max-width: calc(50% - 20px);}}@media (max-width: 600px) {.flex-item {flex: 1 1 100%;max-width: 100%;}.layout-2 .flex-item,.layout-3 .flex-item {flex: 1 1 100%;max-width: 100%;}h1 {font-size: 2.2rem;}}.code-container {background: #2d2d2d;border-radius: 8px;padding: 20px;margin-top: 40px;color: #f8f8f2;font-family: 'Fira Code', monospace;overflow-x: auto;max-width: 100%;}.code-header {display: flex;justify-content: space-between;align-items: center;margin-bottom: 15px;color: #ffd700;}.code-content {line-height: 1.5;}.comment {color: #969896;}.selector {color: #a6e22e;}.property {color: #66d9ef;}.value {color: #ae81ff;}</style>
</head>
<body><div class="container"><header><h1>Flex响应式多列布局</h1><p class="description">根据容器宽度自动调整显示2列或3列布局,支持多行排列</p></header><div class="controls"><button class="btn active" data-cols="auto">自动调整</button><button class="btn" data-cols="2">强制2列</button><button class="btn" data-cols="3">强制3列</button></div><div class="flex-container layout-3"><div class="flex-item"><div class="item-header">Flex项目 1</div><div class="item-content"><p>Flex布局允许容器改变其子项的宽度/高度和顺序,以最佳方式填充可用空间。</p><p>Flex容器可以扩展子项以填充可用空间,或缩小它们以防止溢出。</p></div><div class="item-footer">响应式设计</div></div><div class="flex-item"><div class="item-header">Flex项目 2</div><div class="item-content"><p>使用flex-wrap属性控制Flex项目是否换行,以及如何换行。</p><p>默认情况下,Flex项目会尝试适应一行。您可以更改此设置并允许项目根据需要使用flex-wrap属性换行。</p></div><div class="item-footer">多行排列</div></div><div class="flex-item"><div class="item-header">Flex项目 3</div><div class="item-content"><p>使用flex属性作为flex-grow、flex-shrink和flex-basis的简写。</p><p>建议使用此简写而不是设置单独的属性。简写可以智能地设置其他值。</p></div><div class="item-footer">弹性比例</div></div><div class="flex-item"><div class="item-header">Flex项目 4</div><div class="item-content"><p>媒体查询可以针对不同的设备屏幕尺寸应用不同的样式规则。</p><p>使用@media规则在样式表的其他部分包含一组CSS属性,前提是满足特定条件。</p></div><div class="item-footer">媒体查询</div></div><div class="flex-item"><div class="item-header">Flex项目 5</div><div class="item-content"><p>gap属性在网格布局、Flex布局和多列布局中定义行和列之间的间距。</p><p>它是row-gap和column-gap的简写,可以同时设置行和列之间的间距。</p></div><div class="item-footer">间距控制</div></div><div class="flex-item"><div class="item-header">Flex项目 6</div><div class="item-content"><p>响应式设计使您的网页在所有设备上看起来都很棒。</p><p>响应式设计仅使用HTML和CSS,不是程序或JavaScript。</p></div><div class="item-footer">自适应布局</div></div></div><div class="code-container"><div class="code-header"><h3>CSS代码示例</h3><span>Flex布局核心代码</span></div><div class="code-content"><span class="comment">/* Flex容器 */</span><br><span class="selector">.flex-container</span> {<br>&nbsp;&nbsp;<span class="property">display</span>: <span class="value">flex</span>;<br>&nbsp;&nbsp;<span class="property">flex-wrap</span>: <span class="value">wrap</span>;<br>&nbsp;&nbsp;<span class="property">gap</span>: <span class="value">20px</span>;<br>&nbsp;&nbsp;<span class="property">justify-content</span>: <span class="value">center</span>;<br>}<br><br><span class="comment">/* 默认3列布局 */</span><br><span class="selector">.flex-item</span> {<br>&nbsp;&nbsp;<span class="property">flex</span>: <span class="value">1 1 calc(33.333% - 20px)</span>;<br>&nbsp;&nbsp;<span class="property">min-width</span>: <span class="value">280px</span>;<br>}<br><br><span class="comment">/* 2列布局 */</span><br><span class="selector">.layout-2 .flex-item</span> {<br>&nbsp;&nbsp;<span class="property">flex</span>: <span class="value">1 1 calc(50% - 20px)</span>;<br>&nbsp;&nbsp;<span class="property">max-width</span>: <span class="value">calc(50% - 20px)</span>;<br>}<br><br><span class="comment">/* 媒体查询 - 中等屏幕 */</span><br>@<span class="value">media</span> (<span class="property">max-width</span>: <span class="value">900px</span>) {<br>&nbsp;&nbsp;<span class="selector">.flex-item</span> {<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="property">flex</span>: <span class="value">1 1 calc(50% - 20px)</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="property">max-width</span>: <span class="value">calc(50% - 20px)</span>;<br>&nbsp;&nbsp;}<br>}<br><br><span class="comment">/* 媒体查询 - 小屏幕 */</span><br>@<span class="value">media</span> (<span class="property">max-width</span>: <span class="value">600px</span>) {<br>&nbsp;&nbsp;<span class="selector">.flex-item</span> {<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="property">flex</span>: <span class="value">1 1 100%</span>;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="property">max-width</span>: <span class="value">100%</span>;<br>&nbsp;&nbsp;}<br>}</div></div></div><script>// 切换布局功能document.querySelectorAll('.btn').forEach(button => {button.addEventListener('click', function() {// 更新按钮状态document.querySelectorAll('.btn').forEach(btn => {btn.classList.remove('active');});this.classList.add('active');// 获取布局类型const cols = this.getAttribute('data-cols');const container = document.querySelector('.flex-container');// 移除现有布局类container.classList.remove('layout-2', 'layout-3');// 添加新布局类if (cols !== 'auto') {container.classList.add('layout-' + cols);}});});</script>
</body>
</html>

关键点说明

  1. display: flex - 创建Flex容器

  2. flex-wrap: wrap - 允许项目换行

  3. gap: 20px - 设置项目之间的间距

  4. flex: 1 1 calc(33.333% - 20px) - 设置项目弹性比例和基础宽度

    • 第一个值:flex-grow(扩展比例)

    • 第二个值:flex-shrink(收缩比例)

    • 第三个值:flex-basis(基础宽度)

  5. 媒体查询 - 根据屏幕宽度调整布局

这个布局会根据容器宽度自动调整,宽时显示3列,窄时显示2列,非常窄时显示1列。您可以通过顶部的按钮手动切换布局模式。


文章转载自:

http://vUqk4vTQ.qzcLh.cn
http://08AW5lAP.qzcLh.cn
http://DqmGtOpM.qzcLh.cn
http://RrpfkhnK.qzcLh.cn
http://dj7STMWm.qzcLh.cn
http://5HKDJwff.qzcLh.cn
http://LQrrbZtF.qzcLh.cn
http://d9UcpcPr.qzcLh.cn
http://MQCoHPpg.qzcLh.cn
http://MQyJEIct.qzcLh.cn
http://iSk2XkXN.qzcLh.cn
http://FmpJQVzD.qzcLh.cn
http://ZdiRIeQm.qzcLh.cn
http://3S4hMg8M.qzcLh.cn
http://biLQkzMl.qzcLh.cn
http://TuNUvzT2.qzcLh.cn
http://8LyeqtRD.qzcLh.cn
http://hHuMDR1m.qzcLh.cn
http://XorUvePj.qzcLh.cn
http://o6ESacir.qzcLh.cn
http://HAwmkWHC.qzcLh.cn
http://qjkDg2t5.qzcLh.cn
http://GuVoWAlS.qzcLh.cn
http://PJsYc97O.qzcLh.cn
http://jSwRCStq.qzcLh.cn
http://UFTFIN8G.qzcLh.cn
http://OYqa3X26.qzcLh.cn
http://pHQQPr2V.qzcLh.cn
http://W7QDxvg6.qzcLh.cn
http://Vl0UupBg.qzcLh.cn
http://www.dtcms.com/a/386288.html

相关文章:

  • HTML应用指南:利用POST请求获取全国中石化易捷门店位置信息
  • PDF24 Creator:免费全能的PDF处理工具
  • 小程序交互与一些技术总结
  • Spring Cloud - 面试知识点(负载均衡)
  • 易特ERP软件局域网版安装教程
  • qt QBoxSet详解
  • 电脑散热风扇有噪音怎么解决
  • 行业分享丨汽车电磁兼容仿真技术与应用
  • 缓存与数据库一致性的4大坑及终极解决方案
  • 机器学习面试题:请讲一讲分类评估方式?
  • 【pure-admin】前端使用pure-admin后台管理系统框架,后端使用FastAPI的前端向后端加密发送用户登录密码的完整示例
  • 从 Node.js 安装到 Vue 3 开发环境搭建
  • Python单元测试框架之pytest -- 生成测试报告
  • 使用HBuilderX新建uniapp项目
  • 医疗行业安全合规数据管理平台:构建高效协作与集中化知识沉淀的一体化解决方案
  • 从一次鼠标点击窥探操作系统内核:中断、驱动、IPC与内存安全的奇幻之旅
  • 【超详细】C#的单例模式
  • 加快 NoETL 数据工程实践, Aloudata 荣登《2025 中国数智化转型升级创新服务企业》榜单
  • 香港服务器CN2带宽价格多少钱?很贵吗?
  • 180 课时吃透 Go 语言游戏后端系列1:第一个Go程序
  • MSI 与 IOAPIC LAPIC 如何协作,操作系统如何初始化和使用他们
  • 数据库优化(六)安全字段脱敏设计—东方仙盟金丹期
  • java21学习笔记
  • 大厂综合题库解析
  • 算法奇妙屋(2)-模拟
  • 贪心算法应用:区间调度问题详解
  • js中异步编程的实现方式【详细】
  • 详解 ArduPilot:开源无人机自动驾驶系统的全方位解析
  • 分页查询:时间筛选+日期筛选+增加queryWrapper 筛选条件
  • 通透理清三级缓存--看Spring是如何解决循环依赖的