Different custom background images for every individual page in Nuxt

Looking for a unique background-image for each page in my app is proving to be challenging. Currently, I have defined the background-image in style/app.scss.

@import 'variables';
@import 'page_transition';
@import url('https://fonts.googleapis.com/css?family=Work+Sans&display=swap');
body{
  font-family: 'Open Sans', sans-serif;; // Global font definition
  background-image: url('../../static/background.png');
  background-repeat: no-repeat;
  background-color: $darkPurple;
  scroll-behavior: smooth;
}

I've attempted:

Adding the background url dynamically to the wrapper in the default layout. Result: The wrapper is not the body, so it did not yield the desired outcome.

Also tried adding

body {
  background-image: url('../../static/background.png');
}

in the <style> of each page with scoped. Result: Styling applied to body is disregarded. Tried without scoped as well. Result: Every page ended up having the same background.

Throughout these trials, I made sure to comment out the css line defining the background in style/app.scss

Answer №1

When working with different pages, ensure to adjust the background attributes of the body element using the mounted() hook.

For instance:

export default {
  mounted() {
    document.body.style.backgroundColor = 'red'
  }
}

If you want to set a background image, consider having an image file located at /static/images/bg2.jpg.

export default {
  mounted() {
    document.body.style.backgroundImage = "url('/images/bg2.jpg')"
  }
}

Keep in mind that by utilizing the mounted() hook, you are directly manipulating the DOM for each page. Failure to include this method on a specific page will result in the background image remaining unchanged from the previous page you visited.

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

The empty string is not getting recognized as part of an array

Currently, I have a textarea field where pressing enter submits and creates a new item in the array. Pressing shift + enter creates a new line in the textarea input field. But when trying to submit by pressing shift and enter after creating a new line, it ...

How to stop VueJS from exhibiting reactive behavior

Is there a way to utilize VueDraggable to create a cloned item that is completely independent from the original? You can refer to this fiddle for clarification: https://jsfiddle.net/32f7yu7c/69/ In the provided example, dragging an item from one list to ...

Display a loading indicator while Axios sends the Ajax request

I'm currently working on a Vue app and I am utilizing Axios for API calls. Before each call, I display a loading icon that hides once the call is completed. I'm curious if there is a way to implement this functionality globally so that I don&apo ...

What is the best way to vertically align a pseudo element image using CSS?

After looking for similar questions, none of the answers seem to be working for my specific issue. This is what I'm struggling with: I am attempting to insert and center a decorative bracket image before and after certain content in the following man ...

Turning Vue Table Headers into Clickable Links

Can someone help me figure out how to make a table header in Vue clickable? I've tried using v-on:click and @click but it's not working. <tr> <th scope="col" class="text-center">No.</th> <th scope=&q ...

What are the advantages of compiling our CSS and JS files in Laravel? How does it benefit us?

Just starting out with Vue and came across a video where they were compiling their assets. Got me thinking, why do we need to compile our assets? Can we use Vue and Vue-router in Laravel without asset compilation? If so, how? ...

The columns in CSS grid-template are refusing to change despite the media query code being applied and active

Currently, I am facing an issue while working on a site plugin and trying to utilize CSS grid to showcase posts. Despite the media query being active according to the inspector, the modification in the template columns is not reflecting as expected. Check ...

What is the most effective method for transferring an error message from the server backend to the vue frontend?

I'm currently working on implementing error handling for a Vue/Vuetify application. The challenge I'm facing involves using external pagination for a datatable that connects to an API with rate limiting restrictions. If users exceed the limit, I ...

After deploying DRF, the CSS in Django admin is not displaying

After developing a web application using Django Rest Framework and React, I faced an issue during deployment on IIS. While the deployment is successful, I encountered a problem with the Django Admin where the styles were not displaying properly. This led t ...

What is the counterpart of `width: max(100%, fit-content)`?

In order for my div to adjust its size based on the content if the parent div is smaller than the content, and match the size of the parent otherwise, I have been trying to achieve the first scenario by using width: fit-content, and the second scenar ...

Is it possible to establish a delay for requestAnimationFrame()?

My webpage has a very long layout with text in one column and a floating element in another. I want the floating element to follow the scroll of the window and return to its original offset once scrolling stops. The current code I have is: var ticking = ...

External elements - My physical being is in a state of chaos

Hello there, I hope everything is well with you. I have been working on creating a form using MaterializeCss to calculate an invoice for a craftsman. The user is required to input a number or select an item. Everything seems to be going smoothly so far. ...

Having trouble with changing the color of an element when the radio input is checked?

Can someone help me troubleshoot this HTML and CSS code? <label class="radio"> <input type="radio" name="plan" value="plan" required> <span> <h6>Plan</h6> <i class="pe-7s-graph1 ...

CSS styling is not being applied to buttons within Ionic 2

I'm completely new to Ionic and hybrid apps as a whole. I've been experimenting with a test app, but for some reason, none of the CSS seems to be working. Could someone please help me figure out what mistake I might have made? Here are my files: ...

Styling in CSS is being applied to a button element within a React component,

Having trouble with a button styled with the className 'actions' The button displays the CSS styling from '.actions', but not '.actions button'. Both should be applied. This code snippet works for all elements ...

Having trouble accessing a DOM element within a Vue component while using vanilla JavaScript

I am attempting to dynamically update data from a Vue component using jQuery in a Webpack project. Below is the code snippet: <template> <div id="container"> <slot>show string!!</slot> <div id="s_container"&g ...

CSS code for vertical navigation arrows to remain on both the left and right sides of the webpage

I'm struggling a bit with the CSS. I want to recreate the same effect as seen on . The left and right navigation arrows stay fixed vertically even when scrolling up or down the page. Does anyone have any code examples for that? ...

Distinguishing between a video or image when uploading files in JavaScript

I've been researching whether JavaScript can determine if a file is a video or an image. However, most of the information I found only shows how to accept these types using the input tag. What I really need is for JS to identify the file type and the ...

How can I position a form next to a title when utilizing the Bootstrap Panel feature?

I have experimented with multiple solutions found on various sources, but unfortunately none have been successful. I came close to achieving the desired result, but encountered an issue with the panel's default background color not displaying properly ...

What methods can I use to minimize the frequency of the blue box appearing around text?

I successfully developed code that enables user interaction with text, triggering the opening of a modal box when clicked. In this modal box, the text turns red upon clicking, indicating activation of a checkbox. However, I notice that every time I intera ...