Using webpack to scope all SCSS styles under the selector of a Vue app

Currently, I am developing a Vue app that is embedded within a page on a more traditional server-rendered website. The app is contained within the following container:

<div id="account-summary-container"></div>

During local development, everything runs smoothly. However, once the app is integrated into the website, there are significant style conflicts because both my app and the site's styles are global. This results in my app affecting the styling of the entire website.

I am seeking a solution to scope all styles within my app to be localized to the selector where my app is rendered.

My app utilizes Bootstrap 4 styles, which are loaded using css-loader.

In my webpack.config.js file, I have entrypoints defined as:

entry: {
  app: ["./src/scss/styles.scss", "./src/app.js"]
},

The contents of styles.scss include:

@import '~bootstrap/scss/bootstrap';
@import '../css/feather.min.css';
@import '../css/icomoon-spinners.css';
@import url('https://fonts.googleapis.com/css?family=Maven+Pro:400,500,700,900');
@import 'helpers/variables';
@import 'helpers/mixins';
@import 'helpers/placeholders';
...

I have considered using css-modules as a potential solution. However, I am struggling to instruct css-loader to localize all styles specifically to #account-summary-container. My attempt in styles.scss resulted in an error message:

Error: composition is only allowed when selector is single :local class name not in ":local(#account-summary-container)", "#account-summary-container" is weird

This setback has led me to question if my approach is incorrect. Ideally, I would like to avoid extensive rewriting of styles.

Answer №1

To specifically target components within your Vue instance, you must utilize scoped styles. It can be a bit cumbersome because you are unable to apply CSS scope to the main App.vue file and have it inherited by all children - instead, you need to implement scoped styles on each component individually, leading to potentially more requests than desired.

For example (I have tested this and it should function properly)..

<template>
    <!--
      VUE/COMPONENT TEMPLATE
    -->
</template>

<script>
  export default {
    /* ***********************
         VUE/COMPONENT CODE
       *********************** */
  };
</script>

<!-- 
*********************************************************
***** SCOPE CSS PER COMPONENT ****************************
***** IMPORT VIA 'src="./path/to/cssfile.css"' ***********
**********************************************************
--> 
<style scoped src="../css/style.css">

</style>

Answer №2

Discovered the solution here. Super straightforward. Should've had a lightbulb moment.

.unicorn {
    @import 'sub';
    font-size: 100px;
}

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

Center the image and align the floated texts on each side horizontally

I've recently started learning about HTML and CSS, but I'm having trouble grasping one concept. My goal is to align two floating <p> elements on either side of a centered <img>. Here's an example of what I'm trying to achiev ...

What is the reason behind accessing the state of a module in Vuex as 'store.state.module' instead of 'store.module.state'?

Can you explain why the state of moduleA is accessed using store.state.a rather than store.a.state in the code below? To me, it seems more logical for the state of moduleA to be accessed with store.a.state, considering that the store contains moduleA and ...

Leveraging Leaflet or any JavaScript library alongside Typescript and webpack for enhanced functionality

Important: Despite extensive searching, I have been unable to find a resolution to my issue. My current endeavor involves developing a map library through the extension of leaflet. However, it appears that I am encountering difficulties with utilizing js ...

Using selenium in conjunction with python to click a unique button

Having trouble clicking the connect button on the "people you may know" page of LinkedIn () The HTML code for these buttons is: <a class="vcard-button bt-connect bt-primary" href="#"><span>&nbsp;</span>Connect</a> I attempted ...

Leverage CSS variables within the `url('data:image/svg+xml')` function

Here is a CSS snippet I use to create a dashed border around a div: div { width: 100px; height: 100px; border-radius: 16px; background-image: url('data:image/svg+xml,%3csvg stroke="black" width="100%25" height="100%25" xmlns="http://www.w ...

Display the selected value in the `vuetify` select component before the user

I have integrated Vuetify into my project and encountered an issue with the select component. Here is how I have set it up: <v-select v-model="gender.value" :items="gender.items" label="Gender" :solo=" ...

When viewing the material-ui Chip component at normal zoom, a border outlines the element, but this border disappears when zoomed in or out, regardless of

Edit I have recently discovered a solution to the unusual problem I was facing with the material-ui Chip Component. By adding the line -webkit-appearance: none; to the root div for the Chip, the issue seems to resolve itself. However, this line is being a ...

Guide on how to turn off a checkbox "toggle switch" using JavaScript

Being relatively new to CSS and HTML, I recently experimented with creating a checkbox toggle switch using this resource. I now have a specific requirement to disable this toggle switch upon clicking a different button labeled 'Reset', making it ...

Assign a value to an object within a Vue data property

Hey there, I am currently learning vuejs and facing an issue with setting an object value to a vue data property. data: () => ({ newTodo: "", todoObj: { title: newTodo, undo: false }, toDoList: [ { title: "Study", undo: false }, ...

Using Vue and PHP to parse a JSON string and display it as a table

I am currently facing a challenge in parsing a JSON string from my Laravel application to my Vue view. The format of the JSON string could be as follows: { "1":[ { "row":"Some text here on first column." }, { "row":"And more text. Second row ...

Displaying default value in select dropdown using v-model

I am working on a Vue selectbox with an initial v-model value, and I want to display its value on the selectbox. However, I am unsure of how to accomplish this. Any assistance would be greatly appreciated. new Vue({ el: "#demo", data() { return ...

How to toggle a specific element in Bootstrap 4 with a single click, without affecting other elements

I've been struggling with a frustrating issue: I have 3 elements next to each other at the bottom of the fixed page. I tried using Bootstrap4 toggles to display only text when a user clicks on an image, but it ends up toggling all the images instead o ...

The CSS rule for the Checkbox Input column is consistently implemented on the element located in the top row

Does anyone have experience working with a table containing checkboxes like the one demonstrated here? I am encountering an issue where toggling the checked property of any input on any row always affects the element in the first row. Upon further investi ...

Creating dynamic movement on webpages using CSS3 Animations

Exploring the world of CSS animations is such a blast! I've created this fiddle to play around with the concept of wheels on a bus. Check out my JS Fiddle here One strange thing I noticed is that sometimes there's a white space between the two ...

generate a flexible div that adjusts its height based on the size of the browser window

Is it possible to create a div layout structured like this: <div_wrapper> <div_header> </div_header> <div_content> </div_content> </div_wrapper> Upon page load, the div_header o ...

CSS transition not taking effect

Check out my page HERE (it's for a school project). I have a cool @font-face dingbat mail character at the top next to the word "contact" in the navigation. I want it to turn red when you hover over it, but I can't seem to make it work. Right now ...

What is the best way to send custom props to a Vue component through a function?

How can I successfully pass the boolean value isReadonly from the first component to the second one? It doesn't seem to be working as expected. Edit made following cafertayyar's response: The isReadonly method has been relocated from methods to ...

What is the reason for the absence of content in Vue.js 3?

Hey there, I'm currently diving into VueJS 3 and I've come across a beginner problem. Can someone please help me figure out what's wrong? Check out the image here Here's a snippet from my package.json: "dependencies": { ...

What is the solution to resolving the error message "Uncaught ReferenceError: Pusher is not defined" in Vue.js 2?

Whenever I try to launch my application, the console shows the following error: Uncaught ReferenceError: Pusher is not defined and Uncaught ReferenceError: App is not defined Despite running the following commands in my terminal: npm install and ...

When clicked, the onClick feature will reduce the number each time instead of initiating the timer

Currently, I am working on a meditation application using React. As a starting point, I implemented a 25-minute countdown feature. The challenge I am facing is that the timer starts counting down each time the button is clicked, rather than triggering it ...