前端响应式通用 CSS(Flex 为主,含主色调)

以下是一套以 Flex 布局为核心的响应式通用 CSS,封装了主色调、间距、常用工具类和 12 列栅格。直接复制使用或按需引用即可。


设计思路

  • CSS 变量 管理主色调、文字色、圆角、阴影、断点,换肤只需改 :root 里几个值。
  • Flex 原子类 覆盖方向、换行、主轴/交叉轴对齐、弹性伸缩等,组合拼装大部分布局。
  • 响应式工具 采用移动优先,通过 sm / md / lg / xl / xxl 断点修饰类名,实现不同屏幕下改变 Flex 方向、显示/隐藏、对齐方式。
  • 间距与栅格 提供 m-* / p-* 快捷间距,以及基于 Flex 的 12 列响应式栅格,弥补纯 Flex 工具类在比例划分上的不足。
  • 组件示例 提供 .btn 等基础组件,直接使用主色调变量,快速统一风格。

使用示例(HTML 片段)

html 复制代码
<!-- 主色调按钮 -->
<button class="btn btn-primary">主要按钮</button>

<!-- 水平居中布局,移动端竖向、sm 以上横向 -->
<div class="flex flex-column flex-sm-row justify-center items-center gap-3">
  <div class="card">卡片1</div>
  <div class="card">卡片2</div>
</div>

<!-- 12 列栅格:移动端全宽,md 以上各占一半 -->
<div class="row">
  <div class="col-12 col-md-6">左侧</div>
  <div class="col-12 col-md-6">右侧</div>
</div>

完整 CSS 代码

css 复制代码
/* ===========================================
   前端响应式通用 CSS(Flex 为主,含主色调)
   =========================================== */

/* ------- 1. CSS 变量 / 主色调 ------- */
:root {
  /* 主色调 */
  --color-primary: #3b82f6;
  --color-primary-hover: #2563eb;
  --color-primary-light: #dbeafe;
  --color-secondary: #10b981;
  --color-secondary-hover: #059669;
  --color-warning: #f59e0b;
  --color-danger: #ef4444;

  /* 中性色 */
  --color-text: #1f2937;
  --color-text-secondary: #6b7280;
  --color-bg: #ffffff;
  --color-bg-gray: #f9fafb;
  --color-border: #e5e7eb;

  /* 间距 */
  --space-1: 0.25rem;   /* 4px */
  --space-2: 0.5rem;    /* 8px */
  --space-3: 1rem;      /* 16px */
  --space-4: 1.5rem;    /* 24px */
  --space-5: 2rem;      /* 32px */
  --space-6: 3rem;      /* 48px */

  /* 圆角 & 阴影 */
  --radius: 0.5rem;
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);

  /* 响应式断点 */
  --bp-sm: 576px;
  --bp-md: 768px;
  --bp-lg: 992px;
  --bp-xl: 1200px;
  --bp-xxl: 1400px;
}

/* ------- 2. 全局重置 ------- */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

img {
  max-width: 100%;
  display: block;
}

/* ------- 3. Flex 布局工具类 ------- */
.flex            { display: flex; }
.inline-flex     { display: inline-flex; }

/* 方向 */
.flex-row         { flex-direction: row; }
.flex-row-reverse { flex-direction: row-reverse; }
.flex-column      { flex-direction: column; }
.flex-column-reverse { flex-direction: column-reverse; }

/* 换行 */
.flex-wrap        { flex-wrap: wrap; }
.flex-nowrap      { flex-wrap: nowrap; }

/* 主轴对齐 */
.justify-start    { justify-content: flex-start; }
.justify-end      { justify-content: flex-end; }
.justify-center   { justify-content: center; }
.justify-between  { justify-content: space-between; }
.justify-around   { justify-content: space-around; }
.justify-evenly   { justify-content: space-evenly; }

/* 交叉轴对齐 */
.items-start      { align-items: flex-start; }
.items-end        { align-items: flex-end; }
.items-center     { align-items: center; }
.items-baseline   { align-items: baseline; }
.items-stretch    { align-items: stretch; }

