Am following this tutorial on transclusion https://scotch.io/tutorials/angular-2-transclusion-using-ng-content
However there is no mention on how to style elements that end up replacing ng-content.
It seems that I can only target those element in the css if preceeded by the /deep/
keyword, which is normally used when targeting a nested component. Is this correct?
Use the :host /deep/ selector. If you want to use the shadow DOM polyfill and style your components using the style or styleUrls attribute of the Component decorator, select the element with :host , then ignore the shadow DOM polyfill with the /deep/ child selector. :host will select the element.
There will be no ng-content tag in real DOM, we cannot set a CSS class on it or bind Angular directive. This is just a placeholder for our content. The content cannot be rerendered from the inside.
ng-content example | multiple projections Using the select attribute, you can specify <ng-content> based on different selectors. In this example, we specify an <ng-content> for h1 tags and another for elements with the phone class. Also notice how we include a third <ng-content> without a select attribute.
Both APIs ::ng-deep and /deep/ are deprecated and will eventually be removed in the future.
Content inside <ng-content>
is insulated from the component. It can't see the component's attributes or styling.
If you do need to style it, and sometimes you will, you have two options:
You can create a regular CSS file and style the content like that. You are almost certainly using the shadow DOM polyfill. Regular CSS will see through the polyfill and just style the element. Say you have an app-sidebar. You could write:
app-sidebar p { color:red; }
If you are using ng-cli, any rules you write in style.scss will be global.
If you want to use the shadow DOM polyfill and style your components using the style
or styleUrls
attribute of the Component
decorator, select the element with :host
, then ignore the shadow DOM polyfill with the /deep/
child selector.
:host
will select the element./deep/
will select elements without adding the mock shadow DOM attribute selector to the nested selectors. Put them together and you can select all elements nested inside the host component element, regardless of where they are declared.
like so:
:host /deep/ p { color:red; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With