Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Style rule working only in the styles.css and not the component css

I’m working on an Angular 12 project with PrimeNG. I have a component on which I want to apply a rule, for exemple :

.p-dropdown-panel {
  width:515px;
}

Of course I only want to apply this rule to my component and not to every component, but when I put it in the component css, which is specified in the component.ts (cf bellow), it’s not applying whereas if I put it in the styles.css it does apply. Why is that ?

It looks like this problem only occurs with classes provided by PrimeNG.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@Component({
  selector: 'test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})

A bit more details :

  • I can see the rule in the head of the html in a style div, which does list the content of the test.component.css :

.btn_left[_ngcontent-njo-c121]{float:left}.btn_right[_ngcontent-njo-c121]{float:right}.p-dropdown-panel[_ngcontent-njo-c121]{width:515px}

  • I do not see it in the list of rules when I select the .p-dropdown-panel div with the dev tools, only list the following rules :

    .p-dropdown-panel {
    position: absolute;
    top: 0;
    left: 0;
    }

    .p-dropdown-panel {
    background: #fff;
    color: #333;
    border: 1px solid #c8c8c8;
    border-radius: 3px;
    box-shadow: 0 3px 6px 0 rgb(0 0 0 / 16%);
    }

I do import the dropdown module inside my component :

import { DropdownModule } from 'primeng/dropdown';

>Solution :

I am not familiar with PrimeNG however I would guess that this class is applied to one of their element inside their components.

And that’s exactly the problem. Component-Styles are usually only applied inside that component. That means neither parent nor child components are (generally) affected by any styles inside them. That’s called View encapsulation

You can do it by using the ::ng-deep pseudo-class however that is usually considered bad practice and deprecated. For more info see here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading