How to style ng-content
update
::slotted
is now supported by all new browsers and can be used with ViewEncapsulation.ShadowDom
https://developer.mozilla.org/en-US/docs/Web/CSS/::slotted
original
::content xxx { ... }
might work as well or :host xxx { ... }
. The shimming is not very strict or accurate. AFAIK (>>>
/deep/
old) ::ng-deep
(supported by SASS) and a space do currently the same.
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:
1. Just write CSS
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.
2. 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./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;
}
This solved my issue
::ng-deep {
& > * {
// styles here
}
}
As an alternative to the above answers, you can create a directive to be applied to the content, and use the directive to add the styling for the content.