前端开发必备 CSS 代码片段合集

Created on

前言

在日常开发中,我们经常会遇到一些重复的 CSS 需求。本文整理了最实用的 CSS 代码片段,涵盖文字处理、滚动条、布局、动画等多个方面,帮助你提高开发效率。

文字处理

单行文字省略

.text-ellipsis {
  white-space: nowrap; /* 不换行 */
  overflow: hidden; /* 超出隐藏 */
  text-overflow: ellipsis; /* 显示省略号 */
}

使用场景:标题、用户名、单行描述等

多行文字省略

.text-ellipsis-multiline {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2; /* 显示行数 */
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 3 行省略 */
.text-ellipsis-3 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

注意:需要设置固定宽度或 max-width

文字渐变色

.text-gradient {
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

文字描边

.text-stroke {
  -webkit-text-stroke: 1px #000;
  text-stroke: 1px #000;
}

文字阴影

/* 基础阴影 */
.text-shadow-basic {
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

/* 立体效果 */
.text-shadow-3d {
  text-shadow: 0 1px 0 #ccc, 0 2px 0 #c9c9c9, 0 3px 0 #bbb, 0 4px 0 #b9b9b9,
    0 5px 0 #aaa, 0 6px 1px rgba(0, 0, 0, 0.1);
}

/* 发光效果 */
.text-shadow-glow {
  text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #667eea;
}

首字母大写/小写

/* 首字母大写 */
.text-capitalize {
  text-transform: capitalize;
}

/* 全部大写 */
.text-uppercase {
  text-transform: uppercase;
}

/* 全部小写 */
.text-lowercase {
  text-transform: lowercase;
}

滚动条美化

基础滚动条样式

/* 整个滚动条 */
::-webkit-scrollbar {
  width: 6px; /* 垂直滚动条宽度 */
  height: 6px; /* 水平滚动条高度 */
}

/* 滚动条轨道 */
::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 10px;
}

/* 滚动条滑块 */
::-webkit-scrollbar-thumb {
  background: #888;
  border-radius: 10px;
}

/* 滚动条滑块悬停 */
::-webkit-scrollbar-thumb:hover {
  background: #555;
}

/* 滚动条按钮(上下箭头) */
::-webkit-scrollbar-button {
  display: none; /* 隐藏箭头 */
}

/* 两个滚动条交汇的角落 */
::-webkit-scrollbar-corner {
  background: transparent;
}

隐藏滚动条但保持滚动功能

/* Chrome, Safari */
.hide-scrollbar::-webkit-scrollbar {
  display: none;
}

/* Firefox */
.hide-scrollbar {
  scrollbar-width: none;
}

/* IE, Edge */
.hide-scrollbar {
  -ms-overflow-style: none;
}

细滚动条(macOS 风格)

.thin-scrollbar::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.thin-scrollbar::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.thin-scrollbar::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 4px;
  transition: background 0.3s;
}

.thin-scrollbar::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.4);
}

布局技巧

水平垂直居中

/* Flexbox 方式(推荐) */
.center-flex {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Grid 方式 */
.center-grid {
  display: grid;
  place-items: center;
}

/* Position + Transform */
.center-absolute {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* Position + Margin */
.center-absolute-margin {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  width: 200px; /* 需要固定宽高 */
  height: 200px;
}

Flex 布局常用场景

/* 两端对齐 */
.flex-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 居中对齐 */
.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 垂直布局 */
.flex-column {
  display: flex;
  flex-direction: column;
}

/* 自动换行 */
.flex-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 10px; /* 间距 */
}

/* 平均分配空间 */
.flex-item-equal {
  flex: 1;
}

Grid 布局常用场景

/* 响应式网格 */
.grid-responsive {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}

/* 固定列数 */
.grid-3-columns {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

/* 圣杯布局 */
.grid-holy-grail {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar content aside"
    "footer footer footer";
  grid-template-columns: 200px 1fr 200px;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

粘性定位(Sticky)

/* 吸顶导航 */
.sticky-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: white;
}

/* 吸底按钮 */
.sticky-footer {
  position: sticky;
  bottom: 0;
  z-index: 100;
}

遮罩与禁用

禁止遮罩层底部页面滚动

/* 方法一:给 body 添加样式 */
body.no-scroll {
  height: 100vh;
  overflow: hidden;
}

/* 方法二:给遮罩层添加样式 */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}

使用示例

// 打开遮罩
document.body.classList.add("no-scroll");

// 关闭遮罩
document.body.classList.remove("no-scroll");

遮罩层样式

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(5px); /* 背景模糊效果 */
  z-index: 1000;
}

