Comparison between global and local styling in VueJS

As I develop a project using .vue files, I find it beneficial to have the ability to write CSS (SASS), JS, and HTML all within the same file.

For my global components written in SASS, I have created an assets/styles/app.scss file to house my grid, variables, and mixins.

In addition to the global styles, I also want the flexibility to write local SASS rules specific to each component or page. This seems like a logical approach for any project...

The structure of my local styles looks like this:

<template>
</template>

<script>
</script>

<style lang="scss">
  @import "assets/styles/app";

  .my-style {
    color: $my-variable;
  }
</style>

While this setup works, I've realized that as my VueJS project expands with more components, the global styling is loaded redundantly on each one. For instance, the same rule may be duplicated 5x or even 10x according to the Chrome developer tools. Even though it's a small project, having all styles repeatedly loaded by the browser whenever a new component is added to the page is not ideal.

So, how can I prevent multiple loading of global styles while still utilizing them in every component?

This approach of mixing local and global styling is new to me as I previously preferred separating the styling entirely. However, having everything contained locally simplifies the coding process significantly.

What adjustments should I make to optimize the handling of global styles efficiently within VueJS/NuxtJS projects?

https://i.sstatic.net/vzpIw.png

Note: I'm currently working with NuxtJS, but I believe this issue pertains more broadly to VueJS implementation.

Answer №1

Whenever you include an @import statement in your components, it results in another instance being added to the main CSS file generated by Webpack.

If your Webpack SCSS loader is set up correctly (which seems to be the case since it successfully compiles), you can import the SCSS file once in your app.vue and the compiler will automatically locate it when compiling all other CSS files.

For instance, if you need to incorporate global fonts and mixins:

<style lang="scss">
  @import url('https://fonts.googleapis.com/css?family=Lato:300,400,400i,700,900&subset=latin-ext');
  @import "@/scss/mixins.scss";
</style>

Subsequently, define the CSS styles for each component within the respective component's <style> section. Remember to include the attribute lang="scss" for proper compilation.

You may want to consider exploring scss-resource-loader for Webpack. This feature might be available in the latest CLI versions, although its compatibility with Nuxt is uncertain.

Answer №2

within the file App.vue

<style lang="scss">

@import "assets/styles/common.scss";

</style>

Alternatively,

you can import the compiled sass to a css file in main.js

import './assets/styles/common.css';

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

One method successfully updates the array within the data function, while a separate method continues to find the array empty

I am facing an issue in my app where I need to update an array. The process involves fetching data from the database and adding it to the array, which works fine. However, when I try to use this updated array in another method, it seems that the array is n ...

Having trouble with getting the modal transition to work when closing it

Upon opening the modal, it transitions smoothly. However, when attempting to hide the modal, it does not transition out as expected - instead, it simply disappears. I previously included the within the modal (which worked in Vue2), but it appears to be c ...

Unable to locate element within child component using VueJS test-utils

I'm struggling to use the findComponent method along with the find method in order to locate an element within a child component and set its value. However, every time I attempt to run the test, I encounter the error message: Cannot call setValue on a ...

Using a percentage width on a <div> element with the "overflow-x: scroll" property does not seem to be functioning properly, even when encapsulated within

My code includes a div within another div, taking up 50% of the page's width: <div id="mainContainer" class="mainContainer"> <div id="scroller" class="scrolling"> <!-- items here should make the div really long --> & ...

Elevate a specific element above others and stagger the positioning of certain elements by pushing them downwards

Take a look at this image to see the issue visually. I'm facing a problem with a div that contains a list displayed below a text field. It's essentially a simulated Combo-box that uses a text element and JavaScript. The issue is that when someon ...

Exploring the Power of Jest and Vue Test Utils for Unit Testing in VueJS

Having recently started Unit Testing with vue, I am currently working on unit testing a navigation vue component. My initial goal was to test a method that simply sets a boolean value to false upon clicking. Utilizing vuetify, I attempted to replicate a bu ...

What is the best way to display a page within a div without having it covered by the header and footer elements?

Currently working on customizing a WordPress website. Within my main document, I have the code set up for the header and footer, with a div in between for displaying content. My goal is to have the ability to show content from navigation links clicked wi ...

Creating a dynamic data table in Vuetify using Vue.js by making an API call

Could anyone provide me with an example of how to create a dynamic data table using Vuetify in Vue.js? I have checked the official documentation but couldn't find any examples. ...

"Scrolling with Bootstrap Scroll Spy is causing incorrect sections to be

Can anyone help me with my issue regarding Bootstrap scrollspy settings? It seems to be highlighting the wrong div id, always the last one. Here is the code snippet: Content: <body style="heigt: 100%" data-spy="scroll" data-target="#side-menu"> ...

Unable to eliminate the margin on the box

I am struggling to align 2 divs side by side due to a margin that one of the divs has, and I cannot remove it. .middleHolder{ padding: 0px !important; } body{ font-family: Raleway, sans-serif; } .numberedTitle{ font-family: Roboto, sans-serif; font-si ...

Centering div elements in ASP.NET using C# and Javascript

I am looking to create a functionality on my website where, upon clicking a button, a DIV appears in the center of the browser, overlaying all other content. I have already created the DIV and managed its visibility toggle, but I'm struggling with the ...

Check if the browser is not Google Chrome before running the code in JavaScript

Related Search: Finding a secure way to detect Google Chrome using Javascript? Using Javascript to identify Google Chrome and apply CSS changes Is there a JavaScript statement that can be used to execute code only when the user is not using Google ...

variances in CSS performance between local and remote hosting

My team and I are all using Internet Explorer 8 on Windows 7 Professional, with Visual Studio 2010 Premium. However, we have noticed that when running our application in localhost mode versus remote mode (on the server), there are differences in the CSS re ...

Comparing CSS Variables to CSS Modules' @value functionality

Could you clarify the difference between the css function var(--red) and @value red from css modules for me? CSS Modules Example: @value red from "./color.m.css"; .background { background-color: red; } CSS Var() Example: @import "./color.m.css"; ...

Exploring the world of CSS font families in Sublime Text 3 and linking them to the web

Is there a way to connect a font from the internet (such as those on Google Fonts: ) to my Sublime Text CSS file? I have tried linking it to an HTML file that is already linked to my CSS code, but it hasn't been successful. ...

What is the best way to connect a CSS file to a jade document?

Having some trouble linking normalize.css while using socket and express. html head title= "Real time web chat" link(href='/css/normalize.css') script(src='/chat.js') ...

When the paragraph text is vertically centered within a flexbox, it is targeting the <p> tag. But why does it only center properly when the <p> tag is omitted?

Upon examining the Vuetify code snippet below: <v-layout align-center justify-center row fill-height> <v-flex xs1> <v-icon color="primary">clear</v-icon> </v-flex> <v-flex xs8> ...

Twice the clicking actions triggered by the event

Whenever I try to trigger a function by clicking on the label text, the click event seems to be firing twice. HTML <label class="label_one"> Label One <input type="checkbox"/> </label> However, if I modify the HTML code as f ...

Turned off jquery on a specific div in order to resolve compatibility problems with Vue.js

Currently, I am working on a Drupal project which includes JQuery in all pages. We have recently introduced Vue.js for creating dynamic components directly in the HTML pages (not for building a single-page application). However, we are facing an issue whe ...

Begin one lesson, end all others

Looking for help with my expandable list menu that I created using jQuery. Here is the code snippet: $("#menu ul li ul").hide(); $("#menu ul li").click(function() { $(this).find("ul").slideToggle(); }); You can view the fully functional menu on jsFi ...