What is the correct way to import CSS styles globally in Vue?

My scss file contains a few classes. Each time I save the file, corresponding css files are generated. My initial thought was that I only need to import the resulting css in my main.js file to utilize it throughout my entire application. For example, let's say I have the following css class in my index.scss:

.button-label {
  color: white;
  font-family: "Comic Sans MS", cursive, sans-serif;
  font-weight: bold;
}

I imported its file in main.js like this:

import "@/assets/styles/index.css"; 

...

const vm = new Vue({
  store,
  router,
  Snotify,
  vuetify,
  render: h => h(App)
}).$mount("#app");

export default vm;

I would expect to now be able to use the button-label class in every component within my application. Unfortunately, it does not seem to work. It appears as though the specified classes are being ignored. Do I need to additionally import the mentioned file in my components? What am I doing incorrectly? How can I properly globally import stylesheets? Thank you for any assistance. Below is my index.html file:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@mdi/font@latest/css/materialdesignicons.min.css">
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

Answer №1

Although I hadn't previously used SCSS files in my Vue components, I successfully added a reference to my global stylesheet in the App.vue (main template file) for my single file component (Vue SFC). Here's how I did it:

<style src="../public/styles/style.css"></style>

In my directory structure, App.vue is located in the "src" folder while index.html and stylesheets are in the "public" folder. It seems that this difference in folders didn't have any impact on the success of the setup.

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

Having trouble running the command "npm start"?

Today I started working with electron and followed a tutorial to create a basic browser window application. However, when I ran the command 'npm start' in the terminal, I encountered multiple errors. Below is the console error message along with ...

Limiting the Size of Highcharts Maps with maxWidth or maxHeight

My attempt to adjust the maxWidth of my world map using highcharts isn't altering its size. I have experimented with the following: responsive: { rules: [{ condition: { maxWidth: 500 }, ... As recomme ...

Order your custom tasks on Amazon Mechanical Turk directly through the web interface

Can I prompt the submission of a form on Mechanical Turk using a custom button instead of their designated 'submit' button? (I need to submit an empty form to speed up worker processing time) I attempted this: $('#mturk_form').submit() ...

I need to retrieve information (such as name and email) from an API. Although my program compiles successfully, I am encountering errors in the console while trying to access the data. The API URL is https://randomuser

import React, {useState, useEffect} from "react"; import './App.css'; function App() { const [ data, setData ] = useState([]); useEffect( ()=> { loadData(); //getData(); }, []); const loadData = async () => { ...

Guide to customizing the Materialize Datepicker to change styling when clicking on a specific day

I am currently utilizing the Materialize Datepicker to toggle holidays in a list. In my code, when I click on a specific day, the event is either added or removed from the list immediately. However, the style within the datepicker only updates when I selec ...

Trouble with canvas setLineDash and lineDashOffset persisting in iOS/Safari?

Check out the fiddle linked here: http://jsfiddle.net/mYdm9/4/ When I execute the following code on my PC: ctx.lineWidth=20; ctx.setLineDash([20,30]); ctx.lineDashOffset=10; ctx.beginPath(); ctx.moveTo(150,150); ctx.lineTo(240,240); ctx.lineTo(180,40); ...

What is the correct way to encapsulate a socket.io socket for optimal performance?

In my node.js server application, this code is currently executing: io.sockets.on('connection', function (socket) { var c = new Client(socket, Tools.GenerateID()); waitingClients.push(c); allClients.push(c); if (waitingClients ...

Javascript is throwing a reference error because it can't find a definition for StringBuilder

I have a JavaScript function that utilizes a string builder function. It works smoothly in most major browsers like IE8+, Chrome, and Firefox but occasionally I encounter an error stating "stringbuilder is not defined". This issue seems to only affect cert ...

concealing information through the use of CSS and JavaScript after it has been shown

After successfully implementing a menu display upon clicking an image, my goal now is to hide the menu once a different image (image2) is clicked. If anyone could assist and explain their approach clearly, I would greatly appreciate it! Below is the modifi ...

Develop dynamic and stylized CSS using PHP within the Laravel framework

I am experiencing an issue with creating CSS files in Laravel within my controller. When I try to return the result, it shows up as a normal text file instead of a CSS file. In my routes.php file, I have: Route::get('/css/{css}.css', 'File ...

Handling HTTP request callbacks in Node JS using Node JS technology

Hey there! I'm currently working on making an HTTP request using the callback method, but I'm encountering an issue where I'm receiving a lot of information, but not the specific data I need: Request { domain: null, _events: { e ...

Encountering an issue with running `npm start` after using `npx create-react

I'm encountering an issue with running the create-react-app command. Despite following the instructions twice and attempting various solutions like adding "resolutions": { "ajv": "6.8.1" } to the package.json file, then ...

Pressing the submit button will trigger the execution of a .php script, which will then generate a popup on the screen and refresh a specific part of

I have a select form and submit button on my page, which are dynamically generated based on entries in the database. Here is the HTML output: <div id="structures"> <h1>Build</h1> <form name="buildForm" id="buildForm" method="POST" ons ...

Tips for extracting the src attribute from a dynamically generated iframe

My dilemma stems from a function that generates an iframe with content on my website, which unfortunately I cannot control as it is loaded remotely. The function initially creates: <span id="myspan"></span> Once the JavaScript function fu ...

I must highlight a specific link based on the user's action

Is there a way to dynamically color links based on user interaction? For instance, let's say I want to specify the color for unvisited links: a.newlink:link { color: red; } <a href="/privacy" target="_blank" class="newlink">New l ...

choose a selection hopscotch

Is there a way to prevent the input field from jumping out of alignment when an option is selected in these q-select inputs? I want to fix this issue so that the field remains in line with the horizontal alignment. https://codepen.io/kiggs1881/pen/RwENYEX ...

Having trouble with mapping a localStorage variable containing an Array of Objects, which results in the item disappearing

In my localStorage, I have an array called product_categories that contains various objects, each consisting of a string and a nested array of objects representing products belonging to each category. Despite seeking help from Appery's support team o ...

Is there a way to utilize the build number in a Vue application using Vue-CLI, and if so, how?

I am working on deploying a Vue app for production using Vue CLI. To ensure that testers are using the correct build, I am looking to implement an incrementing build number within the application. This build number would serve two main purposes - first, t ...

Tips for organizing an object according to specific attributes

Within my table, I have implemented a feature that allows the display of only selected columns at a time. To achieve this, I store the chosen columns (table headings) in an array called selectedTableHeaders. The next step is to filter out a new array bas ...

What is the recommended method for choosing text colors when creating GUI for app development?

Currently preparing PSDs for the development of both an iOS and Android app. When it comes to coloring text, should I: 1) Utilize #000000 for black text, adjusting opacity to achieve various shades of gray for primary and secondary text? 2) Or opt for a ...