小弟我之前一直是使用 Bootstrap 做为前端 CSS 框架,由於最近听到很多 Tailwind CSS 的花式吹捧,让我也开始感兴趣了,今天就一起来研究一下吧!
官网一开始就有说到,Tailwind CSS 是一个 utility-first 的框架,并且使用 PostCSS 做开发,而特色如下
class
inline style
还提供 hover、focus 等等伪类 class
class
达到模组化utility-first 即是说以共用 CSS 为主,像是 Bootstrap 中的 text-center
、mx-4
等等
PostCSS 是使用 JavaScript 转换 CSS 的工具,归类为後处理器,与预处理器的 SASS 为两种不同的东西,预处理器将撰写好的 Code 编译成 CSS,而後处理器则是把 Code 依照设定与插件做加工,实际上 Tailwind 的运行方式就是将 CSS 经过 PostCSS 内的 Tailwind 插件做加工,进而产出需要的档案
用 VSCode 写 Tailwind 时有很好的插件辅助,会依照 tailwind.config.js
提示可使用的 class
Tailwind 有许多 css 不认识的语法,所以撰写上可能会有很多错误提示,可以在 settings.json
加入以下设定,想了解更多设定可以看这里
// settings.json
{
"css.validate": false
}
tailwindcss
、postcss
与 autoprefixer
$ npm install tailwindcss postcss autoprefixer
tailwind.config.js
$ npx tailwindcss init
/* style.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
style.css
编译为 tailwind.css
$ npx tailwindcss build ./style.css -o ./tailwind.css
这边的 tailwind.css
就是完整的 CSS 档罗!
在看语法之前我们先看一下 Tailwind 的 CSS Reset
,它采用的是 normalize
的版本,但在此之上又加了许多设定,以下有几个比较重要的
margin
为 0h1 ~ h6
的字体大小与粗细同一般文字svg
与 img
等等图像为 display: block
border
颜色预设 #e5e7eb
,宽度为 0当然还有其他的设定,详细可以看官网,如果不想要这些设定则可以将 preflight
改为 false
// tailwind.config.js
module.exports = {
corePlugins: {
preflight: false
}
}
Tailwind 内有许多自己的语法,包含我们一开始看到的 @tailwind
,还有其他像是 @apply
或 @layer
等等,以下会一一介绍,详细也可以直接看官网
用来载入官方提供的样式
@tailwind screens
可省略,预设会载入在最後方@tailwind base; /* 基础样式 */
@tailwind components; /* 模组样式 */
@tailwind utilities; /* 共用样式 */
@tailwind screens; /* 响应式样式 */
用来将多个样式合并,Tailwind 的模组化就是靠此功能实现
@apply
可与一般 CSS 一起使用@apply
後方样式编译时不照顺序排列,如要排列可使用多个 @apply
@apply
组好的 class
但 !important
不会继承!important
可直接写在 @apply
最後方/* Input */
.btn {
background-color: #3b82f6 !important;
@apply py-2 px-4;
@apply font-bold rounded;
}
.btn-blue {
@apply btn;
/* @apply btn !important; 可将所有 class 加上 !important */
}
/* Output */
.btn {
background-color: #3b82f6 !important;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
padding-left: 1rem;
padding-right: 1rem;
border-radius: 0.25rem;
font-weight: 700;
}
.btn-blue {
background-color: #3b82f6;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
padding-left: 1rem;
padding-right: 1rem;
border-radius: 0.25rem;
font-weight: 700;
}
此功能会依照对应的图层将 CSS 插入该处
base
、components
与 utilities
class
会直接放在原处@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
h1 {
@apply text-2xl;
}
}
@layer components {
.btn-blue {
@apply bg-blue-500 hover:bg-blue-700 text-white py-2 px-4 rounded;
}
}
@layer utilities {
@variants hover, focus {
.filter-none {
filter: none;
}
.filter-grayscale {
filter: grayscale(100%);
}
}
}
可将 class
加上各种的效果,例如 focus、hover 等等
@variants
後方的顺序影响 CSS 排列顺序tailwind.config.js
中可使用的 variant
这边亦可使用/* Input */
@variants focus, hover {
.ares {
color: #3b82f6;
}
}
/* Output */
.ares {
color: #3b82f6;
}
.focus\:ares:focus {
color: #3b82f6;
}
.hover\:ares:hover {
color: #3b82f6;
}
能产出响应式的 class
@variants responsive { ... }
同样也能做到该功能/* Input */
@responsive {
.ares {
color: #3b82f6;
}
}
/* Output */
@media (min-width: 640px) {
.sm\:ares {
color: #3b82f6;
}
}
@media (min-width: 768px) {
.md\:ares {
color: #3b82f6;
}
}
@media (min-width: 1024px) {
.lg\:ares {
color: #3b82f6;
}
}
@media (min-width: 1280px) {
.xl\:ares {
color: #3b82f6;
}
}
可将响应式的断点以 tailwind.config.js
内定义的字串替代
/* Input */
@screen lg {
.ares {
color: #3b82f6;
}
}
/* Output */
@media (min-width: 1024px) {
.ares {
color: #3b82f6;
}
}
提供我们去查找 tailwind.config.js
内的参数
theme()
内需传入字串,且同 JavaScript 使用 .
或 []
查找/* Input */
.ares {
color: theme('colors.blue.500');
}
/* Output */
.ares {
color: #3b82f6;
}
之前在写 Bootstrap 的时候一值很喜欢用 utility class,实际读过 Tailwind 之後发现这才是把 utility 发挥到极致阿!虽然没有许多现成的 Component 可以用,不过这对於什麽都想自干的人应该再适合不过了吧,接着下篇会来介绍它设定的部分~那就这样啦~
<<: 国家标准技术研究院(NIST)最低安全要求的最佳来源-标准
>>: iOS APP 开发 OC 第八天,@synthesize
摘要 作业流程 安装环境/套件 上传打包後模型 介绍Vim编辑器 惨痛经历 内容 作业流程(今日进度...
网页的外观和操作本质上还是和原生的有差异但可以透过配置来让体验更接近。 全萤幕模式 视觉设计 事件操...
在日常生活中,大家应该满常看到有些系统的搜寻输入框是可以在一边打字的同时,一边将搜寻结果呈现在一个...
大家好,我是毛毛。ヾ(´∀ ˋ)ノ 废话不多说开始今天的解题Day~ 67. Add Binary ...
大家好,我想介绍一下自己为什麽会认识spring boot,因为写後端API的时候会用到的框架 然後...