一个内置了多个基础css样式类,可以减少和规范css代码的库 官网:Tailwind CSS - Rapidly build modern websites without ever leaving your HTML. 中文:Tailwind CSS - 无需离开您的HTML,即可快速建立现代网站。
使用css module,只负责自己的页面
无需思考css类名:这些类名在当前文件下看上去合适,放在整体未必合适
使用变量,样式更加规范,比如视觉规范只允许出现2/4/8/16px的边距,且能够快速实现自适应布局
官网安装流程:Installation: Using PostCSS - Tailwind CSS
推荐使用postcss的方式接入:可以对代码进行扫描,只添加使用到的css类,减少代码量
适用于eden 2.x,eden 2.x也是支持postcss.config的
@tailwind base; // 浏览器默认样式重置,和reset-css功能一致
@tailwind components; // 组件,默认是空的
@tailwind utilities; // 基础样式类
module.exports = {
content: ["./src/**/*.{html,js}"], // 需要扫描的文件目录
theme: { // 自定义样式配置 https://tailwindcss.com/docs/configuration
extend: {},
},
plugins: [], // 插件让您注册新的样式,让 Tailwind 使用 JavaScript 代替 CSS 注入用户的样式表。 https://tailwindcss.com/docs/plugins
}
module.exports = {
content: ['./src/**/*.{html,js}'],
theme: {
colors: {
'blue': '#1fb6ff',
'purple': '#7e5bef',
'pink': '#ff49db',
'orange': '#ff7849',
'green': '#13ce66',
'yellow': '#ffc82c',
'gray-dark': '#273444',
'gray': '#8492a6',
'gray-light': '#d3dce6',
},
fontFamily: {
sans: ['Graphik', 'sans-serif'],
serif: ['Merriweather', 'serif'],
},
extend: {
spacing: {
'8xl': '96rem',
'9xl': '128rem',
},
borderRadius: {
'4xl': '2rem',
}
}
},
}
接下来就能和官网演示的一样,愉快的写css了
由于 tailwind 包含的是基础样式类,现有的 utility,base,component 不足以满足所有的场景; 而使用 @layer 指令,Tailwind 将自动将这些样式移动到 @tailwind base, @tailwind utility,@tailwind component 的位置 比如我们要做一个按钮组件,提炼对应的样式:
@layer components {
.btn-blue {
@apply bg-blue-500 text-white font-bold py-2 px-4 rounded;
}
.btn-blue:hover {
@apply bg-blue-700;
}
}
先写个简单的例子:
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
h1 {
@apply text-2xl;
}
}
@layer components {
.btn {
@apply bg-gray-500 text-white font-bold;
@apply py-2 px-4 rounded;
font-family: Georgia, Cambria, "Times New Roman", Times, serif;
background-color: theme("colors.blue-500");
}
}
// hover:.filter-none
@layer utilities {
@variants hover, focus {
.filter-none {
filter: none;
}
}
// bg-opacity-none:md bg-opacity-none:lg
@responsive {
.bg-opacity-none {
@apply opacity-0;
}
}
}
// 正确
2. 一个元素上的类名建议小于四个,太多的话考虑自定义组件、类
3. Vscode添加Tailwind CSS IntelliSense插件,智能提示类名
4. Translate css to tailwind
我们在项目之初就应用 TailwindCss,会有比较丝滑的体验。而对于老项目迁移到 TailwindCss,这可能是一个比较难受的过程,这里可以介绍一个在线工具,可以根据你的 tailwind.config.js 文件,将 CSS 转化为 TailwindCss 类名。
.logo { margin-bottom: 1.6rem; background: url(‘logo.svg’) no-repeat; display: flex; justify-content: center; }
.footer { width: 100%; display: flex; justify-content: space-between; align-items: center; flex-direction: row-reverse; padding: 2.4rem 3rem; border-top: 1px solid #fff5f5; }
转化后:
/* ℹ️ Base selector: .logo // ✨ TailwindCSS: “bg-no-repeat flex justify-center mb-6” // ⚠️ Some properties could not be matched with Tailwind classes. Use @apply to extend a CSS rule: */.logo { @apply bg-no-repeat flex justify-center mb-6; background-image: url(logo.svg); }
/* ℹ️ Base selector: .footer // ✨ TailwindCSS: “border-red-100 border-solid border-t flex flex-row-reverse items-center justify-between py-10 px-12 w-full” */
5. 一些常用的组件
@tailwind base; @tailwind components; @tailwind utilities;
@layer components { .row-center-center { @apply flex items-center justify-center } .row-center-between { @apply flex items-center justify-between } .row-end-center { @apply flex items-end justify-center } // … … }
// … … ```