Unable to adjust the opacity of the router-outlet element for animations in Chrome

I am working on a single-page Angular 4.3.1 application that utilizes the <router-outlet> tag for component rendering.

The problem I am facing is that the component is rendered within an enclosing element named: <ng-component> (when no selector is specified).

This becomes problematic when I set the opacity of the wrapper element, as it does not affect the child elements. This behavior seems to be due to the fact that the wrapper is a custom DOM element.

Here is an example:

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>

<body>
  <ng-component style="opacity: 0.2;">
    <div>First Line</div>
    <div>Second Line</div>
    <div>Third Line</div>
  </ng-component>

  <div style="opacity: 0.2;">
    <div>First Line</div>
    <div>Second Line</div>
    <div>Third Line</div>
  </div>
</body>

</html>

I am using Chrome version 59.0.3071.115, which is currently the latest version available.

For reference, here is a screenshot in case the issue is specific to my environment:

https://i.sstatic.net/znMtZ.jpg

I also tested this in IE11 and found that the opacity worked fine there. Is anyone else encountering this issue?

Update

As requested, here is the Angular routing animation that I am trying to implement successfully in Chrome:

export const routerAnimation =
    trigger('routerAnimation', [
        transition(':enter', [

            // styles at start of transition
            style({ opacity: 0 }),

            // animation and styles at end of transition
            animate('0.3s', style({ opacity: 1 }))
        ]),
    ])

@Component({
    selector: '...',
    templateUrl: '...',
    styleUrls: ['...'],
    animations: [routerAnimation],
    host: { '[@routerAnimation]': '' }
})

Answer №1

You have the ability to embed a <style></style> block within a

<ng-component></ng-component>
tag. In this particular scenario:

<style>
    ng-component > div {opacity: 0.2;}
</style> 

Below is the entire code snippet:

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>

<body>
  <ng-component>
  
      <style>
        ng-component > div {opacity: 0.2;}
      </style>      
  
      <div>First Line</div>
      <div>Second Line</div>
      <div>Third Line</div>
  </ng-component>

  <div style="opacity: 0.2;">
    <div>First Line</div>
    <div>Second Line</div>
    <div>Third Line</div>
  </div>
</body>

</html>

This code has been tested and functions correctly on Chrome Version 59.0.3071.115.

Answer №2

Here's a different approach that does not require any manual changes to the body tag of your question.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script>
  document.addEventListener("DOMContentLoaded", function(){
     let ngComponents = document.querySelectorAll('ng-component');
     ngComponents.forEach(function(ngComponent){
         Array.from(ngComponent.children).forEach(function(child){
             child.setAttribute('style', ngComponent.getAttribute('style'));
         });
     });
 });
</script>
</head>
<body>
  <ng-component style="opacity: 0.2;">
    <div>First Line</div>
    <div>Second Line</div>
    <div>Third Line</div>
  </ng-component>
</body>
</html>

I achieved this using JavaScript, but if you prefer not to use jQuery in your project, you can rewrite it in pure JavaScript.

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

Mistakes are triggered when a non-submit button is clicked in Angular Reactive Forms

Working with angular 7 and a material design toolkit, I have encountered an issue with a form that has multiple validations and two buttons. One button is for opening the file picker (labeled as 'upload image'), while the other one is for submitt ...

The values coming from my cascading combo boxes are incorrect

My form contains drop down boxes with options that dynamically change the values of the next drop down box based on the selection. When I choose the first training option, the dates are successfully sent through the form to my email address. However, i ...

Unable to insert image into div element

Having trouble aligning profile images next to names instead of below the text within the div. Can't seem to figure out how to fix this issue. Just to clarify, I want the images to appear alongside the text on the page (the white image next to ' ...

SASS warning: Units 'px' and 'px*px' are not compatible

I've been working with SCSS to customize the style of Bootstrap 4 for my project, but I encountered an error while compiling my SCSS code: { "status": 1, "file": "H:/!WEBSITE/modules/The Force - Bootstrap/content/scss/variables/variables.scss", ...

What is the best way to align my header buttons in the center and move items to the far right of my toolbar?

