120个 实用CSS 技巧汇总合集

www.jswusn.com CSS 2025-06-27 10:12:58 5次浏览

在前端开发中,CSS 往往是最被低估的一环。但真正优秀的开发者,往往懂得如何用 CSS 写出高效、优雅又强大的界面。

无论你是刚入门的新手,还是正在精进的前端工程师,这 100 个经过精选分类的 CSS 小技巧,都会帮你解决常见痛点,提升页面性能与交互体验。

从布局排版到动画过渡,从表单交互到组件样式,每一个技巧都附有简洁示例,贴近实战,拿来即用。让我们一起重拾 CSS 的乐趣,写出更有表现力的前端页面。

一、布局技巧

1. 居中一个元素(水平 + 垂直)


.center {  
    display: flex;  
    justify-content: center;  
    align-items: center;
}



2. 响应式宽度限制


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



3. CSS Grid 快速布局三列


.grid {  
    display: grid;  
    grid-template-columns: repeat(3, 1fr);  
    gap: 1rem;
}



4. Flex 快速平分布局


.flex-split {  
    display: flex;
}
.flex-split > div {  
    flex: 1;
}



5. sticky 固定导航栏


nav {  
    position: sticky;  
    top: 0;  
    background: white;
}



6. 双栏固定 + 自适应布局


.layout {  
    display: grid;  
    grid-template-columns: 200px auto;
}



7. 圣杯布局


body {  
    display: flex;
}
.left, .right {  
    width: 200px;
}
.center {  
    flex: 1;
}



8. 内容高度占满剩余空间


.wrapper {  
    display: flex;  
    flex-direction: column;  
    height: 100vh;
}
.content {  
    flex: 1;
}



9. 媒体查询适配移动端


@media (max-width: 768px) {  
    .grid {    
        grid-template-columns: 1fr;  
    }
}



10. 强制换行


.break-word {  
    word-break: break-word;
}



11. 固定宽高比例容器(如16:9)


.ratio {  
    position: relative;  
    padding-top: 56.25%; /* 16:9 */
}
.ratio > iframe {  
    position: absolute;  
    top: 0; 
    left: 0;  
    width: 100%; 
    height: 100%;
}



12. 单行文本省略号


.ellipsis {  
    overflow: hidden;  
    text-overflow: ellipsis;  
    white-space: nowrap;
}



13. 多行文本省略号


.multi-ellipsis {  
    display: -webkit-box;  
    -webkit-line-clamp: 3;  
    -webkit-box-orient: vertical;  
    overflow: hidden;
}



14. 子元素自动换行


.flex-wrap {  
    display: flex;  
    flex-wrap: wrap;
}



15. 等高卡片布局


.card-group {  
    display: flex;
}
.card-group .card {  
    flex: 1;  
    display: flex;  
    flex-direction: column;
}



16. 垂直居中(非 Flex)

.parent {  
    position: relative;
}
.child {  
    position: absolute;  
    top: 50%;  
    transform: translateY(-50%);
}



17. 布局自适应缩放字体大小

html {  
    font-size: clamp(14px, 2vw, 18px);
}



18. flex 实现等间距分布

.space-between {  
    display: flex;  
    justify-content: space-between;
}



19. 内容自适应图片

img {  
    max-width: 100%;  
    height: auto;
}



20. Grid 网格自动填充列

.grid-auto {  
    display: grid;  
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));  
    gap: 1rem;
}



二、视觉样式技巧

21. 自定义滚动条

::-webkit-scrollbar {  
    width: 8px;
}
::-webkit-scrollbar-thumb {  
    background: #ccc;  
    border-radius: 4px;
}



22. 纯CSS渐变边框

