Which one is better to use:

The URL provided below contains code for an Angular Material table example: https://stackblitz.com/edit/angular-riepzk-my4nvn?file=app/table-basic-example.html. The user is struggling to find an editable Material table example and is seeking a solution for adjusting column width. The recommended modern approach is to use CSS flex-box, but if content in a cell is long, column width can be specified to a larger number. Alternatively, native HTML can be used to avoid complexity. The source code for the Angular Material table example is provided by @joyBlanks. The user is also experiencing difficulties with form arrays applied on the mat-table.

Question:

The heading implies my query. Can you clarify which one is suitable and explain any disparities that may exist?

or

in my HTML markup?

At first glance, both seem to function well, but I believe there must be some distinction between them.

To clarify, I am referring to the ability to adjust the size of columns, as demonstrated in this sample: https://stackblitz.com/edit/angular-rtfc5v?file=src%2Fapp%2Fapp.component.html

If you switch the


Solution:

To align your data/table, you can use Stackblitz with the

<mat-table>

element, which utilizes
CSS flex-box
properties. However, if you have lengthy text, it won’t automatically resize unless explicitly instructed. It’s important to note that this option may require compromising on certain features such as the inability to span rows or columns if needed, as seen in

rowspan

and

colspan

.

Utilizing the as attribute in an HTML table’s td element through Stackblitz (

table[mat-table]

) can automatically resize the content of the cell based on the column it belongs to. As a result, the entire column adapts to the width of the cell accordingly.

One way to implement a modern layout is by utilizing the CSS flex-box approach, which is especially useful for shorter content. However, for longer content in a cell, one can simply specify a larger column width. Alternatively, the native HTML option can also be used without much hassle, as seen in the code snippet

table/tr/td

.

This is the Angular material table’s source code.

Thanks to @joyBlanks

Frequently Asked Questions