简体   繁体   English

如何设置 Angular 6 mat-tabs 的样式?

[英]How to style Angular 6 mat-tabs?

I have been trying to apply different styles to my mat-tab rows.我一直在尝试对我的mat-tab行应用不同的样式。

I have a web page with has 2 on, one is a main menu (Home, About Us etc) and the second one is an image one using mat-icon .我有一个网页,有 2 个,一个是主菜单(主页,关于我们等),第二个是使用mat-icon的图像。

I want to apply different styles to them but nothing works whatsoever.我想对它们应用不同的样式,但没有任何效果。

I have tried adding separate CSS to each components CSS files but it doesn't apply it unless its in the main (route) style.css file which then applies the style set to both tabs.我尝试向每个组件的 CSS 文件添加单独的 CSS,但它不会应用它,除非它在主(路由) style.css文件中,然后将样式集应用于两个选项卡。

I have tried:我试过了:

HTML for 1st tab row (uses main CSS file)第一个标签行的 HTML(使用主 CSS 文件)

<mat-tab-group class="mainContent">
    <mat-tab label="Personal details">
        <app-clientpersonalinfo></app-clientpersonalinfo>
    </mat-tab> 
    <mat-tab label="Product details">
        <app-comingsoon></app-comingsoon>
    </mat-tab>
</mat-tab-group>

HTML for 2nd tab row (tried adding inline styling)第二个标签行的 HTML(尝试添加内联样式)

<mat-tab-group style="color: red !important">
  <mat-tab label="Personal details" style="color: red !important">
    <ng-template mat-tab-label style="color: red !important">
      <mat-icon title="Investor's personal details" style="color: red !important">person</mat-icon>
    </ng-template>
  </mat-tab> 
  <mat-tab label="Notes">
      <ng-template mat-tab-label>
          <mat-icon title="Investor notes">receipt</mat-icon>
        </ng-template>      
  </mat-tab>
</mat-tab-group>

CSS file for first component with tab group带有选项卡组的第一个组件的 CSS 文件

/* Main Tab */
.mat-tab-header {
    border-bottom: 2px solid green;
    height: 46px;
}

.mat-tab-label:not(.mat-tab-disabled):focus,
.mat-tab-link:not(.mat-tab-disabled):focus,
.mat-tab-group.mat-primary .mat-tab-label-active {
    border-top: 2px solid green;
    border-left: 2px solid green;
    border-right: 2px solid green;
    background-color: red;
}

    /* ink bar style */
    .mat-tab-group.mat-primary .mat-ink-bar, .mat-tab-nav-bar.mat-primary .mat-ink-bar{
        background: transparent;
    }
/* ---  --- */

The first tab group does not have green or red as specified第一个选项卡组没有指定的绿色或红色

CSS File For Second Component With Tab Group带有选项卡组的第二个组件的 CSS 文件

/* Main Tab */
.mat-tab-header {
    border-bottom: 2px solid blue;
    height: 46px;
}

.mat-tab-label:not(.mat-tab-disabled):focus,
.mat-tab-link:not(.mat-tab-disabled):focus,
.mat-tab-group.mat-primary .mat-tab-label-active {
    border-top: 2px solid blue;
    border-left: 2px solid blue;
    border-right: 2px solid blue;
    background-color: yellow;
}

    /* ink bar style */
    .mat-tab-group.mat-primary .mat-ink-bar, .mat-tab-nav-bar.mat-primary .mat-ink-bar{
        background: transparent;
    }
/* ---  --- */

The second tab group does not have blue or yellow as specified only what's in the main CSS file.第二个选项卡组没有蓝色或黄色,仅指定主 CSS 文件中的内容。

I also tried adding the horrible !important to all CSS rows but again it doesn't work at all:我还尝试将可怕的!important添加到所有 CSS 行,但它再次不起作用:

Main CSS主 CSS

/* Main Tab */
.mat-tab-header {
    border-bottom: 2px solid #86a6c8;
    height: 46px;
}

.mat-tab-label:not(.mat-tab-disabled):focus,
.mat-tab-link:not(.mat-tab-disabled):focus,
.mat-tab-group.mat-primary .mat-tab-label-active {
    border-top: 2px solid #86a6c8;
    border-left: 2px solid #86a6c8;
    border-right: 2px solid #86a6c8;
    background-color: #cedbe9;
}

    /* ink bar style */
    .mat-tab-group.mat-primary .mat-ink-bar, .mat-tab-nav-bar.mat-primary .mat-ink-bar{
        background: transparent;
    }
/* ---  --- */

Folder structure文件夹结构

src
|
|_components
  |
  |_menus
    |
    |_mainmenu
    |
    |_mainmenu.component.css
    |_mainmenu.component.html
    |_mainmenu.component.spec.ts
    |_mainmenu.component.ts
  |
  |_submenu
    |
    |_submenu.component.css
    |_submenu.component.html
    |_submenu.component.spec.ts
    |_submenu.component.ts
