Tips for altering the style of a child component using Emulated encapsulation in Angular 6

Is it possible to update the CSS of a child component from its parent without changing the encapsulation view?

Check out this demo for reference.

app.component.ts

<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>

hello.component.ts

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

@Component({
 selector: 'hello',
  template: `<h1 class='test'>Hello {{name}}!</h1>`,
  styles: [`.test { font-family: Lato;color:red; }`]
})
export class HelloComponent  {
  @Input() name: string;
}

app.component.css

:host ::ng-deep .test{
  color:green;
}

p {
  font-family: Lato;
}

Despite attempting to override the style in chrome toolbar, the child component still displays color:red.

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

Answer №2

The :host pseudo-selector targets the main element of your component (for example, <app-hello>, which is not included in its own HTML markup). It's a straightforward concept.

::ng-deep functions a bit differently. When Angular compiles the styles for a component, this selector allows the styles to apply to all child components within their own encapsulated context.

Using :host ::ng-deep .test { means you are targeting descendants of the main element [_nghost-c1] as well as any elements that are children of the main element with the class "test".

The rule .test {, contained within this component, instructs to target every element in the component (compiled to [_ngcontent-c1]) with the class "test".

Since the h1 is a descendant of the main element but a direct part of the component, the latter rule is more specific and takes precedence in the CSS hierarchy.

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

Hidden body text occurs when the div element is set to be sticky at the bottom

I have been trying to resolve an issue with a sticky element at the bottom of a page using javascript/jquery. However, the sticky element is hiding the body text. I want to display all body text without it being hidden by the sticky element, but I can&apos ...

dormant HTML toggle for guests

I am looking to include an HTML switch on my webpage, but with a twist - I want to be the only one able to change its status while visitors can only view it. This is what I currently have: .onoffswitch { position: relative; width: 122px; -webkit- ...

Enhance the aesthetics of material-ui tooltips by utilizing @emotion/styled

Can material-ui tooltips be customized using the styled function from @emotion/styled? import { Tooltip } from '@material-ui/core'; import styled from '@emotion/styled'; const MyTooltip = styled(Tooltip)` // customize the tooltip ...

Using JavaScript to replace static placement

I'm looking to implement a floating navbar on my website, the kind that scrolls down with you and stays at the top of the window. However, I've run into an issue with certain Android versions (like 4.3) when using the fixed position. My navbar w ...

Is the child component in Angular re-rendered or re-initialized?

Recently started working with Angular(14) and encountered a problem. Whenever I update a property of the parent component, which is an array, the child component gets re-initialized (running the ngOnInit function). This issue arises when using the child co ...

Issue with image position shifting due to Bootstrap 4 Modal opening and closing

In the code snippet below, a bs4 modal is opened through a button when hovered over. The problem arises when the modal is closed after being opened, causing the image to move unexpectedly. This issue can be resolved by hovering over the container again. T ...

What is the correct way to start a typed Object in TypeScript/Angular?

As I delve into the world of Angular and TypeScript, I am faced with a dilemma regarding how to initialize an object before receiving data from an API request. Take for instance my model: //order.model.ts export class Order { constructor(public id: num ...

Displaying percentages with chartjs-plugin-labels in Angular 2/8 pie charts

After researching, I discovered that chartjs-plugin-labels is recommended for displaying percentages instead of count values inside or outside angles. However, I couldn't find any code examples for angular 2/8 as shown in the figure below. https://i.s ...

Overlap in Bootstrap 4 Sections

I'm facing an issue with two sections on my website. They are properly separated until a certain size, https://i.sstatic.net/e52t2.png, but when the size is reduced, they start to overlap https://i.sstatic.net/aMtHr.png. Below is a snippet of my code: ...

The successful loading of tab favicons in the DOM of an angular chrome extension is a triumph, however, explicit XHR requests are unfortunately

I've been immersed in developing a Chrome extension with Angular 5. Successfully, I managed to extract favIconUrls from the tabs API and link them to my popup.html's DOM. The icons are retrieved and displayed without any hiccups. See an example ...

What is the technique to shift the blocks to the adjoining column?

https://i.sstatic.net/6jeHV.png Will these elements flow into the adjacent column as I continue adding more of them? GS0 represents the background and block is a class defining the block. .GS0{ min-height: 90vh; display: flex; justify-content: ...

Setting up *ngFor with a freshly created array_INITIALIZER

Angular v5 In my search component, I am encountering an issue where the *ngFor directive is throwing an error because the "searchResults" array is initially empty on page load. This array is populated with data from a service returning a promise of search ...

Intercepting Bootstrap 4 modal display and conceal events using Typescript

While working on integrating a modal with the id myModal, I am attempting to connect it with events that trigger when it is shown and closed. I referred to the documentation at and implemented the following in my modal component: this.modalElement = docu ...

The image is failing to display in the CSS

As a beginner in the world of CSS and HTML, I encountered an issue where my background image is not showing up. Here's the code snippet that I have: ... <style type="text/css"> #header_contain{ width:935px; height: 135px; ...

I am looking to position two divs within a parent div so that they are aligned either to the bottom or to the left/right

I have come across this code snippet HTML <div class="head"> <div class="menu"> </div> <div class="search"> </div> </div> CSS .head { height:110px; position:relative; } .menu { ...

Challenge faced with incorporating ng2-select component into Angular 2 application

I am currently attempting to integrate the ng2-select Angular2 directive into my project from here, but I am facing challenges with its integration. In my index.html file, I have configured Systemjs as follows: System.config({ map: { ...

Child component does not detect changes in @Input array

I am working with an angular2 parent component that looks like this: ParentComponent { some_array : Array; (...) } Its child component is structured as follows: ChildComponent { selector: "child" @Input some_object : Object; } In ...

Clicking on the Primary Division

Experimenting with the onclick event on a parent div led me to realize that making the entire div clickable at 100% width is not what I intended. I only wanted the sub div to trigger a specific function. Here's an example: <div id="loginContain ...

Complications arising from IE when using strict doctype with CSS and jQuery

Here's the code I'm using for a sliding caption effect on my gallery site: I specifically implemented the last effect which is the Caption Sliding (Partially Hidden to Visible). $('.boxgrid.caption').hover(function(){ $(".cover", th ...

Text that is superimposed onto an image

Struggling to implement a hovering text over an image? I attempted to follow a tutorial, but couldn't adapt it to my project. While I managed to create a fixed overlay, I aim for a responsive solution that adjusts with resolution changes. My goal is t ...