【问题标题】:right text align mat-expansion-panel material componentright text align mat-expansion-panel 材质组件
【发布时间】:2018-08-28 15:59:46
【问题描述】:

我想将mat-expansion-panel组件的描述文字右对齐

我尝试使用 align-text to right 但没有任何改变 这是我的 HTML

<mat-expansion-panel>
    <mat-expansion-panel-header>
      <mat-panel-title>
        Cycle ingénieur en informatique
      </mat-panel-title>
      <mat-panel-description>
        2014-2017
      </mat-panel-description>
    </mat-expansion-panel-header>
    ....
  </mat-expansion-panel>

CSS:

mat-panel-description {
    text-align: right;
}

这是实际行为

我会让 2014-2017 年保持不变。

【问题讨论】:

  • 你能检查父元素是容器宽度的100%吗?

标签: html css angular angular-material


【解决方案1】:

将 fxLayoutAlign="end" 添加到 mat-panel-description。它对我有用。

'<mat-panel-description fxLayoutAlign="end">
2014-2017
</mat-panel-description>'

【讨论】:

    【解决方案2】:

    我发现的最简单的解决方案是重置flex-grow of mat-panel-description

    // my.component.css
    
    mat-panel-description.right-aligned {
      flex-grow: 0;
    }
    
    // my.component.html
    
    ...
      <mat-expansion-panel-header>
        <mat-panel-description class="right-aligned">
          2014-2017
        </mat-panel-description>
      </mat-expansion-panel-header>
    ...
    

    StackBlitz

    【讨论】:

    • 它确实适用于.mat-expansion-panel-header-description.right-aligned
    【解决方案3】:

    基于此解决方案:https://github.com/angular/components/issues/10024

    .mat-expansion-panel-header-description, .mat-expansion-panel-header-title {
      flex-basis: 0;
    }
    

    再次阅读问题,这对我有用:

    .mat-expansion-panel-header-description {
      display: flex;
      justify-content: flex-end;
    }
    

    【讨论】:

    【解决方案4】:

    面板标题内容包含在一个作为 flexbox 的 span 中,因此您可以使用它来将内容推送到两侧。

    查看:

    <mat-expansion-panel>
    <mat-expansion-panel-header class="right-aligned-header">
      <mat-panel-title>
        Cycle ingénieur en informatique
      </mat-panel-title>
      <mat-panel-description>
        2014-2017
      </mat-panel-description>
    </mat-expansion-panel-header>    
    

    css(这必须是全局 css,例如,styles.css 不在组件样式中):

    .right-aligned-header > .mat-content {
        justify-content: space-between;
    }
    
    .mat-content > mat-panel-title, .mat-content > mat-panel-description {
      flex: 0 0 auto;
    }
    

    这是Stack Blitz demo

    【讨论】:

    • 你能解释一下为什么这必须在 gobal styles.css 中吗?
    • 我希望它与组件中的 shadow dom 有关,并且全局样式(默认样式)将覆盖它们...
    猜你喜欢
    • 2021-10-25
    • 2019-10-26
    • 1970-01-01
    • 2019-03-09
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    相关资源
    最近更新 更多