🎯 UnoCSS终极速查表:这些原子类让你开发效率翻倍!

665 阅读4分钟

还在为CSS样式烦恼吗?UnoCSS的原子化类名让你告别重复劳动!下面是一份超实用的UnoCSS常用类名速查表,收藏这一篇就够了!

📊 UnoCSS常用类名速查表

类别类名示例说明等效CSS
宽度w-100px w-full w-100vw宽度控制width: 100px width: 100% width: 100vw
高度h-100vh h-50px min-h-200px高度控制height: 100vh height: 50px min-height: 200px
内边距p-10px px-10px py-10px pt-10 pr-10 pb-10 pl-10内边距控制padding: 10px padding-left/right: 10px padding-top/bottom: 10px
外边距m-10px mx-auto my-10 mt-10 mr-10 mb-10 ml-10外边距控制margin: 10px margin-left/right: auto margin-top/bottom: 10px
边框border-1px border-solid border-#ffffff border-t边框样式border-width: 1px border-style: solid border-color: #ffffff border-top: 1px solid
圆角rounded-10px rounded-full rounded-tl-lg圆角控制border-radius: 10px border-radius: 9999px border-top-left-radius: 0.5rem
Flex布局flex flex-col justify-center items-center gap-4Flex布局display: flex flex-direction: column justify-content: center align-items: center gap: 1rem
Grid布局grid grid-cols-24 grid-rows-2 gap-4Grid布局display: grid grid-template-columns: repeat(24, minmax(0, 1fr)) grid-template-rows: repeat(2, minmax(0, 1fr)) gap: 1rem
背景bg-#fff bg-gradient-to-r from-blue-500 to-purple-600背景样式background-color: #fff background-image: linear-gradient(to right, ...)
颜色c-#fff text-#999颜色控制color: #fff color: #999
字体大小text-12px text-sm text-lg text-xl字体大小font-size: 12px font-size: 0.875rem font-size: 1.125rem font-size: 1.25rem
字体粗细font-400 font-bold font-light字体粗细font-weight: 400 font-weight: 700 font-weight: 300
文字颜色text-#999 text-red-500 text-blue-500/50文字颜色color: #999 color: #ef4444 color: rgba(59, 130, 246, 0.5)
行高leading-20px leading-normal leading-relaxed行高控制line-height: 20px line-height: 1.5 line-height: 1.625
阴影shadow-[10px_0_16px_0_rgba(0,0,0,0.06)] shadow-lg阴影效果box-shadow: 10px 0 16px 0 rgba(0,0,0,0.06) box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1)
层级z-100 z-10 z-auto层级控制z-index: 100 z-index: 10 z-index: auto
盒子模型box-border box-content盒子模型box-sizing: border-box box-sizing: content-box
定位absolute top-0 right-0 bottom-0 left-0 fixed sticky定位控制position: absolute top: 0 right: 0 bottom: 0 left: 0
悬停效果hover:bg-#ccc hover:scale-105 hover:text-blue-500悬停状态&:hover { background: #ccc } &:hover { transform: scale(1.05) }
超出隐藏overflow-x-hidden overflow-y-auto overflow-hidden溢出控制overflow-x: hidden overflow-y: auto overflow: hidden
文字换行whitespace-nowrap text-align-end text-center文字处理white-space: nowrap text-align: end text-align: center
文本溢出whitespace-nowrap overflow-hidden text-ellipsis文本溢出white-space: nowrap overflow: hidden text-overflow: ellipsis
响应式sm:w-full md:w-1/2 lg:w-1/3 xl:w-1/4响应式设计@media (min-width: 640px) { width: 100% }
暗黑模式dark:bg-gray-800 dark:text-white暗黑模式@media (prefers-color-scheme: dark) { background: #1f2937 }
过渡动画transition-all duration-300 ease-in-out动画效果transition: all 0.3s ease-in-out
透明度opacity-50 opacity-100透明度控制opacity: 0.5 opacity: 1
光标cursor-pointer cursor-not-allowed光标样式cursor: pointer cursor: not-allowed
显示隐藏hidden block inline inline-block显示控制display: none display: block display: inline display: inline-block

🚀 实用组合示例

按钮样式

<button class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-all">
  点击我
</button>

卡片组件

<div class="p-6 bg-white rounded-lg shadow-md hover:shadow-lg transition-shadow">
  <h3 class="text-xl font-bold text-gray-800">卡片标题</h3>
  <p class="text-gray-600 mt-2">卡片内容描述...</p>
</div>

导航菜单

<nav class="flex gap-4 p-4 bg-gray-100">
  <a class="px-3 py-2 text-blue-600 hover:bg-blue-100 rounded" href="#">首页</a>
  <a class="px-3 py-2 text-gray-600 hover:bg-gray-100 rounded" href="#">关于</a>
</nav>

模态框

<div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
  <div class="bg-white rounded-lg p-6 w-96">
    <h2 class="text-lg font-bold mb-4">模态框标题</h2>
    <p class="text-gray-600">模态框内容...</p>
  </div>
</div>

💡 使用技巧

  1. 组合使用:多个类名可以组合使用,实现复杂样式
  2. 响应式前缀:使用sm:md:lg:xl:前缀实现响应式设计
  3. 状态前缀:使用hover:focus:active:等前缀处理交互状态
  4. 暗黑模式:使用dark:前缀支持暗黑模式
  5. 自定义值:使用方括号[value]支持任意CSS值

🎯 为什么选择UnoCSS?

  • 按需生成:只生成使用的样式,体积极小
  • 零配置:开箱即用,无需复杂配置
  • 高度可定制:轻松扩展自定义规则
  • 性能优异:编译速度极快,开发体验流畅

这份速查表涵盖了UnoCSS最常用的类名,收藏起来随时查阅,让你的开发效率提升300%!

提示:UnoCSS支持无限扩展,你可以根据项目需求自定义规则和快捷方式,打造专属的原子CSS系统!