My header currently displays as follows: https://i.sstatic.net/h8zDQ.png I am aiming to center the [LEARN MORE, ABOUT, RESOURCES, CONTACT] buttons in the middle of the navbar and align the profile icon and switch button to the far right of the page! I en ...

Learn the steps to dynamically generate dropdown items using Angular8

I have been exploring various basic examples on how to utilize the draggable directives, but now I am looking to achieve this programmatically. There is a simple sample application that showcases the issue. It does receive the "dropped" event, however, bot ...

What is the best way to apply a class to a container when a component in AngularJS receives focus or is clicked?

I am trying to apply a class to the container when either the input field is focused or when the dropdown component receives focus or is clicked. The class should be removed when the focus is lost. The requirement is for the input container to have the &a ...

Passing null values between components using @Input()

One component is sending values to another: paquete-detalle.component.ts nombre: string; precio: number; ngOnInit() { const id = this.activatedRoute.snapshot.paramMap.get('id'); this.fs.getPaqueteObject(id).valueChanges().subscribe(paquete = ...

Material Design Forms in Angular: A Winning Combination

I'm currently working on developing a form using Angular Material. This form allows the user to update their personal information through input fields. I am utilizing "mat-form-field" components for this purpose. However, there are certain fields tha ...

Form-linked Progress Bar

This is a little project I created (for fun and learning purposes, even though it may not be the most optimized solution). If you're interested, here's the link to the code: https://codepen.io/paschos/pen/xxGXMQb I'm currently seeking assi ...

Image background within the textarea of Contact Form 7

Struggling to insert a background image inside a textarea on a contact form. The issue arises when the browser window is resized, cutting off the image and failing to fit perfectly within the textarea. I want the textarea to fully display the image and adj ...

Expanding the size of select dropdown in Material UI to accommodate more options

Is there a way to increase the width of the drop down to 400px? I tried adding inline styles, but it didn't work. Upon debugging, I realized that the width is being overridden by this class. I'm not sure how to overwrite it. Could you please prov ...

While attempting to reinstall the admob-free plugin via npm, I encountered an error stating that it was missing a package.json file

While developing an app using Ionic, I encountered an issue with the AdMob plugin not being installed correctly. Trying to resolve this, I attempted to reinstall the plugin multiple times but kept running into errors. Seeking help from various threads, I ...

issues encountered when attempting to modify the background color of navigation items using bootstrap

Presently, I am focusing on enhancing the navigation bar of my website, but I have encountered an issue with my JavaScript code. I have implemented a scroll-spy feature in my navigation and added JavaScript for smooth scrolling. Additionally, I aim to make ...

Scrollbar for aligning text vertically

How can I control the visibility of the vertical scrollbar in a text area on IE when the content exceeds its height? Chrome only shows the scrollbar when needed, but IE seems to always display it. I've tried using -ms-overflow-style:auto and -ms-overf ...

Angular2: Once user is authenticated, navigate to specific routes

I've developed an admin panel with a router for page navigation. The layout of the admin panel includes a sidebar (Component), header (Component), and content (Component). Within the content, I have inserted <router-outlet></router-outlet> ...

When TTF fonts are installed on the device, they take precedence over Google fonts

I have successfully implemented the Ubuntu font from Google Fonts on my website: <link href='http://fonts.googleapis.com/css?family=Ubuntu:300,400,300italic,400italic,500,500italic,700,700italic' rel='stylesheet' type='text/css ...

What methods does DevTools offer for identifying the font used in an SVG?

Can anyone help me identify the font used in an SVG marker displayed on a Google Maps surface? I am specifically referring to the number 9 on a blue marker in this codepen. The label has these properties: label: { text: "9", color: "white", f ...

CSS rule for elements in a disabled state

Currently, I am utilizing AngularJS to manage the enabling and disabling of image buttons. Despite my attempts to use a CSS selector to display them as transparent once disabled, it is not working as expected. While testing with a provided example that app ...

Alternate option for CSS3 transitions

My code includes a CSS3 transition that adjusts the size and position of an image within a div when hovered over. Check out the demo on FIDDLE While this effect works well, I am concerned about older browsers such as IE9- that do not support CSS3 transit ...