I'm a little lost on where exactly I should be placing my CSS within a Vue Single Page Application

Throughout my experience, I have always utilized a single CSS file for storing all my CSS rules when developing multi-page applications.

However, now that I have started using Vue.js and components, I am faced with the dilemma of where to place my CSS.

I could include the CSS within the <style></style> tags of a specific component, but this approach seems suitable only if the CSS is intended solely for that particular component. This raises the question of where to write CSS that should be applied globally across all components.

Consider having CSS rules like the following snippet that need to be applied universally:

*, *:after, *:before {
    margin: 0;
    padding: 0;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    text-decoration: none;
}

Where should such CSS be written?

Answer №1

Here are two suggestions until a more suitable solution is found:

1. Include Code in App.vue File

If the code is minimal, consider placing it in your App.vue file

As mentioned:

"I could write the CSS in the tags of a component but to me this only makes sense if the CSS is only used for this specific component."

CSS within the <style></style> tags in any .vue files in your project will be applied globally.

The <style></style> tag affects the entire project unless you use scoped.

<style scoped>
    body {
        background: green; /* This won't affect your main <body> tag unless you actually have a `<body>` inside the `<template>` of that file */
    }
</style>

2. Import Standalone CSS File (Updated and Simplified)

Suggested by @Fabjan in the comments.
Import the .css file in your main.js file:

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './assets/lib/css/reset.css' // my personal reset file
import './assets/lib/css/myMain.css' // global stylesheet

Ensure the import occurs before launching your app with new Vue({})


(3. My Previous Response:)

Create a styles.vue file and place all global styles inside <styles></styles> without scoped (this method worked for me)

In your main.js file, include the following:

import GlobalStyles from "./assets/lib/css/styles.vue"

Vue.use(GlobalStyles)

Make sure the styles.vue file contains at least one tag inside for proper importation.

<template>
    <div class="empty">

    </div>
</template>

<style>
    body {
        background: red;
        /* Additional global CSS code goes here */
}
</style>

This method may seem crude, but it was effective for me at the moment.

I hope these suggestions prove helpful, and I welcome any feedback or alternative solutions from others.

Answer №2

Check out my approach to setting up global SCSS in a Nuxt project.

  1. Make sure you have already installed node sass and sass-loader.
  2. In the nuxt.config.js file, include the path to your SCSS files from either the static or assets folder

    css: [ '@/assets/scss/main.scss' ]

If you prefer an alternative method, you can explore nuxt-sass-resources-loader

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript array images are not showing when using the img tag

