Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color property of mat-slide-toggle/ overwrite CSS of component?

Tags:

Is there any way to change color property of mat-slide-toggle component from the Angular Material component library? How to override its styling?

Or can anyone suggest me any other slide toggle for angular 5 applications, like material slide toggle?

like image 684
Amith Avatar asked Jun 13 '18 13:06

Amith


People also ask

How to change the color of mat slide toggle?

The color of a <mat-slide-toggle> can be changed by using the color property. By default, slide-toggles use the theme's accent color. This can be changed to 'primary' or 'warn' .

What is toggle in angular?

The <mat-button-toggle>, an Angular Directive, is used to create a toggle or on/off button with material styling and animations. mat-button-toggle buttons can be configured to behave as radio buttons or checkboxes.

How do you find the value of the mat slide toggle?

Slide toggle gives either true or false value. Slide toggle value can be changed either by drag or toggle. Here on this page we will create slide toggles with its properties and will fetch its values using formControl , formControlName and ngModel with reactive form and template-driven form.


2 Answers

You can change the primary colour of the mat-slide-toggle with the below css in your component styles.

/deep/ .mat-slide-toggle.mat-checked:not(.mat-disabled) .mat-slide-toggle-bar {     background-color: #49c5b6; }  /deep/ .mat-slide-toggle.mat-checked:not(.mat-disabled) .mat-slide-toggle-thumb {     background-color: #49c5b6; } 

The above code is tested on angular 5+ version and which is working.

Component styles normally apply only to the HTML in the component's own template.

Use the /deep/ shadow-piercing descendant combinator to force a style down through the child component tree into all the child component views. The /deep/ combinator works to any depth of nested components, and it applies to both the view children and content children of the component.

You may please take time to read more about the deep selectors here.

https://angular.io/guide/component-styles#deep

like image 146
Krishnadas PC Avatar answered Sep 19 '22 10:09

Krishnadas PC


By default, mat-slide-toggle uses the theme's accent color.So to change the color you can try this instead:

<mat-slide-toggle color="primary">Slide Toggle!</mat-slide-toggle> 

You can change color from primary, accent, warn..etc

like image 32
Sai Vamsi Avatar answered Sep 21 '22 10:09

Sai Vamsi