Exclude the parent background in the child class when inheriting other properties

I need the child class to inherit all properties from its parent, except for the background color. The immediate parent has a white background, but I want the child to skip this and take the background color from the grandparent component, which is bluebackground. Even when using transparent background in the child class, it should show as blue instead of white.

I am unable to make any changes to the Parent component, so I have to work with CSS only.

App Component:

<div class="bluebackground">
<app-test>
</app-test>
</div>

Component: test

BlueBackground Start
  <div class="whitebackground">
      Some White Background Stuff that I need
      <div class="child">
          Here I want the background to skip the parent's white background and display the same blue color as the bluebackground class.
      </div>
  </div>
BlueBackground End

CSS Styling:

.child {
    font-size: 2rem;
    text-align: center;
    color: green;
    background: transparent;
}
.bluebackground {
    background: blue;
    font-size: 3rem;
}

.whitebackground {
    background: white;
}

StackBlitz Link:

View on StackBlitz

Answer №1

If you're looking to make some changes to the CSS styling, consider updating it as shown below:

.child {
  font-size: 2rem;
  text-align: center; 
}

.bluebackground {
  font-size: 3rem;
}

.whitebackground {
  background:white;
  color:yellow;
}

.bluebackground, .child {
  background-color: blue;
}

See a live example here: Stackblitz

Update

There's a slightly unconventional method to bring in CSS selectors from the app component to the child component.

test.component.ts

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

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css', '../app.component.css']   // <-- include `app.component.css` here
})
export class TestComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

Now, you can utilize the bluebackground selector in the test component.

test.component.html

BlueBackGround Start
  <div class="whitebackground">
      Some White Background Stuff that I need
      <div class="child bluebackground">     <!-- include `bluebackground` here -->
          Here I want background which should skip the parent whitebackground and show blue color as per the bluebackground class <br />      
      </div>
  </div>
BlueBackGround End

I've made adjustments to your Stackblitz

Update: encapsulation

You have the option to set the encapsulation to ViewEncapsulation.None in the app.component and rename the child selector to bluebackground in the child component as well. Take a look at this approach:

app.component.ts

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  encapsulation: ViewEncapsulation.None     // <-- set `ViewEncapsulation.None` here
})
export class AppComponent  {
  ...
}

test.component.ts

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

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

test.component.css

/* overwrite/introduce values specific to the `bluebackground` in the test component */ 
.bluebackground {
  font-size: 2rem;
  text-align: center; 
}

.whitebackground {
  background:white;
  color:yellow;
}

test.component.html

BlueBackGround Start
  <div class="whitebackground">
      Some White Background Stuff that I need
      <div class="bluebackground"> 
          Here I want the background to override the parent whitebackground and display blue as defined in the bluebackground class <br />      
      </div>
  </div>
BlueBackGround End

Check out the updated example here: Stackblitz

Answer №2

Here is an example for you to try:

<div class="bluebackground">
  Start of Blue Background
  <div class="whitebackground">
      Here is some content with a white background that I need
      <div class="child bluebackground">
          In this section, I want the background to be blue like the main background, skipping the parent white background <br />      
      </div>
  </div>
  End of Blue Background
<div>

See a working demo here

Answer №3

Kudos to Michael D for sharing his insights.

Here's my solution:

To address the issue, I transferred the white background color from the existing "whitebackground" class to a new class named "newwhitebackground."

Html

BlueBackGround Start
  <div class="whitebackground">
      <div class="newwhitebackground">
      Some White Background Stuff that I need
      </div>
      <div class="child">
          Here I want the background to be different from the parent whitebackground and display blue like the bluebackground class <br />      
      </div>
  </div>
BlueBackGround End

CSS

.child {
    font-size: 2rem;
    text-align: center; 
    background:transparent;
}
.whitebackground {
    color: yellow;
}
.newwhitebackground {
   background-color: white;
}

Check out the stackblitz example here: https://stackblitz.com/edit/angular-ivy-pyhqwr

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

Enhancing Image Upload with Ajax/JavaScript: Generating Multiple Previews

I've been experimenting with an Ajax image uploader that I found on this website. Currently, I have managed to create duplicate preview images: one displayed under the input field and the other elsewhere on the page labeled as "this what you chose". H ...

The @angular/fire library encountered a FirebaseError stating that the first parameter in the collection() function should be either a CollectionReference, a DocumentReference, or a

I am encountering an error while attempting to access a firestore collection. Despite exploring various solutions on stackoverflow, I have not been able to resolve this issue. The code has been implemented with AngularFire Cloud Firestore document. For mo ...

How can I achieve a smooth opacity transition without using jQuery?

