The way I see it, it could be used as a replacement for <div>
containers for styling a component. Example:
@Component({
template: `
<div class="container">
<h1>Some Title</h1>
<p>Some Content</h1>
</div>
`,
styles: [`
.container {
border: 1px solid black;
display: flex;
}
`],
})
@Component({
template: `
<h1>Some Title</h1>
<p>Some Content</h1>
`,
styles: [`
:host {
border: 1px solid black;
display: flex;
}
`],
})
If I understand correctly, these two solve the exact same problem. And it's nice to have one less element and class to worry about in pretty much every single component.
Questions: Is this what :host
is intended for? If not, what is the point of it, and what are the best practices for giving style to your component aside from adding containers everywhere?
They don't solve the same problem. :host
is for styling the host and the .container
is for styling any div that has the container
class inside your component.
The .container
div will not be available for styling outside of your component due to encapsulation while the host element can be styled.
Note that the initial value of the display
property is inline
, so by default your component will be displayed as an inline
element. Maybe you don't want that, especially if you have a block element inside of it which might seem counter-intuitive (even if it's allowed).
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