Implementing Dynamic CSS Loading based on the Module Being Loaded using Webpack and VueJS

In my VueJS application, I am using Webpack 2 to manage the modules. As I collaborate with a web designer on styling, we have decided to organize the CSS files in a specific way. The designer prefers having separate CSS files for different sections of the app - one for the login page (login.css), another for after logging in (app.css), and so on. The challenge is that we don't want unnecessary styles to be loaded on certain pages. Additionally, the CSS classes may overlap but serve different purposes. For instance, the body tag might have different font sizes in login.css and app.css.

To include these CSS files, I import them into my main.js file:

import '../src/assets/css/login.css';
import '../src/assets/css/app.css';

This leads to the styles in app.css overriding those in login.css by default.

My question is how can I selectively load the required CSS files based on the page being accessed? For example, load login.css only for the login page and switch to app.css after logging in?

Currently, my webpack.config.js includes the following configuration:

  {
            test: /\.css$/,
            loader: 'style-loader!css-loader'
        },

Answer №1

In your current setup, you are including both of these styles directly in your javascript file. This means that every time you import main.js, these styles will also be loaded.

To improve this, you should consider being more targeted with your imports and only include the necessary styles on the specific pages where they are needed (especially if you have a single page application).

If you do not have a single page application, it is recommended to separate the CSS from your imports and store them in individual files. You can then manually link these files using the <link> tag. To achieve this, you can utilize the following Webpack plugin:

https://github.com/webpack-contrib/extract-text-webpack-plugin

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

With the simple click of a button, I aim to refresh and enhance the appearance of a polygon

My goal is to create a button that, when clicked, causes the gray stripe in the middle to transition to the right. I am not very familiar with HTML, CSS, and JavaScript so I could use some help figuring out how to achieve this effect. Here is a quick illus ...

Looking to implement an underline effect using CSS for the active tab that mimics the hover effect already applied

How can I ensure that the underline on active tabs (Domov) matches the hover effect on other tabs? The border-bottom currently creates a border that is lower than the hover effect, resulting in inconsistent underlines as seen on this page - (try moving ...

Substitute the division

Can I make the old div change its position and move to the side when I add a new div? And can all the old divs be hidden when adding four new divs? This is for my full news website and I want this applied to all four pages. First page: https://i.sstatic. ...

Issues with Bootstrap glyphicon not functioning correctly on mobile devices and Internet Explorer

Within my template, there is a navigation button positioned on the left side of the page that remains fixed as the user scrolls down. When clicked, this button triggers a menu to appear. Embedded within this button is a bootstrap glyphicon: <div class ...

Vue component's property does not exhibit reactivity

I seem to have encountered an issue with Vue.js, or perhaps I am missing something fundamental. In the simple example provided below, there appears to be a bug that I am able to replicate. The App.vue component includes a reactive variable called message, ...

What is the best way to prevent a jQuery hover event from adding a text-shadow to the CSS?

Currently, I am facing an issue with a jQuery event that triggers a text-shadow effect: $(".leftColumn").hover(function (){ $(".leftColumn h2").css("text-shadow", "0px 2px 3px rgba(0, 0, 0, 0.5)"); },function(){}); The problem arises when the text-shad ...

Cease and Begin Page Overflow / Scroll with a Single Click Feature

Background: I recently followed a tutorial on how to create a lightbox using HTML, CSS, and JavaScript. However, I encountered an issue where I wanted to disable the scrollbar functionality when the lightbox is displayed, preventing users from scrolling ...

Applying inline styles in PHP using if/else conditions

I want to customize the appearance of my table based on the status of each entry. Specifically, Completed = Green & Pending = Orange This involves PHP code that retrieves data from a MySQL database. $query = mysql_query("SELECT * FROM cashouts WH ...

Creating a foundational design with Bootstrap 5, featuring a sidebar that scrolls seamlessly and a stunning fixed

I'm having some trouble figuring out the layout for a webapp I want to develop. LAYOUT IMAGE What I envision is a sidebar menu on the "S" section that can be scrolled independently, and a large image on the "I" section that changes based on th ...

What is the best way to stack several elements on top of each other?

<div class="parent"> <div class="child" id="child-A"> <div class="child" id="child-B"> <div class="child" id="child-C"> </div> The main concept here ...

Error 401: Unauthorized access to CSS files in MVC

Initially, I am aware that this issue has been discussed here before, but none of the suggested solutions have worked for me. I have encountered a problem with adding my own CSS files to a new web project I created. The default code template provided by VS ...

Guide on setting up Sentry Vite-Plugin to upload sourcemaps within Quasar

Currently, I'm in the process of setting up error reporting for my Vue.js SPA application following Sentry's documentation. To enable Sentry to capture errors, a sourcemap is required due to minification during the build process, which Vite gener ...

One particular page is having trouble loading CSS, whereas it loads perfectly fine on a different page

I have two pages, 403.html and 404.html, both of which need to load the same style.css file. Strangely, when I visit the 403.html page, the CSS loads perfectly fine. However, when I navigate to the 404.html page, even though it has identical code with only ...

What is the best way to show a block of text when hovering over an image with a smooth easing effect

How can I make a block of text appear with ease when hovering over an image of a tree? The current setup works, but the transition effect is not as smooth as I want it to be: HTML: <p id="hover-text1" class="hover-text1"> Accomplished! </p> & ...

Flip the second column to the top for narrower screens

I am looking to create a layout with 3 columns. The left column (1) should have text that can resize based on content, the middle column (2) should contain an image of fixed width, and the right column (3) should also have text that can grow or shrink to m ...

Reorder the division elements within an HTML document

In my htm file, I have the following htm codes: <div id="masthead"> <div id="logo"> #logo<br /> #logo<br /> #logo</div> <div id="header"> #header<br /> #header<br ...

What is the best way to select specific elements within a table cell with vuejs?

I am facing an issue with a dynamically rendered table where each cell contains data in a readonly input box. The first cell has an edit button, and when clicked, the input boxes should become editable for user input. The edit button should then be hidden, ...

Bringing a module into Vue framework and transferring information

I'm currently working on a Nuxt project that includes a component. The component can be found in components/Boxes.vue: <template> <b-container> <b-row> <b-col v-for="box in boxes" v-bind:key="box"> < ...

Minimize bundle size by utilizing googleapis with Webpack

I am currently developing NodeJS code using TypeScript that I package with Webpack. My project relies on the "googleapis" NPM package, specifically for functionalities related to Gmail and OAuth: import { google } from "googleapis"; const oAuth2Client = a ...

Struggle of UL vs. LI Designs

Currently, I am tackling a web development project that entails a CSS file known as Style.css. Inside this file, a styling series is outlined for UL and LI elements, which have been implemented across various parts and sections of the project. The setup lo ...