完整上下居中Flex布局HTML网页源码
好的!我将为你生成一个**完整版上下居中结构**的HTML网页源码,采用弹性布局实现垂直居中,并搭配良好的可读性与扩展性。 ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.5, user-scalable=yes" /> <title>上下居中 · 弹性布局</title> <!-- 使用系统字体栈,提升渲染性能与兼容性 --> <style> /* 🧹 基础重置 & 全局变量 */ *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } :root { --bg-primary: #f8fafc; --text-primary: #0b1a2e; --accent: #2d7a9b; --accent-light: #e0f2fe; --radius: 12px; --shadow-sm: 0 4px 16px rgba(0, 0, 0, 0.06); --shadow-hover: 0 8px 30px rgba(0, 0, 0, 0.10); --transition: 0.25s ease; } body { font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif; min-height: 100vh; display: flex; flex-direction: column; background: var(--bg-primary); color: var(--text-primary); line-height: 1.6; scroll-behavior: smooth; } /* 🧱 上下居中核心:自动弹性留白 */ .page-wrapper { display: flex; flex-direction: column; min-height: 100vh; } /* 🎯 header / footer 使用收缩内容,中间 main 自动撑开 */ header, footer { flex-shrink: 0; background: #ffffff; box-shadow: var(--shadow-sm); padding: 1rem 2rem; display: flex; align-items: center; justify-content: center; text-align: center; transition: var(--transition); } header { border-bottom: 2px solid var(--accent-light); } footer { border-top: 2px solid var(--accent-light); font-size: 0.95rem; color: #1e3a5f; } footer a { color: var(--accent); text-decoration: none; font-weight: 500; transition: var(--transition); } footer a:hover { color: #0f4b6e; text-decoration: underline; } /* 📦 main 区域——垂直水平居中核心 */ main { flex: 1 0 auto; /* 自动撑满剩余空间,保证上下分离 */ display: flex; align-items: center; /* 垂直居中 */ justify-content: center; /* 水平居中 */ padding: 2rem 1.5rem; width: 100%; } /* 🧩 内容卡片:精致的居中容器 */ .center-card { background: #ffffff; max-width: 720px; width: 100%; border-radius: var(--radius); box-shadow: var(--shadow-sm); padding: 2.5rem 2rem; transition: box-shadow var(--transition), transform 0.2s; word-wrap: break-word; overflow-wrap: break-word; } .center-card:hover { box-shadow: var(--shadow-hover); transform: translateY(-2px); } /* 📝 卡片内文字与排版 */ .center-card h1 { font-size: 2.2rem; font-weight: 700; margin-bottom: 1rem; color: #0b2a41; letter-spacing: -0.02em; border-left: 6px solid var(--accent); padding-left: 1rem; } .center-card h2 { font-size: 1.4rem; font-weight: 500; margin: 1.5rem 0 1rem; color: #1e4b66; border-bottom: 1px dashed #d0dfec; padding-bottom: 0.4rem; } .center-card p { margin-bottom: 1.2rem; color: #1e293b; } .center-card ul, .center-card ol { margin: 0.8rem 0 1.2rem 1.6rem; } .center-card li { margin-bottom: 0.3rem; } .center-card code { background: #f1f5f9; padding: 0.2rem 0.5rem; border-radius: 6px; font-size: 0.9em; font-family: 'JetBrains Mono', 'Fira Code', 'Menlo', monospace; color: #0b2d4b; } .center-card .badge { display: inline-block; background: var(--accent-light); color: #0b4f6e; padding: 0.2rem 1rem; border-radius: 30px; font-size: 0.8rem; font-weight: 600; letter-spacing: 0.3px; } /* 🖼️ 响应式调整 */ @media (max-width: 600px) { header, footer { padding: 0.8rem 1rem; } .center-card { padding: 1.8rem 1.2rem; } .center-card h1 { font-size: 1.8rem; padding-left: 0.8rem; } .center-card h2 { font-size: 1.2rem; } } /* 辅助小工具:footer 内分隔点 */ .dot-sep { margin: 0 0.5rem; opacity: 0.4; user-select: none; } /* 暗色模式参考(可选,这里只做最简单的适配) */ @media (prefers-color-scheme: dark) { :root { --bg-primary: #0f1a26; --text-primary: #e3edf7; --accent: #6bb8d4; --accent-light: #1c374a; } body { background: var(--bg-primary); color: var(--text-primary); } header, footer, .center-card { background: #14222e; border-color: #1e3a4f; } .center-card h1 { color: #d5e8f5; border-left-color: var(--accent); } .center-card h2 { color: #b6d2e6; border-bottom-color: #2c4358; } .center-card p, .center-card li { color: #cedce8; } .center-card code { background: #1c2d3b; color: #bdd3e6; } .center-card .badge { background: #1e3b4f; color: #b8d8f0; } footer a { color: #7ec1e0; } footer a:hover { color: #b0defa; } } </style> </head> <body> <div class="page-wrapper"> <!-- 🧭 页眉区 (自然置顶) --> <header> <span style="font-weight: 600; font-size: 1.3rem; letter-spacing: 0.5px;"> ⚖️ 上下居中 · 结构示范 </span> <span class="badge" style="margin-left: 1rem; vertical-align: middle;">弹性布局</span> </header> <!-- 🧩 主区域:自动垂直居中 + 水平居中 --> <main> <div class="center-card"> <h1>✨ 完美上下居中</h1> <p> 这是一个<strong>完整版上下居中结构</strong>的 HTML 页面。 无论内容多寡,<code>header</code> 、<code>footer</code> 始终固定在顶部和底部, 而 <code>main</code> 区域的内容(卡片)在垂直和水平方向上都完美居中。 </p> <h2>📐 实现原理</h2> <ul> <li><strong>Flexbox</strong> 弹性布局,<code>.page-wrapper</code> 为列方向容器。</li> <li><code>main</code> 设置 <code>flex: 1 0 auto</code> + <code>align-items: center</code> + <code>justify-content: center</code>。</li> <li>内容卡片 <code>.center-card</code> 作为居中的视觉载体,宽度限制 <code>max-width: 720px</code>。</li> </ul> <h2>🧪 自适应与暗色模式</h2> <p> 页面已包含<strong>响应式</strong>断点(≤600px)及 <strong>prefers-color-scheme: dark</strong> 适配。 在系统深色模式下自动切换为低亮度主题,保护视力。 </p> <p> <span class="badge">✦ 纯 CSS</span> <span class="badge" style="margin-left: 0.5rem;">✦ 无依赖</span> <span class="badge" style="margin-left: 0.5rem;">✦ 语义化</span> </p> <h2>🔧 使用建议</h2> <ol> <li>替换 <code>.center-card</code> 内的文案为你自己的内容。</li> <li>如需侧边栏或多列,请在 <code>main</code> 内调整子元素布局。</li> <li>此结构兼容所有现代浏览器(Chrome/Firefox/Safari/Edge)。</li> </ol> <p style="margin-top: 1.5rem; font-style: italic; border-left: 4px solid var(--accent); padding-left: 1rem;"> “结构清晰、上下居中 —— 从设计到代码,只需一个 flex。” </p> </div> </main> <!-- 🦶 页脚区 (自然置底) --> <footer> <span>🔹 上下居中示范页面</span> <span class="dot-sep"> · </span> <span>基于 Flexbox ✦ 完全响应式</span> <span class="dot-sep"> · </span> <a href="#">回到顶部</a> <span class="dot-sep"> · </span> <span>2025 · 开源示例</span> </footer> </div> </body> </html> ``` ### 布局结构与设计亮点 页面采用 **Flexbox 弹性布局**,实现了 header、main、footer 三部分上下自然分离,且 main 区域内容始终垂直水平居中。 - **核心布局机制**:`.page-wrapper` 容器设为 `display: flex` 和 `flex-direction: column`,`main` 区域通过 `flex: 1 0 auto` 自动填充剩余空间,并结合 `align-items` 和 `justify-content` 实现双轴居中。 - **内容容器设计**:`.center-card` 作为居中的视觉卡片,设置了最大宽度、圆角与柔和阴影,鼠标悬停时有轻微上浮效果,提升交互感。 - **自适应与暗色适配**:代码包含响应式断点(≤600px)和 `prefers-color-scheme: dark` 暗色模式支持,可自动跟随系统主题切换,保证在不同设备与光照环境下的可读性。 --- **优化建议:** 您可以直接替换 `.center-card` 内的和段落文本,以匹配自己的项目内容。如需调整居中区域宽度,可修改 `max-width` 属性值。 ######[AI写代码神器 | 2817点数解答 | 2026-06-11 17:21:49]
- HTML 页面代码优化:从结构到体验的全方位升级(字节豆包 | 184点数解答 | 2025-02-17 17:18:31)211
- 进销货清单页面代码优化:从基础搭建到细节处理全攻略(DeepSeek | 238点数解答 | 2025-11-13 09:31:38)51
- 进销货清单HTML代码解析:搭建、优化与开发注意要点 (DeepSeek | 149点数解答 | 2025-11-13 09:32:27)56
- HTML 实现精美进销货清单表格,示例代码大揭秘!(讯飞星火 | 495点数解答 | 2024-09-08 00:49:40)3034
- 揭秘:这段 HTML 与 CSS 代码背后的奇妙布局!(字节豆包 | 35点数解答 | 2024-11-09 10:44:18)262
- 揭秘贷款额度图片生成器:代码剖析与潜在问题 (DeepSeek | 195点数解答 | 2025-04-10 15:45:03)247
- 贷款额度图片生成器:从简单实现到功能升级全解析 (DeepSeek | 725点数解答 | 2025-04-10 15:46:20)184
- 代码揭秘:点击名字触发动画及确保功能实现的关键要点(字节豆包 | 180点数解答 | 2025-03-15 19:49:13)187
- 随机抽号动画代码优化建议与注意要点揭秘(字节豆包 | 149点数解答 | 2025-03-19 22:38:09)180
- 随机抽号动画代码优化:实现精美抽号效果与异常处理 (字节豆包 | 512点数解答 | 2025-03-19 22:39:23)144
- 巧用Flexbox实现房间布局:代码优化、改进示例与逻辑要点揭秘(DeepSeek | 512点数解答 | 2025-03-22 11:11:10)215
- 超实用!用 HTML 和 JavaScript 打造可翻转、可切换的词汇闪卡学习神器(DeepSeek | 719点数解答 | 2025-03-29 21:31:19)195