Angular - Material: Progressbar custom color?

后端 未结 9 2422
生来不讨喜
生来不讨喜 2020-12-14 09:09

I am now trying for hours. I use Material2 and simply want to change the color of the progress-bar. I know there are those themes (primary/accent/warn) but I want to have a

相关标签:
9条回答
  • 2020-12-14 10:08

    Since nobody mentioned so far...

    He's how I solved it.

    @Meet Dave is right about his approach. But you should use encapsulation: ViewEncapsulation.None (disables css modules)

    Something like this:

    Component

    @Component({
      selector: '...',
      templateUrl: '...',
      styleUrls: ['...'],
      encapsulation: ViewEncapsulation.None,
    })
    

    Sass (in my case)

    .audio-progress-bar {
      &.mat-progress-bar {
        height: 10px;
      }
    
      .mat-progress-bar-fill::after {
        background-color: #37474f;
      }
    
      .mat-progress-bar-buffer {
        background-color: #90a4ae;
      }
    
      /* remove animation and the dots*/ 
      .mat-progress-bar-background {
        animation: none;
        background-color: #eceff1;
        fill: #eceff1;
      }
    }
    

    View

    <mat-progress-bar
      class="audio-progress-bar"
      mode="buffer"
    ></mat-progress-bar>
    
    0 讨论(0)
  • 2020-12-14 10:10

    You can add a custom class and add styles to the same in styles.scss (with !important).

    .your-custom-class{
      background-color: colorname !important;
    }
    

    or you can use the existing class to override the defined css properties by adding them to your global styles.scss file.

    .mat-progress-bar-fill::after{
      background-color: colorname;
    }
    .mat-progress-bar-buffer {
      background: colorname;
    }
    
    0 讨论(0)
  • 2020-12-14 10:11

    Update:

    Avoid using deep, TL;DR: Deep is technically invalid (like, deeprecated :p)

    For more info refer: The use of /deep/ and >>> in Angular 2

    Now, to change the color of mat-progress bar, Here is how I got it working,

    Head over to your styles.scss file (or the main css/scss file in your project)

    Add this class -->

    .green-progress .mat-progress-bar-fill::after {
        background-color: green !important;
    }
    

    Your mat-progress should use the above class, like -->

    <mat-progress-bar class="green-progress" mode="indeterminate"></mat-progress-bar>
    
    0 讨论(0)
提交回复
热议问题