Sheldon
Sheldon

Reputation: 1029

Primeng - how to use styleClass?

I want to use the styleClass property of the Togglebutton component. As described in another post, I thought it is straight forward by using:

styleClass="test"

In my css-file I then set some attributes, like

.test { background: red; }

But this does not work. Working with style is perfectly clear by using [style]="{'background':'red'}" No problem with that. But styleClass does not work. Here is the component. Any idea how to use styleClass?

Upvotes: 33

Views: 57391

Answers (4)

PeterS
PeterS

Reputation: 190

If you want to keep the view encapsulation on, another option is to use :host together with ::ng-deep

:host ::ng-deep .test {
    background: red;
}

Note that ::ng-deep might be removed in the future. See this SO question.

Upvotes: 2

Gowtham Gowda
Gowtham Gowda

Reputation: 2036

Example for p-dropdown

.p-dropdown {
  &.add-attr-dropdown {
    border: 0px;
  }
}

Here add-attr-dropdown is your custom class name

Upvotes: 0

Cassius Horvath
Cassius Horvath

Reputation: 119

You can try to override o PrimeNg component doing this, eg. I was using tabMenu and worked:

<p-tabMenu styleClass="override-this"></p-tabMenu>

I suppose it will work with any PrimeNg component that accepts styleClass.

In root style.css file you just use body and then the styleClass name.

body .override-this{
  font-size: 10px;
}

I didn't test with other properties, probably if you need to change some deeper you will need to cascade like

body .override-this .ui-tabmenu .ui-tabmenu-nav and so on.

Upvotes: 2

Sheldon
Sheldon

Reputation: 1029

To make things clear: the styleClass property is only an addition to the original classes of the component. That means, you always have to use the original classes in order to style the component. With styleClass you then have the possibility to address one or more components of a set of components of the same type. So, having five Togglebutton components, you can generally style those components with

.ui-togglebutton.ui-button.ui-state-active{}

If you then want to style one of those components differently, you can add the styleClass property:

<p-toggleButton styleClass="different"></p-toggleButton>

And in your css you can now address this one by:

.different.ui-togglebutton.ui-button.ui-state-active{}

Thus styleClass is not a replacement for the original selectors, it is an addition.

Upvotes: 33

Related Questions