Best way to have global css in Vuejs Best way to have global css in Vuejs vue.js vue.js

Best way to have global css in Vuejs


Import css in your index.html, but if you're using webpack you can just import your stylesheets in your main js config and all your components will get the css.

As comments below suggested if using webpack adding this to main.js works:

import './assets/css/main.css';


I found the best way is to create a new file in the assets folder, I created as global.css but you can name anything of your choice. Then, import this file global.css file in the main.js.

Note: Using this approach you can also create multiple files if you think the global.css is getting really large then simply import all those files in the main.js.

@\assets\global.css

/* move the buttons to the right */.buttons-align-right {  justify-content: flex-end;}

main.js


import Vue from 'vue'import App from './App.vue'import router from './routes'Vue.config.productionTip = false// Importing the global css fileimport "@/assets/global.css"new Vue({  router,  render: h => h(App)}).$mount('#app')


In App.vue you can add a style property to declare you CSS file:

<style>  @import './assets/css/global.css';</style>