How can I use Angular 7 to create a background image for a View made up of multiple components?

Here is the HTML code that I am working with:

<app-navbar></app-navbar>
<router-outlet></router-outlet>

My goal is to have an image set as the background for the home view, which consists of two components: navbarComponent and homeComponent (selectors). However, I do not want this background image to be applied to other components like contactComponent (which should use a different image) with the selectors app-navbar and app-contact.

Any suggestions on how I can achieve using an image as a background specifically for the view created by the app-navbar and app-home components?

Answer №1

If you want to set a background image for your website using CSS, one way to do it is by adding the background image as a CSS class for the <body> tag in your global styles.css file. Then, on the specific page or route where you want the background image to appear, you can add that class name to the <body> tag using either of these two options.

The first option is simple but not highly recommended:

constructor(@Inject(DOCUMENT) private document: Document) {}

ngOnInit(){
   this.document.body.classList.add('test');
}

The second option (my personal preference) involves implementing a custom rendering class from the @angular/core package:

import { Component, OnDestroy, Renderer2 } from '@angular/core';

export class myCoolComponent implements OnDestroy {

  constructor(private renderer: Renderer2) {
    this.renderer.addClass(document.body, 'test');
   }

  ngOnDestroy() {
    this.renderer.removeClass(document.body, 'test');
  }

Hopefully, this information proves useful to you!

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

Adding a class to a field tag in a Django form: A step-by-step guide

Is there a way to add a bootstrap class to the label of an input field in a form? I know how to add classes to the input field itself, but I haven't been able to find a guide on adding a class to the label within the form declaration. Can anyone provi ...

Is the input field not properly centered on the page?

Can someone explain why the input field is not aligning in the center? I am new to web development and need some assistance. <input type="text" id="search_bar" class="form-control" placeholder="Search" align="center"> In my CSS, I've ...

OAuth2 Authorization Code flow with client secret confidentiality protection

Recently, I created a demonstration on the Authorization Code flow of OAuth2 using Spring Security Cloud in conjunction with an Angular 2 client. Thus far, everything is functioning correctly as I am receiving the access token response from the server. I ...

Notification fails to display following POST request

Whenever the select menu displays "Please select your country" and I click the checkout button, an alert pops up. But if I select Canada, then go back to selecting "Please select your country" and press checkout, no alert appears. I need the alert to alway ...

Retrieving a tag from bs4.element.tag results in an empty value

As I follow a tutorial to extract answers from a Quora URL, my code is set up like this url = 'https://www.quora.com/Should-I-move-to-London' r = requests.get(url) soup = BeautifulSoup(r.content, 'html.parser') answers = soup.find(" ...

MUI: Interaction with a button inside a MenuItem when not interacted with MenuItem itself?

Currently, I am utilizing MUI's Menu / MenuItem to create a menu of missions / tasks similar to the screenshot below: https://i.sstatic.net/6FGrx.png The MenuItem is interactive: // ... other code <MenuItem value={mission.issu ...

What is the best way to flip cards with the onClick event in JavaScript?

My cards are currently facing down with the code* provided. What steps should I take to flip them face up using onClick functions? Furthermore, how can I store the IDs in an Array and use them to retrieve images from my image collection? HTML <table s ...

Guide to setting up a Datetime picker in Bootstrap 5 user interface

I've been struggling to implement a datetime picker for Bootstrap 5 (my front end is using Bootstrap 5.1.0). While I came across a solution for Bootstrap 3, unfortunately it doesn't seem to work with Bootstrap 5. ...

Encountering a compilation error while trying to utilize a union type in a function parameter within an

As stated on https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html, it is recommended to utilize multiple types for a parameter in a function (refer to the union part) /* OK */ interface Moment { utcOffset(): number; ...

The naming convention for CSS classes could be defined as follows: .sa-list li a > div{

As I dive into a CSS example, one particular section of code catches my eye: .sa-list li label > span, .sa-list li h3 > span, .sa-list li h4 > span, .sa-list li a > div{ position:relative; display:table-cell; vertical-align:middle; ...

Retrieving addresses by extracting p from the Python class within a div

Currently the code: Extracts URLs for various gyms and saves them in a CSV file as shown below: https://www.lifetime.life/life-time-locations/al-vestavia-hills.html https://www.lifetime.life/life-time-locations/az-biltmore.html Desired functionality: I&a ...

Python // Regular Expressions // Categories

My attempt to retrieve specific text content from various HTML tags using BeautifulSoup (BS4) proved unsuccessful. The code snippet I used only managed to extract the first text item after the <td tag, leaving out the rest that I needed. My goal is to p ...

Exclude mock files from webpack configuration in AngularClass/angular2-webpack-starter for production builds

I've encountered some obstacles while working with the AngularClass/angular2-webpack-starter framework, specifically in configuring its webpack settings. My goal is straightforward, yet I've been struggling to achieve it. In my project directory ...

A guide on incorporating dynamic formControlName functionality into AngularJs2

Currently, I am building forms using the form builder in AngularJS2. My goal is to incorporate the formControlName property/attribute into the form element as shown below: <input type="text" formControlName={{'client_name' + i}} placeholder=" ...

Setting the z-index to place an absolutely positioned element below a relatively positioned one

I am currently facing a challenge where I need to position an element under another one that has already been relatively positioned. The blue box must remain relatively positioned due to specific constraints within the website development process. For bet ...

Converting FormBuilder object to a generic object

Is it possible to convert FormBuilder values into an object model? this.form.value= this.modelObject; -> simply does not work let objectModel: ObjectModel = new ObjectModel(); objectModel.objecta = "valueA"; objectModel.objectb = "valueB"; this.for ...

What is the best way to include a specific stylesheet for Blackberry Curve devices based on certain conditions

Is there a method to include a custom style-sheet specifically for the 'Blackberry curve 2007'? Or perhaps implement a redirect that can identify this device and direct them to a different .html page? I'm looking to create a simplified versi ...

What is the best way to eliminate the blue outline around an image map?

On the landing page, I have a static background image and I've set up a clickable area using an image map. The issue is that when users click on the area, a blue border appears (see image below). I've tried several methods to remove this border b ...

Tips for inserting a PHP array into an email communication

I have a question regarding sending emails through PHP. How can I include PHP arrays in the message section of an email? Here is a simple array that I created and attempted to include in the email, but it seems incorrect as I couldn't find any releva ...

The appearance of the mock button varies between the development and production environments due to CSS styling

I'm currently using CSS to style a hyperlink in order to give it the appearance of a button. This is for a .NET website. a.pretend { display:inline-block; border-top:1px solid #CCCCCC; border-left:1px solid #CCCCCC; border-bottom:1px solid #999999; b ...