前端响应式设计

html 复制代码
/* 响应式设计工具文件 */
/* 响应式断点定义 */
:root {
  --breakpoint-xs: 0;
  --breakpoint-sm: 576px;
  --breakpoint-md: 768px;
  --breakpoint-lg: 992px;
  --breakpoint-xl: 1200px;
  --breakpoint-xxl: 1400px;
}

/* 响应式容器 */
.container {
  width: 100%;
  padding-right: 15px;
  padding-left: 15px;
  margin-right: auto;
  margin-left: 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;
  }
}

/* 响应式网格系统 */
.row {
  display: flex;
  flex-wrap: wrap;
  margin-right: -15px;
  margin-left: -15px;
}

.col {
  flex: 1 0 0%;
  padding-right: 15px;
  padding-left: 15px;
}

/* 响应式列类 */
.col-xs-12 { flex: 0 0 100%; max-width: 100%; }
.col-xs-6 { flex: 0 0 50%; max-width: 50%; }
.col-xs-4 { flex: 0 0 33.333333%; max-width: 33.333333%; }
.col-xs-3 { flex: 0 0 25%; max-width: 25%; }

@media (min-width: 576px) {
  .col-sm-12 { flex: 0 0 100%; max-width: 100%; }
  .col-sm-6 { flex: 0 0 50%; max-width: 50%; }
  .col-sm-4 { flex: 0 0 33.333333%; max-width: 33.333333%; }
  .col-sm-3 { flex: 0 0 25%; max-width: 25%; }
}

@media (min-width: 768px) {
  .col-md-12 { flex: 0 0 100%; max-width: 100%; }
  .col-md-6 { flex: 0 0 50%; max-width: 50%; }
  .col-md-4 { flex: 0 0 33.333333%; max-width: 33.333333%; }
  .col-md-3 { flex: 0 0 25%; max-width: 25%; }
}

@media (min-width: 992px) {
  .col-lg-12 { flex: 0 0 100%; max-width: 100%; }
  .col-lg-6 { flex: 0 0 50%; max-width: 50%; }
  .col-lg-4 { flex: 0 0 33.333333%; max-width: 33.333333%; }
  .col-lg-3 { flex: 0 0 25%; max-width: 25%; }
}

@media (min-width: 1200px) {
  .col-xl-12 { flex: 0 0 100%; max-width: 100%; }
  .col-xl-6 { flex: 0 0 50%; max-width: 50%; }
  .col-xl-4 { flex: 0 0 33.333333%; max-width: 33.333333%; }
  .col-xl-3 { flex: 0 0 25%; max-width: 25%; }
}

/* 响应式工具类 */
.d-none { display: none !important; }
.d-block { display: block !important; }
.d-flex { display: flex !important; }
.d-inline { display: inline !important; }
.d-inline-block { display: inline-block !important; }

/* 响应式显示/隐藏 */
@media (max-width: 575.98px) {
  .d-xs-none { display: none !important; }
  .d-xs-block { display: block !important; }
  .d-xs-flex { display: flex !important; }
}

@media (min-width: 576px) and (max-width: 767.98px) {
  .d-sm-none { display: none !important; }
  .d-sm-block { display: block !important; }
  .d-sm-flex { display: flex !important; }
}

@media (min-width: 768px) and (max-width: 991.98px) {
  .d-md-none { display: none !important; }
  .d-md-block { display: block !important; }
  .d-md-flex { display: flex !important; }
}

@media (min-width: 992px) and (max-width: 1199.98px) {
  .d-lg-none { display: none !important; }
  .d-lg-block { display: block !important; }
  .d-lg-flex { display: flex !important; }
}

@media (min-width: 1200px) {
  .d-xl-none { display: none !important; }
  .d-xl-block { display: block !important; }
  .d-xl-flex { display: flex !important; }
}

/* 响应式文本对齐 */
.text-left { text-align: left !important; }
.text-center { text-align: center !important; }
.text-right { text-align: right !important; }

@media (max-width: 575.98px) {
  .text-xs-left { text-align: left !important; }
  .text-xs-center { text-align: center !important; }
  .text-xs-right { text-align: right !important; }
}

@media (min-width: 576px) and (max-width: 767.98px) {
  .text-sm-left { text-align: left !important; }
  .text-sm-center { text-align: center !important; }
  .text-sm-right { text-align: right !important; }
}

@media (min-width: 768px) and (max-width: 991.98px) {
  .text-md-left { text-align: left !important; }
  .text-md-center { text-align: center !important; }
  .text-md-right { text-align: right !important; }
}

@media (min-width: 992px) and (max-width: 1199.98px) {
  .text-lg-left { text-align: left !important; }
  .text-lg-center { text-align: center !important; }
  .text-lg-right { text-align: right !important; }
}

@media (min-width: 1200px) {
  .text-xl-left { text-align: left !important; }
  .text-xl-center { text-align: center !important; }
  .text-xl-right { text-align: right !important; }
}

/* 响应式间距 */
.m-0 { margin: 0 !important; }
.m-1 { margin: 0.25rem !important; }
.m-2 { margin: 0.5rem !important; }
.m-3 { margin: 1rem !important; }
.m-4 { margin: 1.5rem !important; }
.m-5 { margin: 3rem !important; }

.p-0 { padding: 0 !important; }
.p-1 { padding: 0.25rem !important; }
.p-2 { padding: 0.5rem !important; }
.p-3 { padding: 1rem !important; }
.p-4 { padding: 1.5rem !important; }
.p-5 { padding: 3rem !important; }

/* 响应式字体大小 */
.fs-1 { font-size: 2.5rem !important; }
.fs-2 { font-size: 2rem !important; }
.fs-3 { font-size: 1.75rem !important; }
.fs-4 { font-size: 1.5rem !important; }
.fs-5 { font-size: 1.25rem !important; }
.fs-6 { font-size: 1rem !important; }

@media (max-width: 575.98px) {
  .fs-xs-1 { font-size: 2rem !important; }
  .fs-xs-2 { font-size: 1.75rem !important; }
  .fs-xs-3 { font-size: 1.5rem !important; }
  .fs-xs-4 { font-size: 1.25rem !important; }
  .fs-xs-5 { font-size: 1rem !important; }
  .fs-xs-6 { font-size: 0.875rem !important; }
}

@media (min-width: 576px) and (max-width: 767.98px) {
  .fs-sm-1 { font-size: 2.25rem !important; }
  .fs-sm-2 { font-size: 2rem !important; }
  .fs-sm-3 { font-size: 1.75rem !important; }
  .fs-sm-4 { font-size: 1.5rem !important; }
  .fs-sm-5 { font-size: 1.25rem !important; }
  .fs-sm-6 { font-size: 1rem !important; }
}