|
|_styles.css

Please donot panic, as it seems from yur first message line you have worries about angular and its tabs.请不要惊慌,因为从您的第一条消息行看来,您担心 angular 及其选项卡。 As far as i can understand what you have written, I THINK THERE IS SIMPLE solution that we can work out and make the tabs great.就我所理解的你所写的内容而言,我认为有一个简单的解决方案,我们可以解决这个问题并使标签变得更好。

right, first things first, a disclaimr, what i am saying will be pure css and script, we will not ebe touching angular which actually is agood thing as in any case the tabs are a material thing.对,首先,免责声明,我所说的将是纯 css 和脚本,我们不会接触 angular,这实际上是一件好事,因为在任何情况下,标签都是重要的东西。

  1. we must clear all of your css files.我们必须清除所有的 css 文件。 i would advise clearing styles.scss and then delting the _mainmenu.component.css and _sub menu.component.css.我建议清除styles.scss,然后删除_mainmenu.component.css 和_sub menu.component.css。 (ALERT-1 QUESTION, WHY DO YOU HAVE _UNDERSCORE, THIS IS JAVA THING WE ARE SAYING CSS IT WOULD JUST BE mainmenu.component.css). (ALERT-1 问题,为什么你有 _UNDERSCORE,这是 JAVA 的东西,我们说 CSS 它只是 mainmenu.component.css)。
  2. with the css files deleterd, we can now add the css we need.使用 css 文件 deleterd,我们现在可以添加我们需要的 css。 so in styles css, add the following:所以在样式 css 中,添加以下内容:

     angular.main.component.a { width: 150px; height: 80px; background-color: red; -ms-transform: rotate(20deg); /* IE 9 */ -webkit-transform: rotate(20deg); /* Safari 3-8 */ transform: rotate(20deg); } angular.sub.main.component.b { width: 150px; height: 80px; background-color: yellow; -ms-transform: skewY(20deg); /* IE 9 */ -webkit-transform: skewY(20deg); /* Safari 3-8 */ transform: skewY(20deg); } div.c { width: 150px; height: 80px; background-color: yellow; -ms-transform: scaleY(1.5); /* IE 9 */ -webkit-transform: scaleY(1.5); /* Safari 3-8 */ transform: scaleY(1.5); }

DO NOT PANIC IF this makes the tabs rotate at a strange angle.如果这会使标签以奇怪的角度旋转,请不要惊慌。 simply refresh the page as it will be straight again and in the rows.只需刷新页面,因为它将再次直行并在行中。

I hope this was of helps, please post comments if i can do more helps.我希望这有帮助,如果我能做更多的帮助,请发表评论。

Thank you谢谢

Hello and may I say I feel your plight!你好,我可以说我感受到了你的困境! Our teams of threes have just experienced the exact same issues!我们的三人组刚刚遇到了完全相同的问题!

The ways we went about wit was via the following:我们的智慧是通过以下方式进行的:

Component 1组件 1

Say we have a component.假设我们有一个组件。 Let's call him Billy.我们就叫他比利吧。 Billy was a good little component and wrapped his good heart around another component name Timmy. Billy 是一个很好的小组件,他的好心围绕着另一个组件名称 Timmy。

Component 2组件 2

So if we have tabs.所以如果我们有标签。 Imagine them like so:想象他们是这样的:

Billy -> Timmy -> Tabs.比利 -> 蒂米 -> 标签。

Got it?知道了?

Good!好的!

Where were we?我们刚刚说到哪了? Righttttt, now Billy and Timmy want to be there own peoples. Righttttt,现在比利和蒂米想要成为自己的人。 They want different haircuts and different T-Shirts and the like but they're currently being dressed by the same person.他们想要不同的发型和不同的 T 恤等等,但他们目前由同一个人穿着。 Let's call this main.css .我们称之为main.css

Solution!解决方案!

create abilly.css and timmy.css file

and let main.css be it's own thing.main.css成为它自己的东西。

Then Component 1 inherits the timmy file.然后组件 1继承了 timmy 文件。 And Billy is happy.比利很高兴。

Make sense now but it took our team of threes so manys days to figure this out.现在说得通,但我们的三人小组花了很多天才弄明白这一点。

So glad I could help.很高兴能帮上忙。

You can style default material classes by using the ::ng-deep pseudo element.您可以使用 ::ng-deep 伪元素设置默认材质类的样式。

:host ::ng-deep .mat-tab-header {
    border-width: 9px;
    border-style: solid;
    border-color: orange;
  }

The :host is optional, that scopes the styles to the tabs in the current component. :host 是可选的,它将样式范围限定为当前组件中的选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM