以下是20个实用的CSS单行代码解决方案,覆盖布局、样式优化、交互效果等常见开发痛点,可直接复制使用:
.container { display: flex; justify-content: center; align-items: center; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hide-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
.hide-scrollbar::-webkit-scrollbar { display: none; } /* WebKit兼容 */
.aspect-ratio-box { aspect-ratio: 16/9; } /* 16:9比例 */
.blur-bg { backdrop-filter: blur(10px); }
.no-select { user-select: none; }
html { scroll-behavior: smooth; }
.break-word { word-break: break-all; }
.no-list-style { list-style: none; padding: 0; margin: 0; }
input:focus { outline: none; }
.custom-scrollbar { scrollbar-color: #4a5568 #cbd5e0; }
.responsive-img { object-fit: cover; }
button { -webkit-tap-highlight-color: transparent; }
.indent { text-indent: 2em; }
.box-shadow { box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1); }
.transition { transition: all 0.3s ease-in-out; }
.rotate { transform: rotate(45deg); }
.sticky-nav { position: sticky; top: 0; }
.gradient-bg { background: linear-gradient(45deg, #ff6b6b, #4ecdc4); }
@media (prefers-color-scheme: dark) { :root { background: #1a1a1a; color: #fff; } }
这些代码可直接嵌入项目,减少重复代码量,提升开发效率!