禁用元素(保持占位)

.disabled {
  pointer-events: none; /* 禁用鼠标事件 */
  opacity: 0.5; /* 降低透明度 */
  cursor: not-allowed;
}

表单美化

去除 Input 自动填充样式

/* Chrome, Safari */
input:-webkit-autofill {
  -webkit-box-shadow: 0 0 0px 1000px white inset !important;
  -webkit-text-fill-color: #333 !important;
  transition: background-color 5000s ease-in-out 0s;
}

/* 透明背景方案 */
input:-webkit-autofill {
  -webkit-background-clip: text;
}

美化 Placeholder

::placeholder {
  color: #999;
  font-size: 14px;
  opacity: 1;
}

/* 聚焦时隐藏 placeholder */
input:focus::placeholder {
  opacity: 0;
  transition: opacity 0.3s;
}

去除 Input 默认样式

input,
textarea {
  outline: none; /* 去除聚焦边框 */
  border: none; /* 去除边框 */
  background: none; /* 去除背景 */
  appearance: none; /* 去除默认样式 */
  -webkit-appearance: none;
  -moz-appearance: none;
}

/* 去除 number 类型的上下箭头 */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

美化复选框和单选框

/* 自定义复选框 */
.custom-checkbox {
  display: none;
}

.custom-checkbox + label {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}

.custom-checkbox + label::before {
  content: "";
  position: absolute;
  left: 0;
  top: 2px;
  width: 18px;
  height: 18px;
  border: 2px solid #ddd;
  border-radius: 3px;
  transition: all 0.3s;
}

.custom-checkbox:checked + label::before {
  background: #667eea;
  border-color: #667eea;
}

.custom-checkbox:checked + label::after {
  content: "✓";
  position: absolute;
  left: 4px;
  top: 0;
  color: white;
  font-size: 14px;
}

图形绘制

三角形

.triangle-up {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 50px solid #333;
}

.triangle-down {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 50px solid #333;
}

.triangle-left {
  width: 0;
  height: 0;
  border-top: 50px solid transparent;
  border-bottom: 50px solid transparent;
  border-right: 50px solid #333;
}

.triangle-right {
  width: 0;
  height: 0;
  border-top: 50px solid transparent;
  border-bottom: 50px solid transparent;
  border-left: 50px solid #333;
}

对话框箭头

.tooltip {
  position: relative;
  background: #333;
  color: white;
  padding: 10px;
  border-radius: 4px;
}

.tooltip::after {
  content: "";
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-top: 10px solid #333;
}

圆形、半圆

/* 圆形 */
.circle {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: #667eea;
}

/* 半圆 */
.semicircle {
  width: 100px;
  height: 50px;
  border-radius: 100px 100px 0 0;
  background: #667eea;
}

心形

.heart {
  position: relative;
  width: 100px;
  height: 90px;
}

.heart::before,
.heart::after {
  content: "";
  position: absolute;
  top: 0;
  width: 52px;
  height: 80px;
  background: red;
  border-radius: 50px 50px 0 0;
}

.heart::before {
  left: 50px;
  transform: rotate(-45deg);
  transform-origin: 0 100%;
}

.heart::after {
  left: 0;
  transform: rotate(45deg);
  transform-origin: 100% 100%;
}

动画效果

淡入淡出

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.fade-in {
  animation: fadeIn 0.3s ease-in;
}

@keyframes fadeOut {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

.fade-out {
  animation: fadeOut 0.3s ease-out;
}

缩放动画

@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.8);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.scale-in {
  animation: scaleIn 0.3s ease-out;
}