/* 多行对齐 */
.content-start    { align-content: flex-start; }
.content-end      { align-content: flex-end; }
.content-center   { align-content: center; }
.content-between  { align-content: space-between; }
.content-around   { align-content: space-around; }

/* 子元素弹性 */
.flex-1           { flex: 1; }
.flex-auto        { flex: auto; }
.flex-none        { flex: none; }
.flex-grow        { flex-grow: 1; }

/* 间距辅助(gap) */
.gap-0            { gap: 0; }
.gap-1            { gap: var(--space-1); }
.gap-2            { gap: var(--space-2); }
.gap-3            { gap: var(--space-3); }
.gap-4            { gap: var(--space-4); }
.gap-5            { gap: var(--space-5); }
.gap-6            { gap: var(--space-6); }

/* ------- 4. 显示控制 ------- */
.d-none           { display: none !important; }
.d-block          { display: block !important; }
.d-inline         { display: inline !important; }
.d-inline-block   { display: inline-block !important; }
.d-flex           { display: flex !important; }
.d-inline-flex    { display: inline-flex !important; }

/* ------- 5. 间距工具类 ------- */
/* margin */
.m-0  { margin: 0; }
.m-1  { margin: var(--space-1); }
.m-2  { margin: var(--space-2); }
.m-3  { margin: var(--space-3); }
.m-4  { margin: var(--space-4); }
.m-5  { margin: var(--space-5); }
.m-6  { margin: var(--space-6); }

.mt-1 { margin-top: var(--space-1); }
.mt-2 { margin-top: var(--space-2); }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.mt-5 { margin-top: var(--space-5); }

