![]() |
VOOZH | about |
In Angular Material, the mat-table component is a powerful data table tool that allows you to display data in a structured and flexible way. However, when it comes to customizing the appearance of the table, especially the width of individual columns, you may need to apply custom styling techniques.
In this article, will walk you through various methods for setting the width of mat-table columns in Angular, offering both inline and external styles, as well as techniques for responsive design.
The mat-table component in Angular Material is a versatile data table that supports features such as sorting, pagination, and filtering. By default, the columns in a mat-table take up the space they need based on their content. However, you may want to define specific widths for some or all of the columns to create a better visual layout or to ensure the table remains consistent across different screen sizes.
ng new angular-table
cd angular-table
Next, install Angular Material and add the MatTableModule and MatPaginatorModule to your project:
ng add @angular/material"dependencies": {
"@angular/animations": "^18.2.0",
"@angular/cdk": "^18.2.6",
"@angular/common": "^18.2.0",
"@angular/compiler": "^18.2.0",
"@angular/core": "^18.2.0",
"@angular/forms": "^18.2.0",
"@angular/material": "^18.2.6",
"@angular/platform-browser": "^18.2.0",
"@angular/platform-browser-dynamic": "^18.2.0",
"@angular/router": "^18.2.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.10"
}
Now, let's create the mat-table and define column widths. Follow these steps:
In app.component.ts, we'll create a standalone component that contains the table, sets up data, and defines the column widths.
In this file:
Next, we define the structure of the table in app.component.html:
In app.component.css, you can add some basic styles to enhance the appearance of the table:
To start the application run the following command.
ng serve --open