Add Customize Css Class to the Mat-Tab-Content and other Mat-X tags which are generated runtime

See original GitHub issue

Bug, feature request, or proposal:

Not able to add customize css class or apply css styling to any Mat-X element that are generated during runtime.

What is the expected behavior?

Customize style and add user defined CSS Class to override the behaviour of element (if required)

What is the current behavior?

Not applied

What are the steps to reproduce?

This is my html body

<mat-tab-group class="container">
                   <mat-tab label="General" >
                   <div class="container">
                       <div class="row">
                           <div class="col-sm-2 col-md-4 col-lg-6">
                              This is the test 
                           </div>
                           <div class="col-sm-2 col-md-4 col-lg-6">
                               This is the test
                           </div>
                       </div>
</div>
</mat-tab>
</mat-tab-group>

Below is the Generated Html when it runs in the browser(Chrome).

<mat-tab-group _ngcontent-c12="" class="container mat-tab-group mat-primary">
	<mat-tab-header class="mat-tab-header" ng-reflect-disable-ripple="false" ng-reflect-selected-index="0">
	
		<div aria-hidden="true" class="mat-tab-header-pagination mat-tab-header-pagination-before mat-elevation-z4 mat-ripple mat-tab-header-pagination-disabled" mat-ripple="" ng-reflect-disabled="true"><div class="mat-tab-header-pagination-chevron"></div></div>
		<div class="mat-tab-label-container">
			<div class="mat-tab-list" role="tablist" style="transform: translate3d(0px, 0px, 0px);">
				<div class="mat-tab-labels">
						<!--bindings={"ng-reflect-ng-for-of": "[object Object],[object Object"}-->

						<div class="mat-tab-label mat-ripple mat-tab-label-active ng-star-inserted" mat-ripple="" mattablabelwrapper="" role="tab" ng-reflect-disabled="false" id="mat-tab-label-0-0" tabindex="0" aria-controls="mat-tab-content-0-0" aria-selected="true">

							<!--bindings={}--><!--bindings={"ng-reflect-ng-if": "true"}-->
							General
						</div> 
				</div>
				<mat-ink-bar class="mat-ink-bar" style="visibility: visible; left: 0px; width: 160px;"></mat-ink-bar>
			</div>
		</div>
		
		<div aria-hidden="true" class="mat-tab-header-pagination mat-tab-header-pagination-after mat-elevation-z4 mat-ripple mat-tab-header-pagination-disabled" mat-ripple="" ng-reflect-disabled="true"><div class="mat-tab-header-pagination-chevron"></div></div>
	</mat-tab-header>
<div class="mat-tab-body-wrapper">
	<!--bindings={"ng-reflect-ng-for-of": "[object Object],[object Object"}-->
	<mat-tab-body class="mat-tab-body ng-tns-c23-28 mat-tab-body-active ng-star-inserted" role="tabpanel" ng-reflect-_content="[object Object]" ng-reflect-position="0" id="mat-tab-content-0-0" aria-labelledby="mat-tab-label-0-0" ng-reflect-origin="0">
	
		<div class="mat-tab-body-content ng-trigger ng-trigger-translateTab" style="transform: none;"><!---->
			<div _ngcontent-c12="" class="container ng-star-inserted" style="">
				<div _ngcontent-c12="" class="row">
					<div _ngcontent-c12="" class="col-sm-2 col-md-4 col-lg-6">
						This is the test
					</div>
					<div _ngcontent-c12="" class="col-sm-2 col-md-4 col-lg-6">
						This is the test
					</div>
				</div>
			</div>
	<!----></div>
	</mat-tab-body> 
</div>

</mat-tab-group>.

REQUEST. Want to add “MyTabClass” to the “div.mat-tab-body-content”. I tried adding css style using !important, but still fails to apply custom styling or custom css class to the tags which are generated at runtime.

Please anybody aware about how to implement this guide me. I am new to the Material Design with Angular 2

What is the use-case or motivation for changing an existing behavior?

To have a upperhand in the styling, when required. Usually we are using the default behaviour of material controls.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Latest version of all. OS- windows 10

Is there anything else we should know?

Yes, this is not specifically related to the Mat-tabs. I was searching the way to apply custom css to any of the mat-X controls sub-elements which are auto-generated during rendering in browser

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

18reactions
willshowellcommented, Jan 8, 2018

You are likely just encapsulating your styles. Take a look at this guide and see if it helps:

https://material.angular.io/guide/customizing-component-styles

14reactions
benawhitecommented, Jul 19, 2018

@willshowell If a class was applied to the mat-tab, I would expect the generated html to apply the class to both the label and the body. This would allow the dev to manage both with the addition of the appropriate selector. (.mat-tab-label.first or .mat-tab-body.first)

<mat-tab-group>
   <mat-tab label="First" class="first">First</mat-tab>
   <mat-tab label="Second" class="second">Second</mat-tab>
</mat-tab-group>

Generated HTML is like this

<mat-tab-group>
   <mat-tab-header>
      ...
      <div class="mat-tab-label first">...</div>
      <div class="mat-tab-label second">...</div>
      ...
  </mat-tab-header>
   <div class="mat-tab-body-wrapper">
      <mat-tab-body class="mat-tab-body first">
        <div class="mat-tab-body-content">First</div>
      </div>
      <mat-tab-body class="mat-tab-body second">
        <div class="mat-tab-body-content">Second</div>
      </div>
   </div>
</mat-tab-group>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I use CSS to style Angular Material tabs?
To add a custom class to your material tabs, you have to use the ng-template syntax: <mat-tab-group> <mat-tab> <ng-template mat-tab-label> ...
Read more >
Tabs - Angular Material
Material design tab-group component. Supports basic tab pairs (label + content) and includes animated ink-bar, keyboard navigation, and screen reader.
Read more >
Create a dynamic tab component with Angular | juri.dev
Whenever we click the “Add new person” or “edit” button beneath an ... tab we want to display, Angular gives us the <ng-template>...
Read more >
Dynamic Tab based application using Angular and Angular ...
Angular Material · Whenever there is a change in the length of the tabs array that information is shared with the components via...
Read more >
Angular Mat Custom Label - StackBlitz
<ng-template mat-tab-label>Second</. ng-template>. Content 2. </ng-template>. </mat-tab>. <mat-tab>. <ng-template matTabContent>.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found