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 onClick event for a q-btn does not seem to be functioning properly when used

Inside the q-btn, there is a q-checkbox. Additionally, I included a div to style the text. <q-btn flat color="blue" class="full-width no-padding" @click="tog(item)" > <q-checkbox ...

What is the best way to save the city name received from geolocation into a variable and then make an AJAX request?

<script> new Vue({ el: '#fad' , data: { data: {}, }, mounted() { var self = this; navigator.geolocation.getCurrentPosition(success, error); function success(position) { var GEOCO ...

Aligning the icon within the div and adding a gap between each div

I'm fairly new to web design, specifically dealing with html and css, and I'm attempting to create something similar to the design in the following image: My struggle lies in centering the fa-envelope-o icon both horizontally and vertically. I&a ...

How can I simplify the CSS properties for this border?

I created a div named "commentbox" and I want to apply a border with the color #ccc. However, I only want the left, top, and bottom sides of the div to be bordered, leaving the right side untouched. Thank you! ...

By default, VueRouter should automatically select the first child route

Hello everyone, I am currently utilizing Vue-Router version 2.5.3 and have implemented the Route structure outlined below. path: "/admin", component: require("./views/layouts/MasterLayout.vue"), meta: { requiresAuth: true }, children: [ { ...

Discover the name of a color using its HEX code or RGB values

Is there a way to retrieve the color name from RBG or HEX code using JavaScript or JQuery? Take for instance: Color Name RGB black #000000 white #FFFFFF red #FF0000 green #008000 ...

Guidelines for Implementing Stylesheets from a Referenced Node Module

I am currently developing a VS Code module that incorporates highlight.js to produce HTML for syntax highlighting of source code. Within the highlight.js npm package, there is a directory named styles containing various CSS files that I intend to utilize. ...

Navigation menu with submenus containing buttons

I attempted to incorporate a dropdown into my existing navigation bar, but unfortunately, the dropdown content disappeared after adding the necessary code. I am now at a loss on how to troubleshoot this issue and make the dropdown function properly. Despit ...

Ways to conceal a division within the Repeater

Currently, I am developing a VB ASP.NET site using Visual Studio 2012 Express for Web. Within my project, there is a Repeater containing two div elements with the css classes .dnnFormLabel and .dnnFormItem. Here is a snippet of code from the Repeater: < ...

Vuefire encountering an issue with Vue 3 and throwing a Vue.use error

After setting up a Vue app and importing Vue from the vue module, I encountered an issue: ERROR in src/main.ts:4:5 TS2339: Property 'use' does not exist on type 'typeof import("/data/data/com.termux/files/home/ishankbg.tech/node_modules/vue/ ...

The division of columns in the Bootstrap grid is malfunctioning

Recently, I embarked on my journey with Bootstrap and encountered an issue with the Grid Col System. I am aiming to center the logo and navigation bar on the page by dividing the Grid layout into 4 col-lg-4 (md-4). Subsequently, I inserted the image and na ...

I am currently utilizing grapesjs for my project and I am faced with the challenge of saving data into my database. However, the issue lies in the fact that grapesjs sends the

When I try to save data into my database using GrapesJS, it seems that the URL being sent is not the one I specified in the store manager URL store. Here is the code snippet: import 'grapesjs/dist/css/grapes.min.css'; import plugin from 'gr ...

Instead of utilizing components, consider using multiple Vue instances for a more streamlined

I'm currently working on updating an old .NET MVC project that utilizes HTML, Bootstrap, and jQuery for its Views. I've decided to switch from jQuery to Vue.JS, but I'm struggling to determine the best approach. Since all the views are uniq ...

How can I make Vuejs update v-model when the text input is changed using Jquery?

I am currently working with 4 inputs that have a timeselector popup. The jquery timepicker plugin I am using does not seem to provide an obvious way to manipulate an input through a callback function. Interestingly, Vuejs does not detect changes made to th ...

Generate individual CSS files using postcss and rollup

When I import each css file in my JavaScript, I want it to generate a new css file for the build. For example: import "./app.css"; import "./admin.css"; This would result in creating dist/app.css and dist/admin.css. My configuration fi ...

The CSS Validator does not recognize the CSS pointer-events and appearance properties

I'm a CSS beginner who recently created an app. I've encountered some errors and warnings when validating my CSS code: https://i.stack.imgur.com/g4MUz.png https://i.stack.imgur.com/Es1DE.png Could someone kindly explain these errors and warnin ...

Exploring Vue's feature of passing props and emitting events within nested components

Within my coding project, I am facing the challenge of properly implementing a parent component containing a form that includes a birthday component. This birthday component consists of two separate components within it. My task is to effectively pass prop ...

What is the process of inserting information into a nuxt-link in nuxt.js?

I am currently facing an issue with passing data into nuxt-link. Whenever I click on the link, nuxt-link returns a 404 error and fails to load the file. However, the other two links that use :href and hardcoding seem to be working fine. <template> ...

How can I apply max-width and max-height to a background image?

It seems like a simple task. I am currently transitioning my layout to become more fluid by working on creating scalable images. While using the img tag and setting max-width to 100% is effective, I am considering using a div with the image set as its bac ...

Vue.js and Laravel-Vue.js seem to have trouble detecting updates in their components

Just started diving into Vue.js while working with Laravel. Everything seems to be running smoothly and my components are showing up just fine. However, I noticed that whenever I refresh the page, I lose all the changes. How can I make sure that these chan ...