Angular - Implementing Opacity Inheritance for Child Components

Is there a way to prevent Angular Child components from inheriting their parent's styles, specifically opacity?

I have a form wrapper positioned on top of the page as shown below:

import {Component, EventEmitter, Output} from "@angular/core";

@Component({
  selector: 'new-student-form-wrapper',
  templateUrl: './new-student-form-wrapper.component.html',
  styleUrls: ['./new-student-form-wrapper.component.scss']
})
export class NewStudentFormWrapperComponent {

  @Output() public onNewStudentFormClose: EventEmitter<void> = new EventEmitter<void>();

  public newStudentFormCloseClicked(): void {
    this.onNewStudentFormClose.emit();
  }

}

::ng-deep {
  .student-details-form {
    opacity: 0;
  }
}

.new-student-form-wrapper {
  z-index: 50;
  height: 100vh;
  width: 100vw;
  position: absolute;
  background-color: white;
  opacity: 0.8;

  .c-pointer {
    cursor: pointer;
  }
}

<div class="new-student-form-wrapper p-4">
  <mdb-icon class="c-pointer d-flex close-icon" (click)="newStudentFormCloseClicked()" fas icon="times"></mdb-icon>
  <new-student-form></new-student-form>
</div>

There is a child component that contains the form behavior:

import {Component} from "@angular/core";

@Component({
  selector: 'new-student-form',
  templateUrl: './new-student-form.component.html',
  styleUrls: ['./new-student-form.component.scss']
})
export class NewStudentFormComponent {

}

.student-details-form {
  opacity: 1;
  background-color: purple; /** Just for testing opacity*/
}

<div class="student-details-form md-form input-group d-flex flex-column">
  <div class="form-header">
    <h1>Hello</h1>
  </div>
</div>

Even though I explicitly set the opacity of new-student-form.component to 0, it still inherits the container's 0.8 opacity rule.

https://i.sstatic.net/rw2DU.png

It seems like I am using ng-deep incorrectly, what would be the best approach to remove the opacity for the child? Is there an encapsulation method I can use to prevent the styles from leaking into another component?

Answer №1

Opacity affects the entire element, including its contents, and is not inherited by child elements. This means that both the element and its children will have the same level of transparency in relation to the background, even if they may have different opacities compared to each other.

If you want to create a transparent background, you can use the rgba function like this:

.new-student-form-wrapper {
  z-index: 50;
  height: 100vh;
  width: 100vw;
  position: absolute;
  background-color: rgba(255, 255, 255, 0.8);
}

For those using SCSS, another option is to utilize the transparentize utility function:

background-color: transparentize(white, 0.2)

This approach maintains some transparency for the form-wrapper while keeping child content clearly visible.


By the way, the usage of ::ng-deep in your code snippet might not be correct. One recommended method involves targeting only descendants within the current component by adding the :host selector:

:host ::ng-deep {
  .student-details-form {
    // add styles here
  }
}

To further explain the purpose of ::ng-deep, let's consider some examples. Essentially, this directive prevents the addition of the _ngcontent-xxx-xxxx attribute selector to the generated CSS (relevant when employing ViewEncapsulation.Emulated within a component stylesheet).

For instance, within a component's SCSS file:

::ng-deep {
  .form0 {
    opacity: 0;
  }
}

:host ::ng-deep {
  .form1 {
    opacity: 0.1
  }
}

:host {
  .form2 {
    opacity: 0.2;
  }
}

.form3 {
  opacity: 0.3;
}

.form-wrapper ::ng-deep {
  form4 {
    opacity: 0.4;
  }
}

This would yield the following result:

.form0 {
  opacity: 0;
}

[_nghost-xxx-xxxx] .form1 {
  opacity: 0.1;
}

[_nghost-xxx-xxxx] .form2[_ngcontent-xxx-xxxx] {
  opacity: 0.2;
}

[_ngcontent-xxx-xxxx].form3 {
  opacity: 0.3;
}

.form-wrapper[_ngcontent-xxx-xxxx] .form4 {
  opacity: 0.4;
}

In summary, using ::ng-deep at the root level allows for non-encapsulated CSS styling. Encapsulation is achieved through the host and content attribute selectors.

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 come the width of an element with a relative position remains maximized even when it does not have any child elements?

