2

Is there any contraindications on stylizing Angular2 custom element directly and selecting them with CSS selector ?

Example :

// HTML
<My-Page>
    <header></header>
    <main></main>
    <My-Footer class="sticky-footer"></My-Footer>
</My-Page>

// CSS 
My-Page {
    background-color: grey;
}

header {
    ...
}

.sticky-footer {
    position: absolute;
}

Good or bad practice ?

2 Answers 2

2

While this is perfectly valid it breaks modularity. A component can style its own root-element:

my-page.component.css

:host{
  background-color: grey;
}

header {
    ...
}

.sticky-footer {
    position: absolute;
}

This will achieve the same thing and contains CSS that's vital to your MyPageComponent in the component.

Sign up to request clarification or add additional context in comments.

1 Comment

ealier in RC4 :host child-component{ ..} used to work.. this is no longer working in stable release .. So i had to move the child-component{..} styles set by parent into child component as :host{...}.. Hope this helps someone..
0

You should use piercing CSS combinators >>>, /deep/ and ::shadow

styles: [
    `
     :host { background-color: grey; }

     :host >>> header{
       background:yellow;
     }

     :host >>> My-Footer{  
       position: absolute;
     }         
    `
],
template:
`
<My-Page>  //<----no need as this becomes your :host

    <header></header>
    <main></main>
    <My-Footer class="sticky-footer"></My-Footer>

</My-Page> //<----no need
`

2 Comments

Be aware that >>> and /deep/ (same thing) only work with angular's emulated Shadow DOM. It will break when you switch to native Shadow DOM. ::shadow and /deep/ are deprecated.
What if I want these styles being placed in my global css file (styles.css) and not in the component styles ? Example, I've got some issues due to the root custom tag <app> in index.html. I basically need to set height: 100% on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.