.mb-1 { margin-bottom: var(--space-1); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-3 { margin-bottom: var(--space-3); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-5 { margin-bottom: var(--space-5); }

.ml-1 { margin-left: var(--space-1); }
.ml-2 { margin-left: var(--space-2); }
.ml-3 { margin-left: var(--space-3); }
.ml-4 { margin-left: var(--space-4); }
.ml-5 { margin-left: var(--space-5); }

.mr-1 { margin-right: var(--space-1); }
.mr-2 { margin-right: var(--space-2); }
.mr-3 { margin-right: var(--space-3); }
.mr-4 { margin-right: var(--space-4); }
.mr-5 { margin-right: var(--space-5); }

.mx-auto { margin-left: auto; margin-right: auto; }

/* padding */
.p-0  { padding: 0; }
.p-1  { padding: var(--space-1); }
.p-2  { padding: var(--space-2); }
.p-3  { padding: var(--space-3); }
.p-4  { padding: var(--space-4); }
.p-5  { padding: var(--space-5); }
.p-6  { padding: var(--space-6); }

.pt-1 { padding-top: var(--space-1); }
.pt-2 { padding-top: var(--space-2); }
.pt-3 { padding-top: var(--space-3); }
.pt-4 { padding-top: var(--space-4); }
.pt-5 { padding-top: var(--space-5); }

.pb-1 { padding-bottom: var(--space-1); }
.pb-2 { padding-bottom: var(--space-2); }
.pb-3 { padding-bottom: var(--space-3); }
.pb-4 { padding-bottom: var(--space-4); }
.pb-5 { padding-bottom: var(--space-5); }

.pl-1 { padding-left: var(--space-1); }
.pl-2 { padding-left: var(--space-2); }
.pl-3 { padding-left: var(--space-3); }
.pl-4 { padding-left: var(--space-4); }
.pl-5 { padding-left: var(--space-5); }

.pr-1 { padding-right: var(--space-1); }
.pr-2 { padding-right: var(--space-2); }
.pr-3 { padding-right: var(--space-3); }
.pr-4 { padding-right: var(--space-4); }
.pr-5 { padding-right: var(--space-5); }

/* ------- 6. 响应式断点工具 ------- */
/* 通用:从小屏开始,向上覆盖 */
/* Flex 方向 */
@media (min-width: 576px) {
  .flex-sm-row            { flex-direction: row !important; }
  .flex-sm-row-reverse    { flex-direction: row-reverse !important; }
  .flex-sm-column         { flex-direction: column !important; }
  .flex-sm-column-reverse { flex-direction: column-reverse !important; }
  .justify-sm-start   { justify-content: flex-start !important; }
  .justify-sm-end     { justify-content: flex-end !important; }
  .justify-sm-center  { justify-content: center !important; }
  .justify-sm-between { justify-content: space-between !important; }
  .justify-sm-around  { justify-content: space-around !important; }
  .items-sm-start     { align-items: flex-start !important; }
  .items-sm-end       { align-items: flex-end !important; }
  .items-sm-center    { align-items: center !important; }
  .d-sm-none          { display: none !important; }
  .d-sm-flex          { display: flex !important; }
  .d-sm-block         { display: block !important; }
}

@media (min-width: 768px) {
  .flex-md-row            { flex-direction: row !important; }
  .flex-md-row-reverse    { flex-direction: row-reverse !important; }
  .flex-md-column         { flex-direction: column !important; }
  .flex-md-column-reverse { flex-direction: column-reverse !important; }
  .justify-md-start   { justify-content: flex-start !important; }
  .justify-md-end     { justify-content: flex-end !important; }
  .justify-md-center  { justify-content: center !important; }
  .justify-md-between { justify-content: space-between !important; }
  .justify-md-around  { justify-content: space-around !important; }
  .items-md-start     { align-items: flex-start !important; }
  .items-md-end       { align-items: flex-end !important; }
  .items-md-center    { align-items: center !important; }
  .d-md-none          { display: none !important; }
  .d-md-flex          { display: flex !important; }
  .d-md-block         { display: block !important; }
}

@media (min-width: 992px) {
  .flex-lg-row            { flex-direction: row !important; }
  .flex-lg-row-reverse    { flex-direction: row-reverse !important; }
  .flex-lg-column         { flex-direction: column !important; }
  .flex-lg-column-reverse { flex-direction: column-reverse !important; }
  .justify-lg-start   { justify-content: flex-start !important; }
  .justify-lg-end     { justify-content: flex-end !important; }
  .justify-lg-center  { justify-content: center !important; }
  .justify-lg-between { justify-content: space-between !important; }
  .justify-lg-around  { justify-content: space-around !important; }
  .items-lg-start     { align-items: flex-start !important; }
  .items-lg-end       { align-items: flex-end !important; }
  .items-lg-center    { align-items: center !important; }
  .d-lg-none          { display: none !important; }
  .d-lg-flex          { display: flex !important; }
  .d-lg-block         { display: block !important; }
}

@media (min-width: 1200px) {
  .flex-xl-row            { flex-direction: row !important; }
  .flex-xl-row-reverse    { flex-direction: row-reverse !important; }
  .flex-xl-column         { flex-direction: column !important; }
  .flex-xl-column-reverse { flex-direction: column-reverse !important; }
  .justify-xl-start   { justify-content: flex-start !important; }
  .justify-xl-end     { justify-content: flex-end !important; }
  .justify-xl-center  { justify-content: center !important; }
  .justify-xl-between { justify-content: space-between !important; }
  .justify-xl-around  { justify-content: space-around !important; }
  .items-xl-start     { align-items: flex-start !important; }
  .items-xl-end       { align-items: flex-end !important; }
  .items-xl-center    { align-items: center !important; }
  .d-xl-none          { display: none !important; }
  .d-xl-flex          { display: flex !important; }
  .d-xl-block         { display: block !important; }
}

/* ------- 7. 容器 ------- */
.container {
  width: 100%;
  padding-left: var(--space-3);
  padding-right: var(--space-3);
  margin-left: auto;
  margin-right: auto;
}

@media (min-width: 576px)  { .container { max-width: 540px; } }
@media (min-width: 768px)  { .container { max-width: 720px; } }
@media (min-width: 992px)  { .container { max-width: 960px; } }
@media (min-width: 1200px) { .container { max-width: 1140px; } }
@media (min-width: 1400px) { .container { max-width: 1320px; } }

/* ------- 8. 12 列栅格(Flex 基础) ------- */
.row {
  display: flex;
  flex-wrap: wrap;
  margin-left: calc(var(--space-2) * -1);
  margin-right: calc(var(--space-2) * -1);
}

/* 通用列 */
.col {
  flex: 1 0 0%;
  max-width: 100%;
  padding-left: var(--space-2);
  padding-right: var(--space-2);
}

.col-1  { flex: 0 0 8.333333%;  max-width: 8.333333%;  padding: 0 var(--space-2); }
.col-2  { flex: 0 0 16.666667%; max-width: 16.666667%; padding: 0 var(--space-2); }
.col-3  { flex: 0 0 25%;        max-width: 25%;        padding: 0 var(--space-2); }
.col-4  { flex: 0 0 33.333333%; max-width: 33.333333%; padding: 0 var(--space-2); }
.col-5  { flex: 0 0 41.666667%; max-width: 41.666667%; padding: 0 var(--space-2); }
.col-6  { flex: 0 0 50%;        max-width: 50%;        padding: 0 var(--space-2); }
.col-7  { flex: 0 0 58.333333%; max-width: 58.333333%; padding: 0 var(--space-2); }
.col-8  { flex: 0 0 66.666667%; max-width: 66.666667%; padding: 0 var(--space-2); }
.col-9  { flex: 0 0 75%;        max-width: 75%;        padding: 0 var(--space-2); }
.col-10 { flex: 0 0 83.333333%; max-width: 83.333333%; padding: 0 var(--space-2); }
.col-11 { flex: 0 0 91.666667%; max-width: 91.666667%; padding: 0 var(--space-2); }
.col-12 { flex: 0 0 100%;       max-width: 100%;       padding: 0 var(--space-2); }

/* 响应式列:sm */
@media (min-width: 576px) {
  .col-sm-1  { flex: 0 0 8.333333%;  max-width: 8.333333%; }
  .col-sm-2  { flex: 0 0 16.666667%; max-width: 16.666667%; }
  .col-sm-3  { flex: 0 0 25%;        max-width: 25%; }
  .col-sm-4  { flex: 0 0 33.333333%; max-width: 33.333333%; }
  .col-sm-5  { flex: 0 0 41.666667%; max-width: 41.666667%; }
  .col-sm-6  { flex: 0 0 50%;        max-width: 50%; }
  .col-sm-7  { flex: 0 0 58.333333%; max-width: 58.333333%; }
  .col-sm-8  { flex: 0 0 66.666667%; max-width: 66.666667%; }
  .col-sm-9  { flex: 0 0 75%;        max-width: 75%; }
  .col-sm-10 { flex: 0 0 83.333333%; max-width: 83.333333%; }
  .col-sm-11 { flex: 0 0 91.666667%; max-width: 91.666667%; }
  .col-sm-12 { flex: 0 0 100%;       max-width: 100%; }
}

/* 响应式列:md */
@media (min-width: 768px) {
  .col-md-1  { flex: 0 0 8.333333%;  max-width: 8.333333%; }
  .col-md-2  { flex: 0 0 16.666667%; max-width: 16.666667%; }
  .col-md-3  { flex: 0 0 25%;        max-width: 25%; }
  .col-md-4  { flex: 0 0 33.333333%; max-width: 33.333333%; }
  .col-md-5  { flex: 0 0 41.666667%; max-width: 41.666667%; }
  .col-md-6  { flex: 0 0 50%;        max-width: 50%; }
  .col-md-7  { flex: 0 0 58.333333%; max-width: 58.333333%; }
  .col-md-8  { flex: 0 0 66.666667%; max-width: 66.666667%; }
  .col-md-9  { flex: 0 0 75%;        max-width: 75%; }
  .col-md-10 { flex: 0 0 83.333333%; max-width: 83.333333%; }
  .col-md-11 { flex: 0 0 91.666667%; max-width: 91.666667%; }
  .col-md-12 { flex: 0 0 100%;       max-width: 100%; }
}

/* 响应式列:lg */
@media (min-width: 992px) {
  .col-lg-1  { flex: 0 0 8.333333%;  max-width: 8.333333%; }
  .col-lg-2  { flex: 0 0 16.666667%; max-width: 16.666667%; }
  .col-lg-3  { flex: 0 0 25%;        max-width: 25%; }
  .col-lg-4  { flex: 0 0 33.333333%; max-width: 33.333333%; }
  .col-lg-5  { flex: 0 0 41.666667%; max-width: 41.666667%; }
  .col-lg-6  { flex: 0 0 50%;        max-width: 50%; }
  .col-lg-7  { flex: 0 0 58.333333%; max-width: 58.333333%; }
  .col-lg-8  { flex: 0 0 66.666667%; max-width: 66.666667%; }
  .col-lg-9  { flex: 0 0 75%;        max-width: 75%; }
  .col-lg-10 { flex: 0 0 83.333333%; max-width: 83.333333%; }
  .col-lg-11 { flex: 0 0 91.666667%; max-width: 91.666667%; }
  .col-lg-12 { flex: 0 0 100%;       max-width: 100%; }
}

/* ------- 9. 基础组件(使用主色调) ------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.5em 1.2em;
  font-size: 0.9rem;
  line-height: 1.5;
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}
.btn-primary:hover {
  background: var(--color-primary-hover);
}

.btn-secondary {
  background: var(--color-secondary);
  color: #fff;
}
.btn-secondary:hover {
  background: var(--color-secondary-hover);
}

.btn-outline {
  background: transparent;
  border-color: var(--color-primary);
  color: var(--color-primary);
}
.btn-outline:hover {
  background: var(--color-primary);
  color: #fff;
}

.card {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-3);
}

/* ------- 10. 其他实用类 ------- */
.text-center   { text-align: center; }
.text-left     { text-align: left; }
.text-right    { text-align: right; }

.rounded       { border-radius: var(--radius); }
.shadow        { box-shadow: var(--shadow); }

.bg-primary    { background-color: var(--color-primary); color: #fff; }
.bg-secondary  { background-color: var(--color-secondary); color: #fff; }
.bg-light      { background-color: var(--color-bg-gray); }

快速定制指南

  • 换主色 :直接修改 :root 中的 --color-primary 及相关变量。
  • 调整断点 :改变 --bp-sm 等变量,并同步更新对应媒体查询的像素值(因 CSS 变量无法用于媒体查询条件,需手动对应)。
  • 缩减体积:只保留你需要的工具类和栅格断点,移除无用部分即可。

这套 CSS 涵盖了绝大多数响应式布局场景,可以直接作为新项目的"起始样式表",也可以混入现有工程中使用。

相关推荐
前进的李工1 小时前
智能Agent实战指南:记忆组件嵌入技巧(记忆)
开发语言·前端·javascript·python·langchain·agent
西洼工作室1 小时前
B站登录流程全解析:RSA+极验验证
前端·python·极验
十有八七1 小时前
AI Agent的“骨架”之争:四种Harness设计哲学深度解构
前端·人工智能
卡次卡次11 小时前
14.2:详细补充:子进程会复制什么
前端·python·php
泽_浪里白条2 小时前
superset 踩过的坑之嵌入式 Dashboard 数据筛选
前端·后端
IOT.FIVE.NO.12 小时前
Codex Skill 内部结构解析:从 SKILL.md 到 scripts、references、assets
前端·javascript·人工智能·笔记·html
PILIPALAPENG2 小时前
第4周 Day 2:多步推理 Agent——让 Agent 学会"先想再干"
前端·人工智能·python
LPieces3 小时前
从零实现 AI 流式对话:SSE + Node.js 完整指南
前端