What is Tailwind CSS
flex
、 pt-4
、 text-center
以及 rotate-90
等工具类,可以组合使用并直接在 HTML 代码上实现任何 UI 设计。Why choose Tailwind CSS
<div class="chat-notification">
<div class="chat-notification-logo-wrapper">
<img class="chat-notification-logo" src="/img/logo.svg" alt="ChitChat Logo">
div>
<div class="chat-notification-content">
<h4 class="chat-notification-title">ChitChath4>
<p class="chat-notification-message">You have a new message!p>
div>
div>
<style>
.chat-notification {
display: flex;
max-width: 24rem;
margin: 0 auto;
padding: 1.5rem;
border-radius: 0.5rem;
background-color: #fff;
box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}
.chat-notification-logo-wrapper {
flex-shrink: 0;
}
.chat-notification-logo {
height: 3rem;
width: 3rem;
}
.chat-notification-content {
margin-left: 1.5rem;
padding-top: 0.25rem;
}
.chat-notification-title {
color: #1a202c;
font-size: 1.25rem;
line-height: 1.25;
}
.chat-notification-message {
color: #718096;
font-size: 1rem;
line-height: 1.5;
}
style>
<div class="p-6 max-w-sm mx-auto bg-white rounded-xl shadow-lg flex items-center space-x-4">
<div class="shrink-0">
<img class="h-12 w-12" src="/img/logo.svg" alt="ChitChat Logo">
div>
<div>
<div class="text-xl font-medium text-black">ChitChatdiv>
<p class="text-slate-500">You have a new message!p>
div>
div>
Tailwind CSS 和 Bootstrap 都是前端常用的 UI 框架,但它们的设计思路和使用方式存在一些区别:
tailwind.config.js
中配置:/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: 'class',
// more options...
}
<div class="w-32 h-32 bg-blue-500 dark:bg-green-500">div>
基本规则
类别-属性-值
的结构命名,例如 .bg-blue-500
表示背景色为蓝色(blue),并且其颜色值为 500,类别为 bg
(背景)。w-20
表示宽度为 20 像素;颜色采用单词或十六进制颜色值表示,例如 bg-red-500
表示红色背景(颜色值为 #EF4444);布尔值则采用 true
或 false
表示,例如 hidden
表示隐藏元素。.w-10
表示元素的宽度;.text-xl
表示字体大小.mx-auto
表示水平居中;.text-center
表示文本水平居中;.text-red-500
表示字体颜色{屏幕尺寸}:{属性}-{属性值}
,例如 text-center sm:text-left
表示在小屏幕尺寸下文本左对齐,在大屏幕尺寸下文本居中。.underline
表示下划线;.no-underline
表示去掉下划线;.hover:underline
表示鼠标悬停时出现下划线;.hover:text-red-500
表示鼠标悬停时文本变为红色;.rounded
表示圆角;.shadow
表示阴影;.bg-gradient-to-r
表示背景渐变。通过以上的基本使用方法和语法规则,可以让您更加熟悉和了解 Tailwind CSS 的使用,从而更加高效地使用这个强大的 CSS 框架。
语法技巧
自定义颜色:在 tailwind.config.js 文件中,可以使用 theme 属性自定义颜色,这个属性包含一个颜色对象,您可以并据此为您的项目定义新的颜色。
响应式设计:Tailwind 提供了一系列响应式类,让您可以根据不同的屏幕尺寸为不同的设备定制 UI,例如:sm,md,lg等。
定制样式:Tailwind CSS 提供了许多原始的类,您可以轻松地将它们组合在一起创造出自定义样式的组合。
pt-10
(上边距为10) 和 pb-10
(底边距为10)两个实用类组合在一个元素上,即 class="pt-10 pb-10"
。bg-opacity-90
类,即给背景添加90%的不透明度,只需将 bg-opacity-90
类添加到 bg-blue-500
(蓝色背景)的后面,即 class="bg-blue-500 bg-opacity-90"
。font-bold-uppercase
类,然后使用 @apply
将 font-bold uppercase
两个类组合在一起:less复制代码.font-bold-uppercase {
@apply font-bold uppercase;
}
然后,可以应用该 font-bold-uppercase
类到一个元素中:class="font-bold-uppercase"
。
文本样式:Tailwind CSS 提供了一些实用的类,可以快速设置文本的样式,例如: text-center(居中对齐文本)、text-2xl(增加文本大小)。
构建网格系统:您可以利用 Tailwind CSS 来构建网格系统,这使得在项目中创建布局变得更加容易。利用网格系统类将组件和元素对池进行行和列的多样化排列,能让您快速定位到您需要的布局。
ini复制代码
Col 1
Col 2
Col 3
定位元素:Tailwind 提供了各种定位元素的类,包括 z-index、position、top、right、bottom、left 等等,使您轻松定位元素的位置和层级。
缩放图像:利用 Tailwind CSS,您可以轻松地在UI中缩放图像,只需使用 w-和 h- 后缀来指定图像的宽高。
# 初始化package.json文件
npm init -y
# 安装tailwind
npm install -D tailwindcss
# 创建tailwind.config.js
npx tailwindcss init
// tailwind.config.js
module.exports = {
//路径注意调整
content: ["./**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<link rel="stylesheet" href="/dist/output.css">
head>
<body>
<h1 class="flex text-3xl font-bold underline text-blue-600">
Hello Tailwind CSS!
h1>
body>
html>
/* 引入 tailwind 的基础样式 */
@tailwind base;
/* 引入 tailwind 的组件样式 */
@tailwind components;
/* 引入 tailwind 的工具样式 */
@tailwind utilities;
npx tailwindcss -i ./input.css -o ./dist/output.css