I am facing an issue with a "message" element that needs to be resized if the text exceeds its width within certain limits, similar to how it works in messaging applications. .message { position: relative; color: white; background: #182533 ...

How to prevent links from being affected by the Gooey effect in D3

Issue: When applying the Gooey effect, the links are also affected, resulting in a teardrop shape instead of a circle. The code snippet includes a dragged() function that allows users to detach node 1 from node 0 and reconnect them by dragging. The code s ...

Is there a way in JavaScript to prevent a function from being executed when a button is clicked and instead

Is there a way to automatically stop changing the background color with random colors after a few seconds when clicking on a start button? I attempted using setTimeout() but my limited understanding of JavaScript has hindered me from finding a solution. I ...

What is the best way to switch content between tabs using ajax and php?

I have organized my content into two tabs and I am switching between the tabs using JavaScript. <div class='tab-container'> <div class='tab-1'> <?php $sql="SELECT * FROM posts WHERE status='tab1'"; echo "<d ...

exploring the depths of nested objects and utilizing the 'for in

My issue involves receiving a json response from an API that includes objects within objects. It looks something like this: {Object}->{results}->{manyObjects} When I execute the following code: var list = data.results.list; for(val in list){ ...

Expandable Grid Sections in React MUI

Is there a way to create a grid layout where items with showDefault: true are always displayed at the top, and then users can click an arrow button to expand the grid and also show the items with showDefault: false? Any suggestions on how to achieve this? ...

Issue with Bootstrap 5 Dropdown not toggling back

<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Creative Admin Panel</title> <link rel="stylesheet" href=&q ...

CSS-WHAT Package for Angular Update - Enhance Your Styles!

In my Angular setup, the configurations are as follows: _ _ ____ _ ___ / \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _| / △ \ | '_ \ / _` | | | | |/ _` | '__| | | ...

Striving to convert Celsius to Fahrenheit using ReactJS

I am currently attempting to convert Celsius into Fahrenheit within this specific div. Despite being fairly new to React, I’m finding it challenging to determine where exactly to place the conversion calculation. return ( <div className="app& ...

Position the div to align with just one side of the table-cell

I am struggling with making the height of my code dependent on only the left column, while still allowing the right column to be scrollable if it contains more content than the left column. Despite my best efforts, I can't seem to figure out how to a ...

Initially, the OWL Carousel image is not displaying correctly due to incorrect sizing

I am currently working with OWL Carousel and have implemented a code that displays one image at a time, with the next image sliding in every 15 seconds. The width is set to occupy 100% of the screen and I have configured the JavaScript accordingly so that ...

How to float two divs using flex in Bootstrap 4

I'm having trouble floating two divs within a row or container div. Despite using the classes .justify-content-md-start and .justify-content-md-end, the divs remain next to each other without any float. I've also tried using classes like float-l ...

Angular and Three JS collaboration leads to misplacement of canvas rendering

Currently in the process of developing code for a website, I am attempting to integrate Three JS with Angular. After conducting some research, I have found that this combination is feasible and I have successfully merged these two libraries. However, I am ...

When I hover my mouse over the items on the Navigation bar, they shift forward

Seeking assistance with a coding issue. When hovering over TOYOTA, the HONDA and CONTACT US sections move forward unexpectedly. This movement is unwanted as I would like only the next list item to display when hovering over TOYOTA and HONDA. How can I corr ...

Failure to pass a variable declared within a function to another JavaScript file running simultaneously

After combing through numerous posts on Google and Stack Overflow, I am still unable to pinpoint why this particular issue persists. This case involves 2 HTML files and 2 JS files (explanations provided below in text following the code snippets). 1) inde ...

What is the process of modifying a list using a custom tag in JSP?

I encountered an issue with a custom tag in JSP. When the page finishes loading, the HTML content list generated by the div custom tag appears. I then aim to replace this list with new data using AJAX. First, I clear out the old list and create an HTML str ...

Getting the entire data block in ReactJS: A step-by-step guide

I have encountered an issue with a component I created that only displays the value of block[0], rather than showing the entire block value. For instance, if I input: HI Stackoverflow It only shows "HI" and not the complete content of the field. Is th ...

The hyperlink does not appear to be functioning properly within a specific Div on the iPad. Interestingly, this issue does not persist when using other

Apologies if I am posting this question in the wrong place. I have a webpage (built with asp.net/vb) where each section is contained in divs within a Bootstrap grid. Through the code behind, I am attaching onclick events to each set of divs to perform sp ...

jQuery iso-8859-1 encoding issue

For the past few days, I have been struggling to understand how charset functions. When sending a string like: §test through a form using AJAX, the output does not recognize the "§" case. Below is my code: var message = $("input[name='message&a ...

The error message "removeClass is not a valid function" appeared on the

When I am attempting to eliminate a class using eq(), it is not effective and an error occurs in the console displaying TypeError: $(...).removeclass is not a function $("table tr:eq(9)").removeclass('mandatory');**strong text** html code: &l ...