css原子化的框架Tailwindcss的使用教程(原始html和vue项目的安装与配置)

安装教程

中文官网教程

原始的HTML里面使用

  • 新建文件夹
  • npm init -y 初始化项目

安装相关依赖

js 复制代码
npm install -D tailwindcss postcss-cli autoprefixer

初始化两个文件

js 复制代码
npx tailwindcss init -p

根目录下新建src/style.css

css 复制代码
@tailwind base;
@tailwind components;
@tailwind utilities;

tailwind.config.js内增加如下配置

js 复制代码
/** @type {import('tailwindcss').Config} */
module.exports = {
  // purge内代表匹配所有的html文件,即会扫描所有的html文件进行自动生成对应的css
  purge: ['./src/**/*.html'],
  content: [],
  theme: {
    extend: {},
  },
  plugins: [],
}

package.json新增节点

js 复制代码
"scripts":{
    "watch":"postcss src/style.css -o dist/style.css --watch",
    "build":"postcss src/style.css -o dist/style.css"
}

html引入只需要引入src下面的css文件即可

至此,当我们运行npm run watch之后,html类名有变动则对应的dist/style.css文件内也会增加对应的类

当我们运行npm run build会打包生成dist/style.css

多个类的内容整合成一个类

js 复制代码
在html中
<div class='heading'></div>

在src/style.css内
新增如下:
.heading{
    @apply text-9xl text-center text-blue-600 sm:bg-black sm:text-white;
}

打包出来dist/style.css的结果为
.heading{
    font-size:8rem;
    line-height:1;
    text-align:center;
    --te-text-opacity:1;
    color:rgba(37,99,235,var(--tw-text-opacity))
}
// 后面还有关于heading的响应式,就不写了

vue内使用安装教程

这里使用vite配置

创建项目

js 复制代码
npm create vite@latest

安装依赖

js 复制代码
npm install  // 先全部安装
npm install -D tailwindcss postcss-cli autoprefixer  // 再安装需要的依赖

初始化两个文件

js 复制代码
npx tailwindcss init -p

设置tailwind.config.js文件的目录位置

js 复制代码
/** @type {import('tailwindcss').Config} */
export default {
  // 当以下被匹配到的文件内的类名变化时,会被匹配到,同时会在打包目录下新增类对应的样式
  purge:['./index.html','./src/**/*.{vue,js,ts,jsx,tsx}'],
  content: [],
  theme: {
    extend: {},
  },
  plugins: [],
}

在src下面新增index.css

css 复制代码
@tailwind base;
@tailwind components;
@tailwind utilities;

在main.js内引入index.css

js 复制代码
import { createApp } from 'vue'
import './index.css'
import App from './App.vue'

createApp(App).mount('#app')

编辑App.vue

js 复制代码
<script setup>

import { ref, reactive } from 'vue';
const count = ref(0);

</script>

<template>
  <div class="text-9xl text-center text-blue-600 sm:block">
    hello world
  </div>
</template>

<style scoped lang="scss">

</style>

运行项目

js 复制代码
npm run dev

效果图

具体的类名和样式需要大家去官网查询,这里我推荐的是中文网(因为能看懂)

相关推荐
你真的可爱呀2 小时前
uniapp+vue3项目中的常见报错情况以及解决方法
前端·vue.js·uni-app
哈哈~haha7 小时前
Step 14: Custom CSS and Theme Colors 自定义CSS类
前端·css·ui5
我命由我123457 小时前
VSCode - VSCode 修改文件树缩进
前端·ide·vscode·前端框架·编辑器·html·js
我命由我123459 小时前
VSCode - VSCode 颜色值快速转换
前端·ide·vscode·前端框架·编辑器·html·js
qq_12498707539 小时前
基于SpringBoot+vue的小黄蜂外卖平台(源码+论文+部署+安装)
java·开发语言·vue.js·spring boot·后端·mysql·毕业设计
BD_Marathon10 小时前
【JavaWeb】HTML_常见标签_表单项标签
html
好好好明天会更好10 小时前
uniapp项目中视频播放控制对象
前端·vue.js
萌狼蓝天11 小时前
[Vue2]项目中 vue-draggable-resizable 列宽拖动问题修复(首次拖动列宽突然变得很小)
前端·javascript·vue.js·前端框架·ecmascript
带带弟弟学爬虫__11 小时前
ks安卓—did注册
前端·javascript·vue.js·python·网络爬虫
徐小夕@趣谈前端11 小时前
LuckyFlow:用Vue3实现的一款AI可视化工作流编辑器
vue.js·人工智能·编辑器