一、布局黑科技
1. 居中元素终极方案
/* 水平垂直居中 */
.center-box {
display: grid;
place-items: center;
}
/* 传统方案兼容 */
.old-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
2. 等高分栏布局
.container {
display: flex;
}
.column {
flex: 1; /* 等分宽度 */
min-height: 100vh; /* 等高 */
}
二、响应式适配妙招
1. 容器查询(最新特性)
.card {
container-type: inline-size;
}
@container (min-width: 400px) {
.card {
flex-direction: row;
}
}
2. 隐藏滚动条(保持功能)
.scroll-container {
scrollbar-width: none; /* Firefox */
-ms-overflow-style: none; /* IE */
}
.scroll-container::-webkit-scrollbar {
display: none; /* Chrome/Safari */
}
三、视觉效果优化
1. 渐变文字效果
.gradient-text {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
2. 自定义下划线
.custom-underline {
text-decoration: none;
background: linear-gradient(90deg, red, blue) bottom left / 0% 2px no-repeat;
transition: background-size 0.3s;
}
.custom-underline:hover {
background-size: 100% 2px;
}
四、动画与交互
1. 悬浮放大效果
.zoom-card {
transition: transform 0.3s;
}
.zoom-card:hover {
transform: scale(1.05);
}
2. 骨架屏加载动画
@keyframes skeleton {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
.skeleton {
background: linear-gradient(90deg, #eee 25%, #ddd 50%, #eee 75%);
background-size: 200% 100%;
animation: skeleton 1.5s infinite linear;
}
五、表单美化技巧
1. 自定义复选框
input[type="checkbox"] {
appearance: none;
width: 20px;
height: 20px;
border: 2px solid #333;
border-radius: 4px;
position: relative;
}
input[type="checkbox"]:checked::after {
content: "✓";
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
2. 输入框聚焦效果
.input-field {
border: 2px solid #ddd;
transition: all 0.3s;
}
.input-field:focus {
border-color: #4ecdc4;
box-shadow: 0 0 8px rgba(78, 205, 196, 0.3);
}
六、性能优化技巧
1. 硬件加速优化
.animate-box {
transform: translateZ(0);
will-change: transform;
}
2. 图片懒加载样式
img[data-src] {
opacity: 0;
transition: opacity 0.3s;
}
img.loaded {
opacity: 1;
}
七、实用工具类
1. 单行文字截断
.ellipsis {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
2. 多行文字截断(2行)
.multi-ellipsis {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
八、CSS 变量妙用
1. 主题切换
:root {
--primary-color: #42b983;
--bg-color: #fff;
}
.dark-theme {
--primary-color: #4ecdc4;
--bg-color: #333;
}
body {
background: var(--bg-color);
color: var(--primary-color);
}
2. 动态计算尺寸
.box {
width: calc(100% - 40px);
height: calc(100vh - var(--header-height));
}
九、伪元素创意用法
1. 价格标签效果
.price-tag::after {
content: "¥";
margin-left: 2px;
color: #ff6b6b;
}
2. 必填项红星标记
.required::before {
content: "*";
color: red;
margin-right: 4px;
}
十、调试技巧
1. 元素轮廓调试
css
复制
.debug * {
outline: 1px solid rgba(255, 0, 0, 0.2);
}
2. 响应式断点标记
body::after {
content: "Mobile";
position: fixed;
bottom: 0;
right: 0;
background: red;
color: white;
padding: 2px 5px;
}
@media (min-width: 768px) {
body::after { content: "Tablet"; }
}
@media (min-width: 1200px) {
body::after { content: "Desktop"; }
}
代码对比示例:
/* 传统写法 */
.box {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
}
/* 现代简写 */
.box {
margin: 10px 20px;
}