Is it possible to modify the CSS of a parent element in vue.js only for the current router route?

I am trying to modify the parent component's style without affecting the CSS of other components. Here is an example from my code:

App.vue

<div class="page-content">
    <router-view ref="routeview"></router-view>
</div>

router/index.js

  routes: [
    {   path: '/home',            name: 'Home',       component: HomeView     },
    {   path: '/heroes',      name: 'Heroes',     component: HeroesView   },
  ]

HomeView

<style>
.page-content {
    background-color: red;
}
</style>
<style scoped>
</style>

If I include the .page-content CSS in the global CSS area as shown above, it also affects the styling of HerosView. This is not my intention - I only want to change the background of the .page-content under the current HomeView page.

Is there a way to achieve this? Can I isolate the style changes to specific components?

Answer №1

Include the styled class within the router.beforeEach function

router.beforeEach((to, from, next) => {
    if(...) {
        document.getElementsByClassName('page-content')[0].classList.toggle('styled');
    }
})

<style>
.page-content.styled {
    background-color: blue;
}
</style>

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

Is there a way to automatically import all images from a folder in VUE?

I'm attempting to automatically import all images from a folder. Here is what I've tried under // tested: <template> <p>classical art</p> <img v-for="image in images" :key="image" :src="image.url& ...

What is the reason that setting margin to 0 auto centers an element, while using margin 1 auto does not have the same effect?

When using margin: 0 auto;, it means that there is a margin size of 0 on the top and bottom of the element, with the available space being divided equally for the left and right margins. This behavior works as expected in the example below. In contrast, c ...

Issue with Bootstrap 5 Card Header not extending to full width

I have implemented tables within cards using a CDN. While the table looks fine in wide widths, it does not resize to fill the entire width when squeezed. Below is the HTML code: <div class="card table-responsive"> <div clas ...

What strategies should be considered for styling AngularJS components?

Currently, I am engaged in a project using AngularJS with the intention of gradually organizing things for Angular 6 or whichever version is available when we begin the upgrade process. A significant part of this endeavor involves converting existing direc ...

Reorganize items that extend beyond the list into the next column using Bootstrap

I have a row with two columns, where Column 1 currently contains 7 list items under the ul tag. However, I want to display only 5 list items in Column 1 and automatically move the remaining items to the next column (i.e., Column 2). Is there a way to ach ...

Which symbol is preferable to use in JS imports for Vue.js/Nuxt.js - the @ symbol or the ~ symbol?

I am seeking guidance on a matter that I have not been able to find a clear answer to. Webapck typically uses the ~ symbol as an alias for the root directory. However, I have noticed that some developers use the @ symbol when importing modules using ES6 s ...

Utilizing media queries and page width in a Moodle theme designed with Bootstrap framework

I currently have my LMS set up with Moodle 4 and the boost theme which is based on bootstrap (github) However, the default page layout appears very narrow on all devices and I would like to utilize the responsive design features of Bootstrap for different ...

When the bootstrap 5 dropdown is activated, flex wrap functionality triggers vertical scrolling

Every time I click on the dropdown, it causes a vertical scroll instead of popping out like a regular dropdown. I want to maintain the horizontal scroll while fixing this issue. Any suggestions on how to solve this problem? Demo: https://jsfiddle.net/d3j ...

Safari alters the header color on mobile devices

Debugging this issue has been quite challenging - the number at the top of the page appears in white before changing to a dark grey on iPad and iPhones running Safari. Why is this happening?! It functions correctly on all other devices! Check out www.col ...

What is the best way to display a book cover and position it in a specific location?

There is a dynamically populated HTML table using AJAX: (shown in the image below) https://i.sstatic.net/ig9Nv.png If I click on any cell in the table, except for headers c1 through c4, I want to display a cover hiding the cells to the left of the clicke ...

Using Vue CLI to Display Components across Different Pages with Flask

I am currently working on a non-spa application that uses pages served by Python Flask. In an effort to transition to using vuecli for the benefits of ES6, I intended to wrap the entire application in Vue components within the master.jinja2 template. Howev ...

Any solutions for dealing with longer labels in Bootstrap form-floating?

Is there a way to use Bootstrap's form-floating feature with longer labels so that the text box automatically expands to accommodate the text but doesn't cut off on white-space wrap when too long? I'd like to avoid using position-relative to ...

Is the use of !important included in Bootstrap 4?

Even though I've positioned the style.css file after Bootstrap, it still isn't working. Upon inspection, I noticed that... https://i.sstatic.net/ay621.png How can I solve this issue? .bg-info{ background-color: #17a2b8 !important; } ...

What is the process for connecting an HTML page to a CSS file located in a parent directory?

Here's the problem I'm facing: I recently encountered some organizational issues with my website, so I decided to reorganize my files for better classification. The current folder structure looks like this: www resources images ... css de ...

Eslint highlighting the initial function declaration within a Vue script

I'm currently creating a Vue file using a Typescript script, and encountering an unusual Eslint "error." https://i.sstatic.net/UXv3A.png The issue arises on line 15 with this specific complaint: Parsing error: Unexpected token, expected "," Interes ...

Ways to relocate the menu within the confines of the "menu border"

Is it possible to move the menu inside the border on this webpage? I am new to web design and was thinking of using position:absolute for the links. Should I also move the submenu along with it? Any suggestions on how to achieve this? Thank you! The goal ...

How to Deactivate Horizontal Scrolling Using CSS in WordPress

Hey there, I'm currently working on a WordPress blog and facing an issue. Here's the link to my problem: When I resize the browser (X-Axis) to a minimum, like when using a mobile device, I noticed that I can scroll to the right in the Content se ...

Ways to Design ASP.NET Buttons for Users with Disabilities

Recently, I attempted to style an asp button using CSS. The Button HTML: <asp:Button ID="btnClockin" runat="server" Text="Clock In" class="FullWidthButton" /> Here is the CSS code: .FullWidthButton {width:100%;} Everything was working fine until ...

Alter the query parameters of a Vue router-link without modifying the path

When utilizing Vue.js, I am implementing <router-link> for navigation in the following manner: <router-link :to="{ path: '/', query: { q: item.id, lang: lang } }">{{item.name}}</router-link> In this scenario, the path remains ...

Issue encountered when trying to integrate a CSS sticky footer with a liquid/fluid layout

I've been attempting to incorporate the CSS Sticky Footer technique demonstrated at cssstickyfooter.com. (I also experimented with Ryan Fait's solution without success). I believe I've followed all the instructions correctly. I have a conta ...