Angular 15 Material Design 表格样式不再起作用

huangapple go评论61阅读模式
英文:

Angular 15 Material Design table styling not working anymore

问题

我想要样式化Angular中的Angular Material表格中的表行和单元格。

但是在Angular 15中,以简单的CSS样式为例的“旧”示例不再起作用:

    table {
      width: 100%;
      border-spacing: 0 8px !important;
    }
    
    td.mat-cell {
      padding: 16px 0 16px 0;
      border-bottom: 2px solid #ffa600;
      border-top: 2px solid #ffa600;
    }
    
    tr.mat-row {
      background: #79a1d8;
    }

无论我在组件的CSS文件中使用什么样式,表格设计都保持不变。

这是Angular Material Design的基本表格示例,通过一些样式进行扩展,但没有效果。

Angular Material Design的基本表格示例

从旧版本(如版本8)到当前版本15,Angular进行了哪些更改,导致表格样式失效?

英文:

I want to style the table rows and cells in an Angular Material Table in Angular.

But the "old" examples with simple css styles don't work anymore in Angular 15:

table {
  width: 100%;
  border-spacing: 0 8px !important;
}

td.mat-cell {
  padding: 16px 0 16px 0;
  border-bottom: 2px solid #ffa600;
  border-top: 2px solid #ffa600;
}

tr.mat-row {
  background: #79a1d8;
}

No matter what styles in the component's css file I use, the table design stays the same.

Here is the basic Table-Example of the Angular Material Design - extended by some styles - with no effects.

https://stackblitz.com/edit/angular-kwfygf?file=src/app/table-basic-example.css

What does Angular changed from older versions like version 8 to the current version 15, that let the table styles fail?

答案1

得分: 3

以下是翻译好的部分:

"你尝试定位的元素没有类,而是似乎具有使用不同样式语法的属性。"

table {
  width: 100%;
  border-spacing: 0 8px !important;
}

td[mat-cell] {
  padding: 16px 0 16px 0;
  border-bottom: 2px solid #ffa600;
  border-top: 2px solid #ffa600;
}

tr[mat-row] {
  background: #79a1d8;
}

"这段CSS似乎有效。"

希望这对你有所帮助。如果还有其他需要翻译的部分,请告诉我。

英文:

The elements you try to target don't have classes, instead they seem to be having attributes which use different styling syntaxes.

table {
  width: 100%;
  border-spacing: 0 8px !important;
}

td[mat-cell] {
  padding: 16px 0 16px 0;
  border-bottom: 2px solid #ffa600;
  border-top: 2px solid #ffa600;
}

tr[mat-row] {
  background: #79a1d8;
}

This CSS seems to work.

https://stackblitz.com/edit/angular-kwfygf-kdthdr?file=src%2Fapp%2Ftable-basic-example.css

Here is a very small example of the difference in case the link stops working:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-css -->

table {
  width: 100%;
  border-spacing: 0 8px !important;
}


/** Styling for attributes **/

td[mat-cell] {
  padding: 16px 0 16px 0;
  border-bottom: 2px solid orange;
  border-top: 2px solid orange;
}

tr[mat-row] {
  background: skyblue;
}


/** For classes **/

td.mat-cell {
  padding: 16px 0 16px 0;
  border-bottom: 2px solid green;
  border-top: 2px solid green;
}

tr.mat-row {
  background: pink;
}

<!-- language: lang-html -->

&lt;!-- table with attributes --&gt;

&lt;table mat-table&gt;
  &lt;tr mat-row&gt;
    &lt;td mat-cell&gt; Table with attributes &lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr mat-row&gt;
    &lt;td mat-cell&gt; Name &lt;/th&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;!-- table with classes --&gt;

&lt;table class=mat-table&gt;
  &lt;tr class=mat-row&gt;
    &lt;td class=mat-cell&gt; Table with classes &lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr class=mat-row&gt;
    &lt;td class=mat-cell&gt; Name &lt;/th&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 15:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定