Although I understand that JQuery offers functions for this purpose, I am interested in finding a solution using only pure javascript. Is there a way to change the CSS opacity setting over time? Perhaps utilizing a unix time stamp with millisecond precisi ...

Utilize a jQuery selector to target the initial element of every alphabet character

I'm seeking some assistance with jQuery selectors and have a specific scenario that I need help with. Here is the HTML structure I am working with: <ul> <li class="ln-a"></li> <li class="ln-b"></li> <li cl ...

Creating a search field in Bootstrap 4 that emulates material design, facing challenges with transition animations

I tried to create a material design search field using Bootstrap form input. While I was able to make it work, I noticed a small issue with the focus state. When I click inside the field, the underline animation runs smoothly. However, when I click outside ...

The Angular 2 update I posted successfully refreshes my list, however, it does not reflect any changes in

My current project involves using Angular2 and MVC. While I've been following tutorials on angular.io, I've made some adjustments to tailor the solution to my needs. One of the main components is a table that displays clients along with their ide ...

How can you make the coordinates of the cursor track the cursor when it hovers over a rectangle?

The code provided below will constantly display the cursor's coordinates right below the cursor: function displayCursorCoordinates(e) { var x = event.clientX; var y = event.clientY; var coordinates = "(" + x + ", " + y + ")"; document.getEl ...

Adaptive grids in Bootstrap

I've been using bootstrap and I'm trying to get the items to align next to each other, but using the bootstrap columns doesn't seem to be working for me. I may be coding it incorrectly. <link rel="stylesheet" href="https://cdn.jsdelivr ...

After populating the grid with data, there are no scroll bars present

I'm facing some challenges while integrating ag-grid into my Angular application. Although I can successfully load data onto the grid, I encounter a problem where there are no scroll bars present after loading my dataset. Furthermore, attempting keyb ...

Issues with slideToggle and hover functionality in Jquery

I am currently working on a webpage that showcases team members' photos, along with their descriptions that slide down when clicked. I'm facing an issue where the hover effect for displaying names over the photos does not work in tandem with the ...

Elevate your website with Bootstrap 4's compact floating icon menu

I've been searching online, but I can't seem to find exactly what I need. My goal is to create a compact Sidebar menu that stays on the left side and is centered. The only solution I came across was this: `https://codepen.io/kemsly/pen/mJxwJg` ...

Issues with Angular Component not detecting changes in @Input array

I'm dealing with a challenging setup where: The Parent Service (A) is imported in the Parent Component (B). Then, the Parent Component passes an array of Objects to a Child Component (C), which are referenced from the Parent Service (e.g. <child-c ...

Can someone please share the steps on how to insert a new row into a PrimeNg DataTable component

I am currently working on enhancing a table by adding a new row underneath the existing data list. This new row will contain a save button and allow users to insert information, such as saving a phone number. While I have indicated where the new row should ...

Angular - What causes variables to lose data after component changes?

Having an issue with two components - one creating and changing an array, and the other getting the array. The problem is that when retrieving the array in the second component, it only shows the default empty data. Code for array creation: export cla ...

Creating Tab-Focused Accessibility in HTML Fragment (Modal Window)

I'm currently working on an Angular App with a custom Modal Dialog implementation. I'm trying to figure out how to restrict tabbing to just the elements within the Modal, rather than allowing users to tab through everything in the background. I ...

Looking for a way to conceal the scrolling content within a div beneath a transparent header? Seeking

This particular question has been asked multiple times, and after careful consideration, I found a solution that closely aligns with my scenario: Hide content underneath a transparent div while scrolling The crux of the issue is that the content div is m ...

Is it possible to incorporate a CSS3 transition into the styling of a list of images?

Looking to achieve a CSS3 linear transition for a "list-style-image" specifically for Firefox? You'll need to include "-moz-" in your code. -moz-transition: list-style-image 0.2s linear; However, the above code is not producing the desired result. I ...

How to set up scroll restoration for the Angular Standalone Router?

The Angular Router provides the option to restore scrolling functionality, with documentation on how to implement it when loading data. Is there a way to configure the standalone router to automatically scroll back to the top of the router outlet? A demo ...

What are the steps to create a table using divs and CSS?

Can someone guide me on how to create a table using divs, CSS, and proper XHTML formatting? <table width="100%" border="1"> <tr> <td style="width: 130px">1</td> <td align="center">2</t ...

Animate the fire with a click instead of a hover

Is there a way to incorporate a flip animation into a button using a click event instead of hover? Perhaps starting with the + button on the frontside could be a good starting point. Check out this link for reference I have grasped that the animation is ...