CSS典型代码示例 - 按应用场景分类(含详细备注)

121 阅读8分钟

CSS典型代码示例 - 按应用场景分类(含详细备注)

1. 布局类

Flexbox布局

/* 水平垂直居中 - 适用于弹窗、加载动画等需要居中的场景 */
.center-flex {
  display: flex;
  justify-content: center;     /* 水平居中 */
  align-items: center;         /* 垂直居中 */
  height: 100vh;              /* 全屏高度,可根据需要调整 */
}

/* 三栏布局 - 适用于博客、新闻网站等经典布局 */
.three-column {
  display: flex;
}
.sidebar {
  flex: 0 0 200px;            /* 不放大、不缩小、基础宽度200px */
}
.main-content {
  flex: 1;                    /* 占据剩余空间 */
  margin: 0 20px;             /* 左右间距 */
}

/* 导航栏 - 适用于顶部导航菜单 */
.navbar {
  display: flex;
  justify-content: space-between;  /* 两端对齐,logo和菜单分离 */
  align-items: center;             /* 垂直居中 */
}
.nav-links {
  display: flex;
  gap: 20px;                       /* 子元素间距 */
}

Grid布局

/* 响应式网格 - 适用于产品展示、图片画廊等需要自适应的网格布局 */
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));  /* 自适应列数,最小300px */
  gap: 20px;                                                    /* 网格间距 */
}

/* 卡片布局 - 适用于首页推荐、产品列表等固定列数的布局 */
.card-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);  /* 3列等宽 */
  grid-gap: 20px;                         /* 网格间距 */
}

/* 复杂布局 - 适用于企业网站等需要精确控制区域位置的场景 */
.complex-layout {
  display: grid;
  grid-template-areas: 
    "header header header"      /* 头部横跨3列 */
    "sidebar main aside"        /* 中间三列布局 */
    "footer footer footer";     /* 底部横跨3列 */
  grid-template-rows: 80px 1fr 60px;     /* 行高:头部80px,主体自适应,底部60px */
  grid-template-columns: 200px 1fr 200px; /* 列宽:侧边栏200px,主体自适应,右侧栏200px */
}

浮动布局(传统方法)

/* 经典两栏 - 兼容老浏览器时使用,现在推荐用Flexbox */
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}
.sidebar {
  float: left;
  width: 200px;
}
.main-content {
  float: right;
  width: calc(100% - 220px);  /* 减去侧边栏宽度和间距 */
}

2. 导航菜单

水平导航

/* 水平导航 - 适用于网站顶部主菜单 */
.horizontal-nav {
  list-style: none;           /* 移除默认列表样式 */
  margin: 0;
  padding: 0;
  display: flex;
  background: #333;           /* 深色背景 */
}
.horizontal-nav li {
  flex: 1;                    /* 平均分配宽度 */
}
.horizontal-nav a {
  display: block;
  padding: 15px 20px;
  color: white;
  text-decoration: none;
  text-align: center;
  transition: background 0.3s; /* 平滑过渡效果 */
}
.horizontal-nav a:hover {
  background: #555;           /* 悬停背景色 */
}

垂直侧边栏

/* 垂直侧边栏 - 适用于管理后台、文档网站等 */
.sidebar-nav {
  width: 200px;
  background: #f5f5f5;        /* 浅灰色背景 */
  height: 100vh;              /* 全屏高度 */
}
.sidebar-nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.sidebar-nav li {
  border-bottom: 1px solid #ddd;  /* 底部分割线 */
}
.sidebar-nav a {
  display: block;
  padding: 15px 20px;
  color: #333;
  text-decoration: none;
  transition: all 0.3s;           /* 所有属性平滑过渡 */
}
.sidebar-nav a:hover {
  background: #007bff;            /* 蓝色主题 */
  color: white;
}

下拉菜单

/* 下拉菜单 - 适用于多级导航、用户菜单等 */
.dropdown {
  position: relative;
  display: inline-block;
}
.dropdown-content {
  display: none;              /* 默认隐藏 */
  position: absolute;
  background: white;
  min-width: 160px;
  box-shadow: 0 8px 16px rgba(0,0,0,0.2);  /* 阴影效果 */
  z-index: 1;                 /* 确保在最上层 */
}
.dropdown:hover .dropdown-content {
  display: block;             /* 悬停时显示 */
}
.dropdown-content a {
  display: block;
  padding: 12px 16px;
  text-decoration: none;
  color: black;
}
.dropdown-content a:hover {
  background: #f1f1f1;        /* 悬停背景色 */
}

3. 按钮样式

基础按钮

/* 基础按钮 - 适用于各种操作按钮 */
.btn {
  display: inline-block;
  padding: 12px 24px;         /* 合适的点击区域 */
  background: #007bff;        /* 蓝色主题 */
  color: white;
  border: none;
  border-radius: 4px;         /* 圆角 */
  cursor: pointer;
  text-decoration: none;
  font-size: 16px;
  transition: all 0.3s;       /* 所有变化都有过渡效果 */
}
.btn:hover {
  background: #0056b3;        /* 深色悬停效果 */
  transform: translateY(-2px); /* 向上微移 */
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);  /* 添加阴影 */
}
.btn:active {
  transform: translateY(0);   /* 点击时恢复位置 */
}

/* 边框按钮 - 适用于次要操作 */
.btn-outline {
  background: transparent;
  border: 2px solid #007bff;  /* 蓝色边框 */
  color: #007bff;
}
.btn-outline:hover {
  background: #007bff;
  color: white;
}

/* 按钮尺寸 - 适用于不同场景的按钮大小 */
.btn-large {
  padding: 16px 32px;
  font-size: 18px;
}
.btn-small {
  padding: 8px 16px;
  font-size: 14px;
}

4. 表单样式

现代表单

/* 现代表单 - 适用于注册、登录、联系表单等 */
.form-container {
  max-width: 500px;
  margin: 0 auto;             /* 居中显示 */
  padding: 20px;
}
.form-group {
  margin-bottom: 20px;        /* 表单项间距 */
}
.form-label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #333;
}
.form-input {
  width: 100%;                /* 全宽 */
  padding: 12px;
  border: 2px solid #ddd;     /* 边框 */
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;  /* 边框颜色过渡 */
}
.form-input:focus {
  outline: none;              /* 移除默认轮廓 */
  border-color: #007bff;      /* 聚焦时边框颜色 */
  box-shadow: 0 0 0 3px rgba(0,123,255,0.1);  /* 聚焦阴影 */
}
.form-input.error {
  border-color: #dc3545;      /* 错误状态红色边框 */
}
.form-error {
  color: #dc3545;             /* 错误提示红色 */
  font-size: 14px;
  margin-top: 5px;
}

搜索框

/* 搜索框 - 适用于网站搜索功能 */
.search-box {
  position: relative;
  max-width: 400px;
}
.search-input {
  width: 100%;
  padding: 12px 45px 12px 15px;  /* 右侧留出按钮空间 */
  border: 2px solid #ddd;
  border-radius: 25px;           /* 圆形边框 */
  font-size: 16px;
}
.search-btn {
  position: absolute;
  right: 5px;                    /* 右侧定位 */
  top: 50%;
  transform: translateY(-50%);   /* 垂直居中 */
  background: #007bff;
  color: white;
  border: none;
  border-radius: 20px;
  padding: 8px 15px;
  cursor: pointer;
}

5. 卡片组件

基础卡片

/* 基础卡片 - 适用于产品展示、文章预览、用户信息等 */
.card {
  background: white;
  border-radius: 8px;         /* 圆角 */
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);  /* 阴影 */
  overflow: hidden;           /* 隐藏溢出内容 */
  transition: transform 0.3s, box-shadow 0.3s;  /* 过渡效果 */
}
.card:hover {
  transform: translateY(-5px);  /* 悬停时上移 */
  box-shadow: 0 5px 20px rgba(0,0,0,0.2);  /* 增强阴影 */
}
.card-img {
  width: 100%;
  height: 200px;
  object-fit: cover;          /* 保持比例填充 */
}
.card-body {
  padding: 20px;
}
.card-title {
  margin: 0 0 10px 0;
  font-size: 20px;
  color: #333;
}
.card-text {
  color: #666;
  line-height: 1.6;           /* 行高 */
  margin-bottom: 15px;
}
.card-footer {
  padding: 0 20px 20px;
}

6. 响应式设计

媒体查询

/* 移动优先 - 适用于移动端优先的响应式设计 */
.container {
  width: 100%;
  padding: 0 15px;            /* 移动端内边距 */
}

/* 平板 - 768px及以上 */
@media (min-width: 768px) {
  .container {
    max-width: 750px;
    margin: 0 auto;
  }
  .grid-md-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;  /* 两列 */
    gap: 20px;
  }
}

/* 桌面 - 1024px及以上 */
@media (min-width: 1024px) {
  .container {
    max-width: 1200px;
  }
  .grid-lg-3 {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;  /* 三列 */
    gap: 30px;
  }
}

/* 大屏幕 - 1200px及以上 */
@media (min-width: 1200px) {
  .container {
    max-width: 1400px;
  }
}

移动端适配

/* 移动端优化 - 解决移动端常见问题 */
.mobile-friendly {
  font-size: 16px;            /* 防止iOS缩放 */
  -webkit-tap-highlight-color: transparent;  /* 移除点击高亮 */
}

/* 触摸目标 - 确保移动端按钮足够大 */
.touch-target {
  min-height: 44px;           /* iOS推荐最小触摸目标 */
  min-width: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 响应式图片 - 适用于各种屏幕尺寸 */
.responsive-img {
  max-width: 100%;            /* 最大宽度100% */
  height: auto;               /* 自动高度保持比例 */
}

7. 动画效果

基础动画

/* 淡入效果 - 适用于页面加载、模态框显示等 */
.fade-in {
  animation: fadeIn 0.5s ease-in;  /* 0.5秒缓入动画 */
}
@keyframes fadeIn {
  from { opacity: 0; }        /* 从透明开始 */
  to { opacity: 1; }          /* 到完全显示 */
}

/* 滑动效果 - 适用于通知、下拉菜单等 */
.slide-up {
  animation: slideUp 0.3s ease-out;  /* 0.3秒缓出动画 */
}
@keyframes slideUp {
  from {
    transform: translateY(20px);     /* 从下方20px开始 */
    opacity: 0;
  }
  to {
    transform: translateY(0);        /* 到原始位置 */
    opacity: 1;
  }
}

/* 脉冲效果 - 适用于吸引注意的元素 */
.pulse {
  animation: pulse 2s infinite;      /* 2秒无限循环 */
}
@keyframes pulse {
  0% { transform: scale(1); }        /* 原始大小 */
  50% { transform: scale(1.05); }    /* 放大5% */
  100% { transform: scale(1); }      /* 恢复原大小 */
}

加载动画

/* 旋转加载器 - 适用于数据加载状态 */
.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;       /* 浅灰色边框 */
  border-top: 4px solid #007bff;   /* 蓝色顶部边框 */
  border-radius: 50%;              /* 圆形 */
  animation: spin 1s linear infinite;  /* 1秒线性无限旋转 */
}
@keyframes spin {
  0% { transform: rotate(0deg); }   /* 从0度开始 */
  100% { transform: rotate(360deg); }  /* 旋转360度 */
}

8. 实用工具类

间距工具

/* 内边距工具类 - 快速设置内边距 */
.p-0 { padding: 0; }
.p-1 { padding: 0.25rem; }    /* 4px */
.p-2 { padding: 0.5rem; }     /* 8px */
.p-3 { padding: 1rem; }       /* 16px */
.p-4 { padding: 1.5rem; }     /* 24px */
.p-5 { padding: 3rem; }       /* 48px */

.pt-3 { padding-top: 1rem; }    /* 上内边距 */
.pb-3 { padding-bottom: 1rem; } /* 下内边距 */
.pl-3 { padding-left: 1rem; }   /* 左内边距 */
.pr-3 { padding-right: 1rem; }  /* 右内边距 */

/* 外边距工具类 - 快速设置外边距 */
.m-0 { margin: 0; }
.m-1 { margin: 0.25rem; }
.m-2 { margin: 0.5rem; }
.m-3 { margin: 1rem; }
.m-4 { margin: 1.5rem; }
.m-5 { margin: 3rem; }

.mt-3 { margin-top: 1rem; }     /* 上外边距 */
.mb-3 { margin-bottom: 1rem; }  /* 下外边距 */
.ml-3 { margin-left: 1rem; }    /* 左外边距 */
.mr-3 { margin-right: 1rem; }   /* 右外边距 */
.mx-auto { 
  margin-left: auto; 
  margin-right: auto;           /* 水平居中 */
}

文本工具

/* 文本对齐工具类 - 快速设置文本对齐 */
.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-justify { text-align: justify; }

/* 文本大小工具类 - 快速设置字体大小 */
.text-small { font-size: 0.875rem; }  /* 14px */
.text-normal { font-size: 1rem; }     /* 16px */
.text-large { font-size: 1.25rem; }   /* 20px */
.text-xl { font-size: 1.5rem; }       /* 24px */

/* 文本颜色工具类 - 快速设置文本颜色 */
.text-primary { color: #007bff; }     /* 主色 */
.text-success { color: #28a745; }     /* 成功色 */
.text-danger { color: #dc3545; }      /* 危险色 */
.text-warning { color: #ffc107; }     /* 警告色 */
.text-muted { color: #6c757d; }       /* 灰色 */

/* 文本装饰工具类 - 快速设置文本样式 */
.text-bold { font-weight: bold; }
.text-italic { font-style: italic; }
.text-uppercase { text-transform: uppercase; }  /* 大写 */
.text-capitalize { text-transform: capitalize; } /* 首字母大写 */

这些CSS代码示例都包含了详细的注释说明,解释了每个样式的作用和适用场景,方便开发者理解和使用。