material2:适当改变 mat-mini-fab 尺寸和图标尺寸

material2: properly changing of mat-mini-fab size and icon size

抱歉,如果我重复了一个问题 - 没有找到关于我的问题的任何信息。

我正在使用 angular 6 with material design。我需要更改按钮 mat-fab-mini 的大小和其中的图标。在 inspect 工具 html 中以这样的方式呈现:

为了改变大小,我使用了以下代码,但它并没有使按钮内的图标变小:

html

<button mat-mini-fab class="my-fab">
    <mat-icon aria-label="favorite">favorite</mat-icon>
</button>

scss

.my-fab {
    width: 20px;
    height: 20px;
    line-height: 16px;

    .mat-button-wrapper {
         padding: 2px 0 !important;
         line-height: 16px !important;
         width: 16px !important;
         height: 16px !important;

         .mat-icon {
              font-size: 16px;
              padding-right: 4px;
              line-height: 16px;
          }
    }
}

结果如下:

.mat-button-wrapper 的属性根本没有改变。图标在按钮的底部,图标的大小没有改变。我怎样才能改变 material2 的图标大小来避免它?

根据我弄乱 angular material 组件样式的经验,我可以说它们的样式是深度嵌套的,几乎所有时候层次结构都是这样的,无论您针对 classes 在元素上,不起作用。

但是,对于图标,有一个简单的解决方案。使用 <i> 标签和有意义的 material class 代替 <mat-icon>

<button mat-mini-fab class="my-fab">
  <i class="material-icons md-18" aria-label="favorite">favorite</i>
</button>

在上面的代码中,class md-18定义了图标的字体大小为18像素。您还可以使用 md-24md-36md-48,它们都是 material 框架中预定义的 classes。如果您想要自定义大小,您可以在 css 中使用 my-fab class 定位按钮内的 <i> 标签,并更改其字体大小,如下所示

.my-fab i {
  font-size: 15px;
}

希望对您有所帮助!

好的,我找到了解决方案。更改 angular material 2 组件中的继承属性真的很难。要在我的情况下做到这一点,我应该按照回答 here (already plused :D )

所以在我的代码中,我对 scss 进行了以下更改:

.my-fab {
     width: 20px;
     height: 20px;
     line-height: 14px;
     font-size: 14px;

     &::ng-deep .mat-button-wrapper{
         line-height: 14px;
         padding: 0;

          .mat-icon {
              font-size: 14px;
              padding-right: 4px;
              padding-top: 4px;
          }
    }
}

现在一切看起来都很棒。如果您知道更改 mat-fab-mini 和其中图标的大小的更好和正确的方法 - 请在此处回答,如果有效,我会将其标记为正确答案。否则我会在 2 天后将我的答案标记为正确。

我这样成功了:

html

<button mat-mini-fab color="basic" class="button24">
  <mat-icon class="icon24">attach_file</mat-icon>
</button>

css

.button24 {
  background-color: #eee;
  width: 24px;
  height: 24px;
}
.icon24 {
  font-size: 16px;
  margin-top: -8px;
}

JPM

这个问题的根本原因是图标是内联元素。

所以调整图标大小最清晰的方法是将其更改为块元素:

HTML

<button mat-raised-button>
  <mat-icon>create</mat-icon>
</button>

CSS

button {

      mat-icon {
        display: block;
        width: 128px;
        height: 128px;
        font-size: 128px;
        line-height: 128px;
      }
    }

为我工作

HTML

  <button mat-fab>
    <mat-icon class="md-36" inline aria-label="Home button">
      home
    </mat-icon>
  </button>
  <button mat-fab>
    <mat-icon class="md-48" inline aria-label="Up button">
      expand_less
    </mat-icon>
  </button>

CSS

.material-icons.md-18 {
  margin-top: -3px;
  font-size: 18px;
}
.material-icons.md-24 {
  margin-top: -3px;
  font-size: 24px;
}
.material-icons.md-36 {
  margin-top: -3px;
  font-size: 36px;
}
.material-icons.md-48 {
  margin-top: -4px;
  font-size: 48px;
}

图标正确居中,包装器 div 不会溢出 fab/button 我猜不太优雅

我在标题中使用较小的编辑按钮 (SCSS) 的解决方案:

h3 > button {
    width: 1.8rem !important;
    height: 1.8rem !important;
    line-height: 80% !important;

    .mat-button-wrapper {
        padding-top: 0.4rem !important;
        line-height: 80% !important;
    }

    .mat-icon {
        width: 80%;
        height: 80%;
        font-size: 80%;
    }
}

模板:

<h3>
    Title
    <button mat-mini-fab color="primary" class="ml-2" title="Edit" (click)="edit()">
        <mat-icon>edit</mat-icon>
    </button>
</h3>

似乎要更改按钮的大小,我们已经更改了 1.button 大小 2. 跨度,它是图标的包装器和 3. 包含的图标的大小。还要在全局 style.scss 文件中创建一个 mixin,如下所示,以便它可以重复用于不同的大小。

@mixin change-button-size($btnsize,$fontsize) {
    height: $btnsize;
    width: $btnsize;
    line-height: $btnsize;
    span{
        height: $btnsize;
        padding: 0px !important;
        i{
            font-size: $fontsize;
        }
    }
}
.mat-mini-fab.mdb-30{
    @include change-button-size(30px, 15px);
}
.mat-mini-fab.mdb-40{
    @include change-button-size(40px, 20px);
}

对于带文本的 mat fab 图标,我遇到了类似的问题。实际上有一个简单的解决方案是不使用已弃用的 ng deep

  .my-custom-fab-button {
        display: flex;
        align-items: center;
        justify-content: space-around;
   }

和图标

.mat-icon-button {
           line-height: 1;
                 }

如果您使用带有文本内容的 mat fab 图标(圆形按钮),那么您可以使用 like

.my-fabbutton-with-text {
    width: 50px;     //for the outline
    height: 50px;    // for the outline
    font-size: 14px; //for the text in icon
    display: flex;
    align-items: center;
    justify-content: space-around;
      }

希望对大家有所帮助

快速简单 :)

<button mat-mini-fab color="primary" [ngStyle]="{transform: 'scale(0.8)'
              }">
   <mat-icon>cloud_download</mat-icon>
</button>

您可以尝试在不弄乱字体和定位的情况下缩放 mat-mini-button。请参阅下面的示例。

HTML

<button mat-mini-fab>
    <mat-icon>favorite</mat-icon>
</button>

CSS

button[mat-mini-fab] {
 transform: scale(0.7) !important;
}