Update the background image of a Vue app based on the current route

I am currently facing a dilemma in my application where I need to add a background image to certain routes while leaving others blank. Here is a snippet of my app.vue file:

<template>
  <div id="app">
    <div class="h-100">
      <div class="bg" >
        <NavBar/>
        <router-view></router-view>      
      </div>
    </div>
    <Footer/>
  </div>
</template>

To add the background image on the '/' route, I utilized the following code within my Home.vue component:

<style >
.bg {
  height: 100%;
  background-image: url('../assets/equipo.jpg') !important;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
</style>

However, this applied the background image throughout the entire application. To remove the image from specific routes, I attempted to target the parent element like so:

<style >
 .bg {
   background-image: none;
 }
</style>

Unfortunately, this removed the background image from all routes instead of just the intended ones. I also experimented with scoped styles for the second component, but that did not yield the desired result either.

It seems like the main question here is: How can I modify a parent element's style from within a child component's scoped style?

Answer №1

When working with your component(s), consider implementing "scoped" styles:

<style scoped>
// Add your CSS styling here - it will only apply to the specific component and won't spread to others.
</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

How to conditionally prevent event propagation to children in VueJs

This Vue component is called ColorButtonGroup, and it serves as a group of checkbox/toggle buttons. It has a maximum limit of 4 colors that can be selected. The component utilizes another component called ToggleButton, which is a simple toggle selection w ...

Retrieve the content from paginated pages using ajax, apply a filter to it, and then add it to the existing page

My blog needs more functionality and I want to avoid using paginated pages. My idea was to extract content from these paginated pages through ajax, filter it, and add it to a specific div element. I'm not completely confident if I am on the right tra ...

Refreshing JqGrid tables after making changes and saving a row

In my PHP file, I have 4 jqGrid tables where I pass a different JSON array for each table. The data displayed on the grids come from one table with various SQL conditions. I am using inline editing for all the grids. After editing and saving a row in tabl ...

Tips for targeting an element for focus following a re-render in ReactJS

Within my web application, when a user hits the enter key, they are able to save the current record. A message confirming that the "record has been successfully saved" is then displayed. However, I have noticed that the blinking cursor in one of the input ...

Activate a commandButton action using a JavaScript-passed variable in JSF

I want to perform an Ajax call from my .xhtml page to my ManagedBean function. Here is the code snippet of what I am trying to achieve: .xhtml form code <h:form id = "jsfform"> <h:commandButton id="button" action="#{c ...

Transforming JSON data from JavaScript to Python JSON format

I am working with JavaScript JSON data: var data = '{a: 10, b: 20}' Now I need to convert this into Python JSON. Any suggestions on how to achieve this? Current Scenario: I have a script that extracts text from a website. The data on the webs ...

unable to reset contact form functionality

I need to implement a Contact Us form on my website. The form should clear the text fields after submission and display a Thank You message. Below is the HTML code: <div class="form"> <div id="sendmessage">Your message has been sent. Thank yo ...

Can you verify my comprehension of the process for iteratively displaying numerous custom Tree components in Vue.js 3?

While exploring the Vue.js documentation, I came across an example of iteratively rendering a Tree using the v-for directive. My aim was to modify the code to render multiple TreeItems in the App.vue file. I am puzzled by the fact that it is possible to i ...

Interactive Image Effects with Hover using HTML and JavaScript

I have observed that the transform works fine, but there is a slight issue when initially hovering or touching the image. After the first touch or hover, everything works great. Any assistance on fixing this minor issue would be greatly appreciated. Thank ...

The node.js application was unable to locate the '../HMS/server/models/user' file

Hi there! I'm currently working on an application with a folder structure as shown below. I want to use the following line in my passport.js file: var User = require('../server/models/user'); However, I encountered the error below after tr ...

What is the best way to update all HTML content using Javascript?

After attempting to use $("html").html(this.responseText);, I found that it successfully replaced the content but did not replace the head and body tags. For instance, if I intended to replace the following content: <!DOCTYPE html> <htm ...

The Firebase JQuery .on method is incrementally updating individual values in an array instead of updating them all simultaneously

I am trying to update the values of the orders placed by users on the Corporate's page without a refresh. I have implemented the jQuery .on method for this purpose. However, the values are being returned one by one from the array created for the order ...

Utilizing JavaScript to Display JSON Content on an HTML Page

I am hoping to achieve a layout similar to this: Image related to the output My question is, how can I generate this output without repeatedly including <p> using just pure JavaScript and JSON, without relying on any JavaScript libraries? The JSON ...

Acquire a JSON response from a web address by utilizing JavaScript

If you navigate to , you will find a JSON file filled with information about your current geolocation. My goal is to save this JSON data into JavaScript variables, allowing me to manipulate and extract specific fields from the file. ...

Unable to display elements from an array in the dropdown menu generated by v-for

Having just started learning Vue.js, I am facing a challenge in rendering the following array: countries: ["US", "UK", "EU" ] I want to display this array in a select menu: <select> <option disabled value="">Your Country</option& ...

What's the deal with $routeProvider in angularJS?

I'm having some trouble with my simple web app that I'm trying to update using route provider. Everything works fine when I click on the list items on the page, but when I refresh the page, I get a 404 error. It seems like it's trying to acc ...

Require a warning notification or alert prior to accessing any mature content section on a website

Once upon a time, I stumbled upon a solution online involving javascript that would fade out a webpage until the user clicked Yes or No. If they clicked No, it would redirect them to the front of the site; if they clicked Yes, it would reveal the adult c ...

Vuejs - Enhancing User Experience with Multiple Unique Loading Indicators for Various States

I am in search of a spinner like the one showcased in this tutorial on Adding Loading Indicators to Your Vue.js Application. However, I need this spinner to function differently for various elements and states. For instance, when the first image is being ...

Ensuring a DIV remains fixed in place for a specific number of scrolls

I'm looking to create a 'Page section' that remains in place while scrolling for a specific distance and then smoothly transitions to the next section. I've attempted to implement this within the child theme without success... Any sugge ...

Animating the movement of a div

How can I move the div "#menu" to the side when it's clicked on? I've tried the following code: <script> $("#menu").click(function(){ $("this").animate({ marginLeft: "+=250px", }, 1000 ); }); < ...