/* 响应式表格 */
.responsive-table {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.responsive-table table {
  min-width: 600px;
}

/* 响应式图片 */
.img-fluid {
  max-width: 100%;
  height: auto;
}

.img-thumbnail {
  padding: 0.25rem;
  background-color: #fff;
  border: 1px solid #dee2e6;
  border-radius: 0.25rem;
  max-width: 100%;
  height: auto;
}

/* 响应式按钮组 */
.btn-group-responsive {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

@media (max-width: 575.98px) {
  .btn-group-responsive {
    flex-direction: column;
  }
  
  .btn-group-responsive .btn {
    width: 100%;
    margin-bottom: 0.5rem;
  }
}

/* 响应式表单 */
.form-responsive .form-group {
  margin-bottom: 1rem;
}

@media (max-width: 575.98px) {
  .form-responsive .form-group {
    margin-bottom: 0.75rem;
  }
  
  .form-responsive .form-label {
    display: block;
    margin-bottom: 0.25rem;
    font-weight: 500;
  }
}

/* 响应式卡片 */
.card-responsive {
  height: 100%;
  transition: transform 0.2s ease-in-out;
}

.card-responsive:hover {
  transform: translateY(-2px);
}

@media (max-width: 575.98px) {
  .card-responsive {
    margin-bottom: 1rem;
  }
}

/* 响应式导航 */
.nav-responsive {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

@media (max-width: 575.98px) {
  .nav-responsive {
    flex-direction: column;
    gap: 0.5rem;
  }
  
  .nav-responsive .nav-item {
    width: 100%;
  }
  
  .nav-responsive .nav-link {
    text-align: center;
    padding: 0.75rem;
    border: 1px solid #dee2e6;
    border-radius: 0.25rem;
  }
}

/* 响应式模态框 */
.modal-responsive .modal-dialog {
  margin: 1rem;
  max-width: calc(100% - 2rem);
}

@media (min-width: 576px) {
  .modal-responsive .modal-dialog {
    margin: 1.75rem auto;
    max-width: 500px;
  }
}

@media (min-width: 768px) {
  .modal-responsive .modal-dialog {
    max-width: 600px;
  }
}

@media (min-width: 992px) {
  .modal-responsive .modal-dialog {
    max-width: 800px;
  }
}

/* 响应式工具提示 */
.tooltip-responsive {
  max-width: 200px;
}

@media (max-width: 575.98px) {
  .tooltip-responsive {
    max-width: 150px;
    font-size: 0.875rem;
  }
}

/* 响应式加载状态 */
.loading-responsive {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 200px;
}

@media (max-width: 575.98px) {
  .loading-responsive {
    min-height: 150px;
  }
}

/* 响应式滚动条 */
.scrollbar-responsive {
  scrollbar-width: thin;
  scrollbar-color: #888 #f1f1f1;
}

.scrollbar-responsive::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.scrollbar-responsive::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.scrollbar-responsive::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 3px;
}

.scrollbar-responsive::-webkit-scrollbar-thumb:hover {
  background: #555;
}

/* 响应式动画 */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* 响应式焦点样式 */
@media (prefers-reduced-motion: no-preference) {
  .focus-visible {
    outline: 2px solid #007bff;
    outline-offset: 2px;
  }
}

/* 响应式选择器 */
@media (max-width: 575.98px) {
  .select-responsive {
    width: 100%;
    margin-bottom: 1rem;
  }
}

/* 响应式日期选择器 */
@media (max-width: 575.98px) {
  .datepicker-responsive {
    width: 100%;
  }
  
  .datepicker-responsive .el-date-editor {
    width: 100%;
  }
}

/* 响应式级联选择器 */
@media (max-width: 575.98px) {
  .cascader-responsive {
    width: 100%;
  }
  
  .cascader-responsive .el-cascader {
    width: 100%;
  }
}

/* 响应式标签页 */
@media (max-width: 575.98px) {
  .tabs-responsive .el-tabs__nav {
    flex-wrap: wrap;
  }
  
  .tabs-responsive .el-tabs__item {
    flex: 1;
    text-align: center;
    min-width: 0;
  }
}

/* 响应式分页 */
@media (max-width: 575.98px) {
  .pagination-responsive .el-pagination {
    justify-content: center;
    flex-wrap: wrap;
  }
  
  .pagination-responsive .el-pagination .el-pager {
    display: none;
  }
  
  .pagination-responsive .el-pagination .btn-prev,
  .pagination-responsive .el-pagination .btn-next {
    display: inline-block;
  }
}

/* 响应式通知 */
@media (max-width: 575.98px) {
  .notification-responsive {
    width: calc(100% - 2rem);
    margin: 0 1rem;
  }
}

/* 响应式消息框 */
@media (max-width: 575.98px) {
  .message-responsive {
    max-width: calc(100% - 2rem);
    margin: 0 1rem;
  }
}

/* 响应式确认框 */
@media (max-width: 575.98px) {
  .confirm-responsive {
    width: calc(100% - 2rem);
    margin: 0 1rem;
  }
  
  .confirm-responsive .el-message-box__content {
    padding: 1rem;
  }
  
  .confirm-responsive .el-message-box__btns {
    padding: 0.5rem 1rem 1rem;
  }
} 

通用性分析:

✅ 可以直接使用的部分:

  • CSS变量定义:断点变量(--breakpoint-xs、--breakpoint-sm 等)
  • 基础网格系统:.container、.row、.col 等
  • 响应式工具类:显示/隐藏、文本对齐、间距、字体大小等
  • 基础响应式组件:表格、图片、按钮组、表单等

    ⚠️ 需要项目适配的部分:

  • UI框架特定样式:

css 复制代码
   /* 这些是针对Element UI的,如果项目使用其他UI框架需要修改 */
   .datepicker-responsive .el-date-editor
   .cascader-responsive .el-cascader
   .tabs-responsive .el-tabs__nav

颜色和主题
*

css 复制代码
   /* 这些颜色值可能需要匹配项目主题 */
   .focus-visible { outline: 2px solid #007bff; }
   .scrollbar-responsive::-webkit-scrollbar-thumb { background: #888; }

建议使用方式:

  • 作为基础模板:复制到新项目,然后根据需求调整
  • 按需引入:只复制需要的部分,避免冗余
  • 项目定制:根据具体项目的设计规范和UI框架进行适配
  • 总的来说,这是一个很好的响应式设计起点,但建议根据具体项目需求进行适当修改和优化。
相关推荐
风舞1 分钟前
一文搞定JS所有类型判断最佳实践
前端·javascript
coding随想1 分钟前
哈希值变化的魔法:深入解析HTML5 hashchange事件的奥秘与实战
前端
一树山茶9 分钟前
uniapp在微信小程序中实现 SSE进行通信
前端·javascript
coding随想9 分钟前
小程序中的pageshow与pagehide事件,HTML5中也有?揭秘浏览器往返缓存(BFCache)
前端
萌萌哒草头将军14 分钟前
Rspack 1.5 版本更新速览!🚀🚀🚀
前端·javascript·vue.js
阿卡不卡19 分钟前
基于多场景的通用单位转换功能实现
前端·javascript
♡喜欢做梦30 分钟前
jQuery 从入门到实践:基础语法、事件与元素操作全解析
前端·javascript·jquery
flyliu34 分钟前
前端权限控制应该怎么做
前端·前端工程化
酸菜土狗37 分钟前
gitignor配置禁止上传文件目录到 Git
前端·javascript
小猪猪屁37 分钟前
告别依赖地狱!Monorepo 打造高效 Vue3 项目体系
前端·前端框架