How to change the size of mat-icon on Angular Material?
To resize a mat-icon, adjust the font-size in your CSS:
Then, tag your mat-icon with the new CSS class in your HTML:
This way the size will be double using 2em, or set any other scalar value for custom relative sizing.
Scaling icons with transform property
If you find that font-size is too basic for your stylistic ambition, you can have more direct control using the transform CSS property.
Implementing transform for scaling
With the transform: scale(VALUE); style rule, mat-icon can be set to any size designated by VALUE:
The scale() function modifies both the width and height dimensions, while keeping the integrity of the icon intact.
Font-size: a word of caution
The font-size property can produce inconsistent results with mat-icon, depending on the complexity of the icon design. Hence, it's better to trust the scale() function for reliable scaling.
Styling icons with class
Organize your styles by assigning a class to your mat-icon. This would make your CSS target its styles more accurately:
Then, match this in your CSS:
Watch your step: Icon displacement
Note that incorrect adjustments in CSS could lead to icon misalignment. It's always advisable to check the scaled icon's position post scaling.
Advanced Strategies for Mat-icon Styling
Here are some advanced strategies for a more tailored and maintainable approach to Mat-icon styling.
Dynamic Scaling with Viewport Units
Viewport units (vw/vh) can be used for dynamic scaling that adjusts the icon size relative to the viewing area:
Efficiency with CSS Variables
Setting CSS custom properties (variables) can be a time-saver and also makes your code tidy:
Overflow Concerns
When scaling up, ensure icons don’t flow beyond their containers. You might need to wiggle the viewBox attribute or adjust the container dimensions.
Was this article helpful?