Repository navigation
Description
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>
.
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
Activity
willshowell commentedon 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
ovoronin commentedon Mar 21, 2018
Why is this closed? <mat-tab> classes are not applied anywhere
willshowell commentedon Mar 21, 2018
@ovoronin if you place the styles in a global stylesheet or remove view encapsulation from your component styles, they'll be applied.
ovoronin commentedon Mar 22, 2018
@willshowell
For example I have
Generated HTML is like this
As you can see there are no CSS classes 'first' and 'second' anywhere
It will be good to have them applied to corresponding mat-tab-body elements
willshowell commentedon Mar 22, 2018
I see. What DOM element would you expect to receive the class?
You could do this if you just wanted to style the body:
ovoronin commentedon Mar 22, 2018
@willshowell
I was trying to style mat-tab-body elements to fix some scrolling stuff
borsini commentedon Mar 23, 2018
@ovoronin I also wanted to style
mat-tabelements, making them take all the available height.I fixed them with flexbox using
mat-tab-group { flex: 1; }in the component stylesheet and.mat-tab-body-wrapper { flex: 1; }in the global stylesheet.Quick and dirty. It works but I'm quite sure it is not the best option 😟
karthickDhanapal commentedon Mar 29, 2018
How to disable the animation that have been trigged when tab clicked ngtrigger translate-tab css
hlgrondijs commentedon May 25, 2018
I have the same type of issue, where angular generates a wrapping
<div> element inside the of the<mat-tab>tag in the generated code. In my case this<div>hasdisplay: flexset, where I want it to bedisplay: blockMy intuition tells me this is not the way we should be styling these elements. These generated components should never have to be touched if Angular is/was designed well. However if I change the display to block in DevTools as said above, I get the desired design and with flex I don't so, yeah... if it's possible, would like to know how :D
shootdaj commentedon May 29, 2018
@willshowell What if placing them in the global styles is not possible? I have two types of tabs in my mat-tab-group and need to apply different styles to them. How would I accomplish this without applying classes to the specific mat-tabs?
willshowell commentedon May 29, 2018
@shootdaj I think you might be limited to a workaround like some manual class manipulation.
@andrewseguin what do you think? would it be possible to allow classes be applied to
mat-tab?benawhite commentedon 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)
Generated HTML is like this
Hypenate commentedon Nov 21, 2018
@willshowell Any news on this? It would be great to have some flexibility over the control.
I tried to style a specific tab, but the class/style is thrown away when the tabs are generated in the browser.
EnricoSaunders commentedon Dec 13, 2018
You can use ng-template:
simeyla commentedon Feb 23, 2019
@shootdaj I found by accident once that you can add global styles with
::ng-deepJust use
::ng-deeplike this in ANY component - not nested under anything, and it will be propagated to root styles (assuming the defining component has been displayed) :You can do this to avoid having to put things in root styles - perhaps you don't have access to it, or you're writing a debugging control. Also this approach may not work forever, so you'll have to make your own judgement based on that.
I wish they would just officially support
:rootguibot17 commentedon Mar 10, 2019
As of today, the best approach I've found is just using inline styling:
<div style="display: block; color: blue;"><div>content</div></div>It was great for me. I hope it helps you all.
nazarov-dmitry commentedon Mar 27, 2019
we can use CSS :nth-child() Selector. Thank you
JasonKoolman commentedon Apr 13, 2019
A clean solution would be to:
<mat-tab-group>element.You can now style both
.mat-tab-bodyand.mat-tab-body-wrapper.In some cases you don't want to globally define all component styles, which happens when you disable view encapsulation. To prevent this, simply create a component that's dedicated to display (and style) tab groups.
anysunflower commentedon Jul 15, 2019
I have to hack the css of
mat-tabbecause when I usepopoverin mat-tabthe pop-over will hide by the
overflowset inmat-tab-body-wrapperand.mat-tab-body.mat-tab-body-activeI use
:host ::ng-deepto hack the mat-tab styleangular-automatic-lock-bot commentedon Sep 11, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.