.border-gradient {  
    border: 4px solid transparent;  
    background-clip: padding-box, border-box;  
    background-origin: padding-box, border-box;  
    background-image: linear-gradient(#fff, #fff),    
    linear-gradient(to right, #f06, #4a90e2);
}



23. 毛玻璃效果

.blur-bg {  
    background: rgba(255,255,255,0.3);  
    backdrop-filter: blur(10px);
}



24. 渐变文字

.gradient-text {  
    background: linear-gradient(to right, #f06, #4a90e2);  
    -webkit-background-clip: text;  
    -webkit-text-fill-color: transparent;
}



25. 图片变灰滤镜

img.gray {  
    filter: grayscale(100%);
}



26. 自定义光标

.custom-cursor {  
    cursor: url(cursor.png), auto;
}



27. 图标 hover 动画

.icon:hover {  
    transform: scale(1.2);  
    transition: transform 0.2s ease-in;
}



28. 半透明遮罩层

.overlay {  
    background-color: rgba(0, 0, 0, 0.5);
}



29. 设置图片圆角头像

.avatar {  
    border-radius: 50%;
}



30. 投影增强立体感

.shadow-box {  
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}



31.自定义文本选择颜色

::selection {  
    background: #ffb7b7;  
    color: #000;
}



32.跳动心形动画

@keyframes pulse {  
    0% { 
        transform: scale(1); 
    }  
    50% { 
        transform: scale(1.1); 
    }  
    100% { 
        transform: scale(1); 
    }
}
.heart {  
    animation: pulse 1s infinite;
}



33.带阴影边框文字

.text-shadow {  
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}



34.虚线边框动画

.dashed-animate {  
    border: 2px dashed #000;  
        animation: dashmove 4s linear infinite;
    }
    @keyframes dashmove {  
        to {    
            background-position: 100% 0;  
        }
    }
}



35. hover 变暗按钮

button:hover {  
    filter: brightness(90%);
}



36. 高亮当前导航链接

nav a.active {  
    color: #f00;  
    font-weight: bold;
}



37. 使用 CSS clip-path 制作形状

.clip-circle {  
    clip-path: circle(50%);
}



38. 霓虹灯文字效果

.neon {  
    color: #fff;  
    text-shadow: 0 0 5px #0ff, 0 0 10px #0ff;
}



39. 动态加载效果

.loading {  
    background: linear-gradient(90deg, #eee, #ccc, #eee);  
    background-size: 200% 100%;  
    animation: shimmer 1.5s infinite;
}
@keyframes shimmer {  
    100% { 
        background-position: -200% 0; 
    }
}



40. 设置文字描边

.stroke-text {  
    color: white;  
    -webkit-text-stroke: 1px black;
}



三、表单与交互技巧

41. 自定义复选框样式

input[type="checkbox"] {  
    accent-color: #f06;
}



42. placeholder 字体颜色

input::placeholder {  
    color: #999;
}



43. 输入框获得焦点样式

input:focus {  
    outline: none;  
    border-color: #4a90e2;  
    box-shadow: 0 0 5px rgba(74,144,226,0.5);
}


44. 禁止输入框缩放(移动端)

input, textarea {  
    font-size: 16px;
}


45. 表单验证成功样式

input:valid {  
    border-color: green;
}



46. 表单验证失败样式

input:invalid {  
    border-color: red;
}



47. 仅样式第一个或最后一个输入框

input:first-of-type {  
    border-top-left-radius: 8px;
}
input:last-of-type {  
    border-bottom-left-radius: 8px;
}



48. 按钮禁用状态样式

button:disabled {  
    background-color: #ccc;  
    cursor: not-allowed;
}



49. 设置全局按钮样式

button {  
    padding: 10px 20px;  
    border: none;  
    background: #4a90e2;  
    color: #fff;  
    border-radius: 5px;  
    cursor: pointer;
}


50. 自定义 radio 样式(基础)

input[type="radio"] {  
    accent-color: #ff5722;
}


51. 禁止输入框拖动文本(移动端)

input {  
    -webkit-user-select: none;  
    user-select: none;
}


52. 提交按钮 hover 效果

button:hover {  
    background: #357ab7;
}


53. 透明背景输入框

input {  
    background: transparent;  
    border: 1px solid #ccc;
}


54. 用 label 美化上传按钮

<label for="file">上传文件</label>
<input type="file" id="file" hidden>


55. 鼠标悬停切换 input 边框色

input:hover {  
    border-color: #4caf50;
}


56. checkbox 开关按钮样式(切换器)

.toggle input:checked + span {  
    background: #4caf50;
}


57. input 自动填充样式(兼容 Chrome)

input:-webkit-autofill {  
    background-color: #e0f7fa !important;
}


58. 自定义 select 下拉样式(简洁版)

select {  
    appearance: none;  
    background: url('arrow-down.svg') no-repeat right;  
    padding-right: 20px;
}


59. 限制只能输入数字

input[type="number"] {  
    -moz-appearance: textfield;
}


60. 表单输入框光标颜色

input {  
    caret-color: #ff4081;
}


四、性能与响应式技巧

61. 设置图像懒加载

<img src="image.jpg" loading="lazy">


62. 使用 will-change 提前渲染动画元素

.box {  
    will-change: transform;
}


63. 减少重绘:统一触发动画属性

.fast {  
    transform: translateY(10px);  
    opacity: 0.8;  
    transition: all 0.3s ease;
}


64. 使用 contain 限制重排范围

.card {  
    contain: layout paint;
}


65. 优化字体渲染

body {  
    -webkit-font-smoothing: antialiased;
}


66. 禁止图片拖拽

img {  
    user-drag: none;
}


67. 限制图片最大宽度防止溢出

img {  
    max-width: 100%;  
    height: auto;
}


68. 提高点击响应速度

a, button {  
    touch-action: manipulation;
}


69. 媒体查询:切换浅色/深色主题

@media (prefers-color-scheme: dark) {  
    body {    
        background: #222;    
        color: #fff;  
    }
}


70. 字体大小根据屏幕缩放

body {  
    font-size: clamp(14px, 1.5vw, 18px);
}


71. 使用 aspect-ratio 设置图片比例

img {  
    aspect-ratio: 16 / 9;  
    width: 100%;
}


72. 自适应弹性网格

.grid {  
    display: grid;  
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}


73. 只在需要时开启动画

@media (prefers-reduced-motion: reduce) {  
    * {    
        animation: none !important;    
        transition: none !important;  
    }
}


74. 防止 iOS 字体放大

html {  
    -webkit-text-size-adjust: 100%;
}


75. 设置固定视口单位避免移动端跳动

:root {  
    --vh: 100vh;
}


76. 移动端一键适配缩放

<meta name="viewport" content="width=device-width, initial-scale=1">


77. 适配 Retina 屏图片

@media (-webkit-min-device-pixel-ratio: 2) {  
    .logo {    
        background-image: url("logo@2x.png");  
    }
}


78. 控制响应式卡片宽度

.card {  
    max-width: 400px;  
    margin: auto;
}


79. 媒体查询响应式字体

@media (max-width: 600px) {  
    h1 {    
        font-size: 1.5rem;  
    }
}


80. 使用 object-fit 填充图像

img.cover {  
    width: 100%;  
    height: 200px;  
    object-fit: cover;
}


五、动画与过渡技巧

81. 渐变背景无限动画

@keyframes gradientMove {  
    0% { 
        background-position: 0 0; 
    }  
    100% { 
        background-position: 100% 0; 
    }
}
.bg-animate {  
    background: linear-gradient(270deg, #f06, #4a90e2);  
    background-size: 200% 100%;  
    animation: gradientMove 5s infinite linear;
}


82. 按钮点击波纹动画

button {  
    position: relative;  
    overflow: hidden;
}
button::after {  
    content: '';  
    position: absolute;  
    width: 100%;  
    height: 100%;  
    background: rgba(255,255,255,0.5);  
    animation: ripple 0.6s ease-out;
}
@keyframes ripple {  
    from { 
        opacity: 1; 
        transform: scale(0); 
    }  
    to { 
        opacity: 0; 
        transform: scale(2); 
    }
}


83. 页面加载过渡动画

.fade-in {  
    opacity: 0;  
    animation: fadeIn 1s ease forwards;
}
@keyframes fadeIn {  
    to { 
        opacity: 1; 
    }
}


84. 悬浮卡片弹起

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


85.自定义 loading 圆圈

.loader {  
    border: 4px solid #eee;  
    border-top: 4px solid #4a90e2;  
    border-radius: 50%;  
    width: 30px;  
    height: 30px;  
    animation: spin 1s linear infinite;
}
@keyframes spin {  
    100% { 
        transform: rotate(360deg); 
    }
}


86. 渐隐渐现切换内容

.fade {  
    transition: opacity 0.5s ease-in-out;
}
.fade.hidden {  
    opacity: 0;
}


87. 抖动动画(错误提示)

@keyframes shake {  
    0%, 100% { 
        transform: translateX(0); 
    }  
    25% { 
        transform: translateX(-5px); 
    }  
    75% { 
        transform: translateX(5px); 
    }
}
.shake {  
    animation: shake 0.3s;
}


88. 按钮点击缩放动画

button:active {  
    transform: scale(0.95);
}


89. 文字打字机效果

.typing {  
    overflow: hidden;  
    white-space: nowrap;  
    animation: typing 3s steps(30) forwards;
}
@keyframes typing {  
    from { 
        width: 0; 
    }  
    to { 
        width: 100%; 
    }
}


90. 滚动条平滑滚动

html {  
    scroll-behavior: smooth;
}


六、实用小组件技巧

91. 标签页切换(纯 CSS)

<input type="radio" name="tab" id="tab1" checked>
<input type="radio" name="tab" id="tab2">
<div class="tabs">  
    <label for="tab1">标签1</label>  
    <label for="tab2">标签2</label>
</div>
<div class="content">  
    <div class="tab1-content">内容1</div>  
    <div class="tab2-content">内容2</div>
</div>
#tab1:checked ~ .content .tab1-content { 
    display: block; 
}
#tab2:checked ~ .content .tab2-content { 
    display: block; 
}
.tab1-content, .tab2-content { 
    display: none; 
}



92. 纯 CSS 手风琴菜单

<input type="checkbox" id="accordion1">
<label for="accordion1">展开内容</label>
<div class="panel">这里是内容</div>
#accordion1:not(:checked) ~ .panel {  
    display: none;
}



93. CSS 打勾动画

.checkmark {  
    width: 30px;  
    height: 30px;  
    border: 2px solid #4caf50;  
    transform: rotate(45deg);  
    border-left: none;  
    border-top: none;  
    animation: check 0.3s ease forwards;
}
@keyframes check {  
    0% { 
        width: 0; 
        height: 0; 
    }  
    100% { 
        width: 30px; 
        height: 30px; 
    }
}


94. 进度条动画

.progress {  
    width: 100%;  
    background: #eee;
}
.progress-bar {  
    height: 10px;  
    width: 0%;  
    background: #4a90e2;  
    animation: fill 2s forwards;
}
@keyframes fill {  
    to { 
        width: 80%; 
    }
}


95. 模态弹窗样式(配合 JS 控制)

.modal {  
    position: fixed;  
    inset: 0;  
    background: rgba(0,0,0,0.5);  
    display: flex;  
    justify-content: center;  
    align-items: center;
}
.modal-content {  
    background: white;  
    padding: 2rem;  
    border-radius: 10px;
}


96. 纯 CSS 弹出提示 Tooltip

<button class="tooltip" data-title="提示内容">悬停我</button>
.tooltip {  
    position: relative;
}
.tooltip::after {  
    content: attr(data-title);  
    position: absolute;  
    bottom: 100%;  
    background: #333;  
    color: white;  
    font-size: 12px;  
    padding: 5px;  
    white-space: nowrap;  
    border-radius: 5px;  
    opacity: 0;  
    transform: translateY(10px);  
    transition: 0.3s;
}
.tooltip:hover::after {  
    opacity: 1;  
    transform: translateY(0);
}



97. 纯 CSS 标签/徽章(Badge)

.badge {  
    background: red;  
    color: white;  
    padding: 2px 6px;  
    border-radius: 999px;  
    font-size: 12px;
}


98. 折叠菜单(侧边栏)

.sidebar {  
    width: 0;  
    overflow: hidden;  
    transition: 
    width 0.3s;
}
.sidebar.open {  
    width: 200px;
}


99. 响应式卡片 hover 信息浮现

.card {  
    position: relative;  
    overflow: hidden;
}
.card-info {  
    position: absolute;  
    bottom: -100%;  
    background: rgba(0,0,0,0.7);  
    color: white;  
    width: 100%;  
    padding: 1rem;  
    transition: bottom 0.3s;
}
.card:hover .card-info {  
    bottom: 0;
}


100. 星级评分组件(伪元素)

.rating {  
    display: flex;
}
.rating::before {  
    content: '★★★★★';  
    letter-spacing: 3px;  
    background: linear-gradient(90deg, gold 60%, #ccc 0%);  
    -webkit-background-clip: text;  
    color: transparent;
}


101.CSS 图片放大镜效果

.zoom {  
    overflow: hidden;
}
.zoom img {  
    transition: 0.3s;
}
.zoom:hover img {  
    transform: scale(1.2);
}


102.CSS 时间轴

.timeline {  
    position: relative;  
    border-left: 2px solid #4a90e2;  
    padding-left: 20px;
}
.timeline-item {  
    margin-bottom: 20px;  
    position: relative;
}
.timeline-item::before {  
    content: '';  
    position: absolute;  
    left: -9px;  
    top: 0;  
    width: 10px;  
    height: 10px;  
    background: #4a90e2;  
    border-radius: 50%;
}


103.输入框带搜索图标

.search {  
    background: url(search-icon.svg) no-repeat 10px center;  
    padding-left: 30px;
}


104.CSS 骨架屏(Skeleton)

.skeleton {  
    background: linear-gradient(90deg, #eee, #ddd, #eee);  
    background-size: 200% 100%;  
    animation: skeleton 1.2s infinite;
}
@keyframes skeleton {  
    100% { 
        background-position: -200% 0; 
    }
}


105.CSS 数字翻牌效果(纯动画)

@keyframes flipIn {  
    0% { 
        transform: rotateX(-90deg); 
        opacity: 0; 
    }  
    100% { 
        transform: rotateX(0); 
        opacity: 1; 
    }
}
.flip-number {  
    animation: flipIn 0.5s ease;
}


106.卡片阴影层级提升

.card {  
    transition: box-shadow 0.3s;
}
.card:hover {  
    box-shadow: 0 12px 24px rgba(0,0,0,0.2);
}


107.图标徽章位置(右上角)

.icon-wrapper {  
    position: relative;
}
.badge {  
    position: absolute;  
    top: 0;  
    right: 0;  
    background: red;  
    color: white;  
    font-size: 10px;  
    border-radius: 50%;  
    padding: 2px 6px;
}


108.拖拽指示样式

.draggable {  
    cursor: grab;
}
.draggable:active {  
    cursor: grabbing;
}


109.CSS 时间倒计时样式(需 JS 逻辑)

.countdown {  
    font-family: monospace;  
    font-size: 2rem;  
    background: #000;  
    color: #0f0;  
    padding: 10px;
}


120.滚动提示箭头动画

.scroll-down::after {  
    content: '↓';  
    display: block;  
    animation: bounce 1s infinite;
}
@keyframes bounce {  
    0%, 100% { 
        transform: translateY(0); 
    }  
    50% { 
        transform: translateY(10px); 
    }
}


写在最后

以上,就是我今天整理的自实际开发中的 100+ 个高频 CSS 技巧。如果你觉得实用,建议收藏并多加练习,逐步内化为自己的前端“肌肉记忆”。你也可以将这份技巧清单应用到项目优化、组件封装和样式规范中,减少冗余代码,提升 UI 品质。


上一篇:没有了!

CSS

下一篇:一文搞懂前端 Flex 布局:从入门到实战

技术分享

苏南名片

  • 联系人:吴经理
  • 电话:152-1887-1916
  • 邮箱:message@jswusn.com
  • 地址:江苏省苏州市相城区

热门文章

Copyright © 2018-2025 jswusn.com 版权所有

技术支持:苏州网站建设  苏ICP备18036849号