Angular Material customize tab

后端 未结 5 1584
天涯浪人
天涯浪人 2020-12-08 07:04

I\'m using angular 4 and I\'m using Angular Material.


    

        
相关标签:
5条回答
  • 2020-12-08 07:39

    Update

    To customize tab background and ink bar, you can define your own theme then use the theme options on the tab group:

    <div class="my-theme">
        <mat-tab-group [backgroundColor]="'primary'" [color]="'warn'">
            <mat-tab label="First"> Content 1 </mat-tab>
            <mat-tab label="Second"> Content 2 </mat-tab>
            <mat-tab label="Third"> Content 3 </mat-tab>
        </mat-tab-group>
    </div>
    

    Here is an example on StackBlitz.

    Old answer with ::ng-deep

    If you don't want to touch ViewEncapsulation, use ::ng-deep instead with class selector (inspect by browser dev tool).

    For example (Angular 5, Material 2):

    /* active tab */
    ::ng-deep .mat-tab-list .mat-tab-labels .mat-tab-label-active {
    color:red;
    background-color: green;
    }
    
    /* ink bar */
    ::ng-deep .mat-ink-bar {
    background-color: var(--primary-color,#1F89CE) !important;
    }
    
    0 讨论(0)
  • 2020-12-08 07:41

    Angular version 6

    New Design for custom tab

    html

    <mat-tab-group [disableRipple]="true" mat-align-tabs="center">
         <md-tab label="Tab 1"></md-tab>
         <md-tab label="Tab 2"></md-tab>
    </md-tab-group>
    

    scss

    /deep/ .mat-tab-labels {
      min-width: auto !important;
      div {
        border: 1px solid #fff;
        background-color: #404040;
          &.mat-tab-label-active {
            background-color: #3aafa9;
            .mat-tab-label-content {
              border: none;
              background-color: #3aafa9;
              }
          }
          .mat-tab-label-content {
            border: none;
            color: #fff !important;
            }
        }
     }
    
     /deep/ .mat-tab-group{
      &.mat-primary {
        .mat-ink-bar {
          background-color: transparent;
        }
      }
     }
    
     /deep/ .mat-tab-body-wrapper{
       min-height: 550px
     }
    
     /deep/ .mat-tab-label-container {
       flex-grow: 0 !important;
     }
    
    /deep/ .mat-tab-label{
      opacity: 1 !important;
     }
    
    0 讨论(0)
  • 2020-12-08 07:42

    In your component, set ViewEncapsulation to None and add the styles in your component.css file.

    Changes in Typescript code:

    import {Component, ViewEncapsulation} from '@angular/core';
    
    @Component({
      ....
      encapsulation: ViewEncapsulation.None
    })
    

    Component CSS:

    /* Styles for tab labels */
    .mat-tab-label {
        min-width: 25px !important;
        padding: 5px;
        background-color: transparent;
        color: red;
        font-weight: 700;
    }
    
    /* Styles for the active tab label */
    .mat-tab-label.mat-tab-label-active {
        min-width: 25px !important;
        padding: 5px;
        background-color: transparent;
        color: red;
        font-weight: 700;
    }
    
    /* Styles for the ink bar */
    .mat-ink-bar {
        background-color: green;
    }
    

    Demo

    0 讨论(0)
  • 2020-12-08 07:45

    Latest Solution:-

    1)Override in styles.css 2) Use selector of component of where that material-tab exists

    styles.css

      app-child .mat-tab-label.mat-tab-label-active {
        padding: 0px 15px ;
      justify-content: flex-start;
      }
    
      app-child .mat-tab-label{
        padding: 0px 15px ;
      justify-content: flex-start;
      }
    
    .mat-tab-group.mat-primary .mat-ink-bar, .mat-tab-nav-bar.mat-primary .mat-ink-bar{
        background:#6168e7;
      }
    
    0 讨论(0)
  • 2020-12-08 07:45

    It took me a long time to figure out how to change the background color of tabs, by overriding material design. I don't know where to share my end result, so here it goes:

    For the .ts file:

    @Component({
      selector: 'app-tabs',
      templateUrl: './tabs.component.html',
      styleUrls: ['./tabs.component.css'],
      encapsulation: ViewEncapsulation.None
    })
    

    For the css file:

    .mat-tab-labels, .mat-tab-label, .mat-tab-link {
      color: white;
      font-size: 16px;
      background-color: #804A71;
    }
    
    .mat-tab-group.mat-primary .mat-ink-bar, .mat-tab-nav-bar.mat-primary .mat-ink-bar{
      background: white;
      height: 5px; 
    }
    

    So this is both the backgroundcolor, the color and size of the text (label), and the tabs bar under the text. It finally worked when I used both .mat-tab-labels and .mat-tab-label.

    0 讨论(0)
提交回复
热议问题