For this module, I am working on creating a flipbook (magazine) using images stored in a JavaScript array. However, I am facing an issue where the images are not loading up properly. Instead of displaying the image, it shows as [object" htmlimageelement]=" ...

Abundance of DOM elements - the difference between hidden and display none

When I have numerous DOM elements on my page and assign them all a display: none property, the browser continues to perform quickly (smooth scrolling and snappy page response). But if I hide the elements using visibility: hidden instead, the browser slows ...

What is the best way to incorporate a transition effect into a flex item with a specified max-width, where the width adjustments are managed by a child div?

The example below demonstrates a situation where the child's width grows with a smooth transition, but the parent's width abruptly changes! What I aim for is to have both the child's width and the parent's width transition smoothly. I ...

Transform shapefiles to geojson format using OpenLayers 5

As I embark on a new project that involves combining Vue.js and OpenLayers, I am thoroughly enjoying the process. While I have successfully implemented various functionalities like drawing, reading, and modifying vectors, I find myself stuck when it comes ...

Struggling with css margins and div image constraints, seeking guidance and tips for resolution

Struggling with creating a dynamic gallery that works smoothly in jsfiddle but encounters multiple issues when integrated into WordPress. The main problem is the excessive stretching of margins between image titles and meta-data, leading to misalignment an ...

What is the purpose of using "recaptcha_get_html" function to echo?

Hello everyone. I've been struggling to incorporate Google's reCAPTCHA into my project and I keep encountering an error when trying to use the recaptcha_get_html function. It keeps showing up as undefined and despite searching high and low, I can ...

Utilizing the vue-tags-input plugin in combination with Laravel

I'm attempting to integrate vue-tags-input with Laravel 7. I have successfully displayed it on the page within a form and entered values, but I am struggling to figure out how to pre-populate the tags upon loading or retrieve the submitted values in a ...

Any ideas on how to fix the bug that pops up when I launch my website for the very first time?

After successfully getting my website up and running, I noticed a tiny glitch that appears the first time it's opened. You can view the site here: www.voip-account.com This is not built using a WordPress theme; it's purely made with HTML and CSS ...

Using words instead of symbols to represent logical operators

When coding in C++, I typically prefer using the 'word' operators: not instead of ! and instead of && or instead of || I find it easier to read, especially when negating statements with not. Is there a similar approach possible in ...

Place the text in the middle of a div element

I have a div that is typically sized to be 200px x 40px. Depending on the content, it may contain either one or two lines of text. When there are two lines, I adjust the line height to maintain balance within the div. However, when there is only one line o ...

designing a parallax footer that stays hidden under the surface?

I'm completely new to asking questions here, but I've gained valuable knowledge from browsing the site. Thank you! Just stumbled upon this amazing 'under the rug' style parallax effect on a footer and I'm wondering how it was crea ...

Detecting a click on the background div in Angular without capturing clicks on child divs

Over at ollynural.github.io, I've been working on creating a pop-up div in the portfolio page that provides additional information about the project you select. To close the pop-up, I have implemented an ng-click feature so that clicking on the main p ...

Guide on setting up sidebar navigation

I am looking to enhance my html page with a sidebar navigation menu. Here is the current code snippet: <html> <head> <title>Admin dashboard</title> </head> <header style="height:100px; background-colo ...

How can I add an object to an array of objects in Vue.js?

Hey there! I'm new to Vue and currently working on understanding the whole concept of Vue and how to use it. Right now, my focus is on learning lists. JS: Vue.config.productionTip = false; new Vue({ el: '#app', data: { items: [ ...

Auto-selecting the first item in a CSS menu when the switcher is tapped on mobile devices

Struggling with a responsive CSS menu on my website, . The switcher on mobile seems to automatically click the first item in the menu (Home) as I open it, redirecting me instantly. When setting the link to "#" everything is fine, but then I lose the home l ...

Experimenting with incorporating easing effects into jQuery tabs

Could someone please help me implement an easing/fading effect on tabs using jQuery? I am looking to add an easing effect to my current code and also incorporate two CSS classes to change the background color along with the text color. Thank you for any as ...

What is the best way to display swipebox thumbnails in a grid layout instead of a single column?

I'm a novice in the world of web design and attempting to organize a group of 70 thumbnails into a grid or columns within an existing layout for a friend. Currently, they are displayed in a single lengthy column. Is there a straightforward way to adju ...

Arranging three elements in a CSS grid for perfect alignment

I started with 3 elements and thought about making them adaptive by using css grid. <div class="elements-wrapper"> <div class="element-1"></div> <div class="element-2"></div> <div class="element-2"></div> ...

Bootstrap SideBar (Template): I desire for the sidebar to expand in the content area without the need for lengthy paragraphs below

After following an online tutorial to create a visually appealing sidebar, I made some adjustments to the code using Bootstrap 4 instead of the tutorial's Bootstrap 3. However, I encountered an issue where the navbar does not stretch to the right as e ...

Mastering the correct way to access array index positions in Vuejs

HelloWorld.vue <template> <div> <div v-for="(pizza, index) in pizzas" :key="pizza.name"> {{ pizza.name }} <List :content="matchingPizza" :pname="pizza.name" :qname="quantities[index] ? ...