滑入动画

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.slide-in-right {
  animation: slideInRight 0.3s ease-out;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.slide-in-up {
  animation: slideInUp 0.3s ease-out;
}

旋转加载动画

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: rotate 1s linear infinite;
}

脉冲动画

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.7;
  }
}

.pulse {
  animation: pulse 2s ease-in-out infinite;
}

抖动动画

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  10%,
  30%,
  50%,
  70%,
  90% {
    transform: translateX(-10px);
  }
  20%,
  40%,
  60%,
  80% {
    transform: translateX(10px);
  }
}

.shake {
  animation: shake 0.5s;
}

交互效果

鼠标悬停效果

/* 颜色渐变 */
.hover-color {
  transition: color 0.3s, background-color 0.3s;
}

.hover-color:hover {
  color: white;
  background-color: #667eea;
}

/* 阴影效果 */
.hover-shadow {
  transition: box-shadow 0.3s;
}

.hover-shadow:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 缩放效果 */
.hover-scale {
  transition: transform 0.3s;
}

.hover-scale:hover {
  transform: scale(1.05);
}

/* 上移效果 */
.hover-lift {
  transition: transform 0.3s, box-shadow 0.3s;
}

.hover-lift:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
}

按钮点击效果

.button-press {
  transition: transform 0.1s;
}

.button-press:active {
  transform: scale(0.95);
}

平滑滚动

html {
  scroll-behavior: smooth;
}

/* 或者使用 JavaScript */
/* element.scrollIntoView({ behavior: 'smooth' }); */

响应式设计

媒体查询断点

/* 移动设备 */
@media (max-width: 767px) {
  .container {
    padding: 10px;
  }
}

/* 平板设备 */
@media (min-width: 768px) and (max-width: 1023px) {
  .container {
    padding: 20px;
  }
}

/* 桌面设备 */
@media (min-width: 1024px) {
  .container {
    padding: 30px;
  }
}

响应式字体

/* 使用 clamp */
.responsive-text {
  font-size: clamp(16px, 2vw, 24px);
  /* 最小 16px,理想 2vw,最大 24px */
}

/* 使用媒体查询 */
.text {
  font-size: 14px;
}

@media (min-width: 768px) {
  .text {
    font-size: 16px;
  }
}

@media (min-width: 1024px) {
  .text {
    font-size: 18px;
  }
}

响应式容器

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

@media (min-width: 768px) {
  .container {
    padding: 0 30px;
  }
}

实用技巧

清除浮动

.clearfix::after {
  content: "";
  display: block;
  clear: both;
}

1px 边框问题(移动端)

/* 使用伪元素 + transform */
.border-1px {
  position: relative;
}

.border-1px::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 200%;
  height: 200%;
  border: 1px solid #e5e5e5;
  transform: scale(0.5);
  transform-origin: left top;
}

强制换行

/* 强制不换行 */
.no-wrap {
  white-space: nowrap;
}

/* 允许换行 */
.wrap {
  word-wrap: break-word;
  word-break: break-all;
}

选择文本样式

::selection {
  background: #667eea;
  color: white;
}

::-moz-selection {
  background: #667eea;
  color: white;
}

占位符高度撑开

/* 用于图片懒加载占位 */
.aspect-ratio-16-9 {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%; /* 16:9 = 9/16 = 0.5625 */
}

.aspect-ratio-16-9 > * {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

毛玻璃效果

.glass {
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
}

文字竖排

.vertical-text {
  writing-mode: vertical-rl; /* 从右到左 */
  /* writing-mode: vertical-lr; 从左到右 */
}

多列布局

.columns {
  column-count: 3;
  column-gap: 20px;
  column-rule: 1px solid #ddd;
}

总结

这些 CSS 代码片段涵盖了日常开发中最常见的需求。建议你:

  1. 收藏本文,需要时快速查找
  2. 创建自己的代码片段库,积累项目经验
  3. 使用 CSS 预处理器(Sass/Less)管理这些片段
  4. 关注浏览器兼容性,必要时添加前缀

记住:好的代码片段能让你事半功倍,但理解原理更重要

相关资源