Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue3 Tailwind CSS

Vue3 Tailwind CSS

Using Tailwind CSS in a Vue project is a popular choice because it provides a utility-first approach to writing CSS, letting you apply styles directly in class names, making styling more concise and intuitive.

Tailwind CSS tutorial:https://www.runoob.com/tailwindcss/tailwindcss-tutorial.html

Tailwind CSS official site:https://tailwindcss.com/

GitHub address:https://github.com/tailwindlabs/tailwindcss

Tailwind CSS is a powerful CSS framework that makes styling more concise and modular through a utility-first approach. Unlike traditional class-based CSS frameworks, Tailwind provides a set of low-level utility classes that can be used directly on HTML elements to build custom designs quickly and flexibly.

The following is a complete getting-started example using Vue 3 and Tailwind CSS, showing how to integrate Tailwind CSS with Vue 3 and use its features to build responsive interfaces.

Install Vue 3 and Tailwind CSS

First, make sure you have Vue 3 and Tailwind CSS installed.

To install Vue 3, run npm create vue@latest in the terminal to create a new Vue 3 project:



npm create vue@latest

In the output window, enter the project name and press Enter all the way through:

Running the above command requires setting some initialization options; use the arrow keys to choose.

Enter the initialized directory vue-tailwind-app and install dependencies:



cd vue-tailwind-app

npm install

Install Tailwind CSS and related dependencies:



npm install -D tailwindcss@latest postcss@latest autoprefixer@latest

npx tailwindcss init -p

After creation, the directory structure is as follows:

Configure Tailwind CSS by opening the tailwind.config.js file and modifying it to:

Example

/** @type {import('tailwindcss').Config} */

export

default

{

  content

:

[

   

"./index.html"

,

   

"./src/**/*.{vue,js,ts,jsx,tsx}"

,

 

]

,

  theme

:

{

    extend

:

{

}

,

 

}

,

  plugins

:

[

]

,

}

Add Tailwind directives: create a styles.css file in the src/assets directory and add the following:



/* src/assets/styles.css */

@tailwind base;

@tailwind components;

@tailwind utilities;

Then import this file in src/assets/main.js:



import './assets/styles.css


Create Vue Components and Styles

Now you can start writing your Vue 3 components and use Tailwind CSS to style the page.

App.vue

App.vue is the main component of the application; it contains the basic usage of Vue 3 and Tailwind CSS.

Example

Additionally, we can set our own port in the vite.config.js file, for example setting it to 3000 as follows:

Example

import

{

fileURLToPath

,

URL

}

from

'node:url'

import

{

defineConfig

}

from

'vite'

import

vue from

'@vitejs/plugin-vue'

import

vueDevTools from

'vite-plugin-vue-devtools'

// https://vite.dev/config/

export

default

defineConfig

(

{

  plugins

:

[

    vue

(

)

,

    vueDevTools

(

)

,

 

]

,

  resolve

:

{

    alias

:

{

     

'@'

:

fileURLToPath

(

new

URL

(

'./src'

,

import

.

meta

.

url

)

)

   

}

,

 

}

,

  server

:

{

    port

:

3000

,

// Your own specified port number

 

}

,

}

)

After setup is complete, run the npm run dev command; the output is as follows:

# npm run dev                                                         



> vue-tailwind-app@0.0.0 dev

> vite





  VITE v6.0.3  ready in 333 ms



  ➜  Local:   http://localhost:3000/

  ➜  Network: use --host to expose

  ➜  Vue DevTools: Open http://localhost:3000/__devtools__/ as a separate window

  ➜  Vue DevTools: Press Option(⌥)+Shift(⇧)+D in App to toggle the Vue DevTools



  ➜  press h + enter to show help

Visit http://localhost:3000/; the display is as follows:

Next we try creating a list by modifying the App.vue file, with the code as follows:

Example

Code analysis:

Visit http://localhost:3000/; the display is as follows: