Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material Tab add image to button

I'm beginner for the Angular. I try to do something with Angular material tab . I added the tab section to image but that part is not working. Does anyone know how to add a image to the angular material tab. Please have a look at my attached image to understand what I try to achieve.

Need like this

My code so far (stackblitz demo)

My code part:

<mat-tab-group mat-stretch-tabs class="example-stretched-tabs mat-elevation-z4">
  <mat-tab label="First"  style="background:url('/assets/img/school-01.png')"> Content 1 </mat-tab>
  <mat-tab label="Second"> Content 2 </mat-tab>
  <mat-tab label="Third"> Content 3 </mat-tab>
</mat-tab-group>

css

.example-stretched-tabs {
  max-width: 800px;
}
/* active tab */
.mat-tab-list .mat-tab-labels .mat-tab-label-active {
  color:#0f2241;
  background-color: #535353;
  opacity: 1;
}

/* ink bar */
.mat-tab-group.mat-primary .mat-ink-bar {
  background: none;
  content: url('https://image.flaticon.com/icons/svg/60/60995.svg');
  height: 10px; 
}

.ts

import {Component, ViewEncapsulation} from '@angular/core';

/**
 * @title Tab group with stretched labels
 */
@Component({
  selector: 'tab-group-stretched-example',
  templateUrl: 'tab-group-stretched-example.html',
  styleUrls: ['tab-group-stretched-example.css'],
  encapsulation: ViewEncapsulation.None
})
export class TabGroupStretchedExample {}
like image 258
core114 Avatar asked Sep 14 '18 05:09

core114


People also ask

How do I add an image to a mat Tab label?

According to the Angular docs: For more complex labels, add a template with the mat-tab-label directive inside the mat-tab. So you can place your <img> or whatever you want into the template <ng-template mat-tab-label/> .

How do I stop mat tab changing?

How to stop animation in mat tab? Controlling the tab animation If you want to disable the animation completely, you can do so by setting the properties to 0ms .

What is Mat tab in angular?

Tabs and navigation While <mat-tab-group> is used to switch between views within a single route, <nav mat-tab-nav-bar> provides a tab-like UI for navigating between routes.


1 Answers

According to the Angular docs:

For more complex labels, add a template with the mat-tab-label directive inside the mat-tab.

So you can place your <img> or whatever you want into the template <ng-template mat-tab-label/>.

<mat-tab-group>
    <mat-tab>
        <ng-template mat-tab-label>
            <img src="...">
            <span>foo</span>
        </ng-template>
    </mat-tab>
    <mat-tab>
        ....
    </mat-tab>
</mat-tab-group>
like image 66
L. Guthardt Avatar answered Sep 20 '22 20:09

L. Guthardt