Adjust the dimensions of the mat-icon-button

Is there a way to adjust the size of a mat-icon-button? Currently, my icon is set at 24px and I would like to increase it to 48px. I am using mat-icon as the button content, but it seems that mat-icon-button has a fixed size of 40px by 40px.

<button mat-icon-button color="primary">
    <mat-icon class="material-icons">play_circle_filled</mat-icon>
</button>

Answer №1

Customizing Icons in Angular

For those using Angular 8 and above, I found that the following CSS code works perfectly for resizing icons:

.small-icon-button {
   width: 24px !important;
   height: 24px !important;
   line-height: 24px !important;

   .mat-icon {
      width: 16px !important;
      height: 16px !important;
      line-height: 16px !important;
   }
   .material-icons {
      font-size: 16px !important;
   }
}

You won't need to use any ::ng-deep selector.

Update for Angular 15+

If you are working with the new MDC components, the styling approach is slightly different. You may also want to consider using a custom directive for handling various icon button sizes. Check out my directive implementation at: https://github.com/btxtiger/mat-icon-button-sizes

.small-icon-button {
   width: 24px !important;
   height: 24px !important;
   padding: 0px !important;
   display: inline-flex !important;
   align-items: center;
   justify-content: center;

   & > *[role=img] {
      width: 16px;
      height: 16px;
      font-size: 16px;

      svg {
         width: 16px;
         height: 16px;
      }
   }

   .mat-mdc-button-touch-target {
      width: 24px !important;
      height: 24px !important;
   }
}

Answer №2

When it comes to mat-icons, they are essentially font images. To adjust their size, you will need to overwrite the font size of the mat-icon class. For Angular Material 8 and above, include the following code in the component's stylesheet:

.mat-icon{
    font-size:48px !important; //increase the size to 48px from the default 24px
    width:48px;                //remember to adjust the width 
    height:48px;               //and/or height accordingly 

}

See Demo

Alternatively, you can directly modify it in the HTML like so:

<mat-icon style="font-size:48px">mail</mat-icon>

For older versions, you still have the option to use ::ng-deep to target that specific class deep within the host element. Adjusting the width and height is also necessary to maintain proportionality with the backdrop size.

HTML:

<button mat-button>    
  <mat-icon class="material-icons">play_circle_filled</mat-icon>
</button>

CSS

::ng-deep .mat-icon{
    height:48px !important;
    width:48px !important;
    font-size:48px !important;
}

Take a look at the Demo


If you prefer to avoid using `::ng-deep`, you can opt for `ViewEncapsulation.None` (but do so judiciously):

Class:

import {ViewEncapsulation} from '@angular/core';

@Component({
  encapsulation: ViewEncapsulation.None
})

This allows you to style directly from the component's stylesheet.

CSS:

.mat-icon{
    height:48px !important;
    width:48px !important;
    font-size:48px !important;
}

See Demo


Another approach is to style it from the main stylesheet, styles.css:

styles.css

.mat-icon{
    height:48px !important;
    width:48px !important;
    font-size:48px !important;
}

See Demo


Lastly, inline styling is also an option for customization:

HTML:

<button mat-button>    
  <mat-icon style="
    height:48px !important;
    width:48px !important;
    font-size:48px !important;" class="material-icons">play_circle_filled</mat-icon>
</button>

See Demo

Answer №3

To customize the size of buttons and icons in your component's SCSS file (assuming you are using Sass), you can do the following:

.mat-icon-button.large {

    width: 48px;
    height: 48px;
    line-height: 48px;

    .mat-icon {
      font-size: 48px;
      width: 48px;
      height: 48px;
      line-height: 48px;
    }
  }

You have the flexibility to adjust both the button and icon sizes to your liking. Keeping them both at 48px will eliminate any spacing around the icon. However, if you prefer a larger button size, consider increasing it to something like 64px.

When implementing this in your HTML code, use the following:

<button mat-icon-button class="large"><mat-icon>chat</mat-icon></button>

Answer №4

Here is a simple solution for setting the size of an icon in Angular 9:

If you want your icon to be 40x40, follow these steps:

HTML:

<button mat-icon-button>
    <mat-icon id="add">add</mat-icon>
</button>

CSS:

.mat-icon {
    height: 40px;
    width: 40px;
    font-size: 40px;
    line-height: 40px; // Make sure to include this line
}

You do not need to use ng-deep or !important.

Answer №5

Illustrated below is a sample utilizing scss from the styles.scss to address any encapsulation challenges (defining it within a custom theme achieves the same). It incorporates an additional level of specificity to eliminate the need for !important.

For further detail, refer to this Stackblitz demo.

html

<h3>Standard Button</h3>
<button mat-icon-button color="warn" aria-label="standard button">
    <mat-icon>cloud_upload</mat-icon>
  </button>

<h3>Enlarged Button</h3>
<button mat-icon-button color="warn" class="icon-button-large" aria-label="small button">
    <mat-icon>cloud_upload</mat-icon>
  </button>

<h3>Compact Button</h3>
<button mat-icon-button color="warn" class="icon-button-small" aria-label="large icon">
    <mat-icon>cloud_upload</mat-icon>
</button>

style.scss

button[mat-icon-button]{
$large-size-button: 80px;
$large-size-icon: 48px;

    &.icon-button-large {
      width: $large-size-button;
      height: $large-size-button;
      line-height: $large-size-button;
    .mat-icon {
      font-size: $large-size-icon;
      width: $large-size-icon;
      height: $large-size-icon;
      line-height: $large-size-icon;
    }
    .mat-button-ripple {
      font-size: inherit;
      width: inherit;
      height: inherit;
      line-height: inherit;
    }
  }

  $small-size-button: 24px;
  $small-size-icon: 18px;

    &.icon-button-small {
      width: $small-size-button;
      height: $small-size-button;
      line-height: $small-size-button;
    .mat-icon {
      font-size: $small-size-icon;
      width: $small-size-icon;
      height: $small-size-icon;
      line-height: $small-size-icon;
    }
    .mat-button-ripple {
      font-size: inherit;
      width: inherit;
      height: inherit;
      line-height: inherit;
    }
  }
}

Answer №6

Although it's been a bit since this question was initially asked, I find the zoom CSS property to be quite effective and elegant for resolving certain issues.

Ensuring the selector specificity is high enough to override Angular Material styles can prevent the need for using the disliked !important override method.

button[mat-icon-button].mat-icon-button.large-icon-button {
  zoom: 1.5; // Please adjust as needed for precise sizing.
}

Answer №7

In my Angular 12 project, I have implemented the following SCSS styles:

$sizes: [17, 20, 25, 35, 40];
@each $size in $sizes {
  button.mat-icon-button {
    &[size="#{$size}"] {
      line-height: initial;
      min-width: auto;
      height: #{$size}px;
      width: #{$size}px;
      .mat-button-wrapper {
        line-height: initial;
        mat-icon {
          height: auto;
          width: auto;
          line-height: initial;
          font-size: #{$size * .7}px;
        }
      }
    }
  }
}

It seems that sizes smaller than 17 are not functioning as expected, but considering the given range, it should suffice for most cases. I trust this information proves useful to you :)

Answer №8

In addition to the fantastic solution provided here

To streamline code redundancy for various sizes, I incorporated a scss mixin

@mixin mat-icon-size($button-size, $icon-size) {

  width: $button-size !important;
  height: $button-size !important;
  padding: 0 !important;
  display: inline-flex !important;
  align-items: center;
  justify-content: center;

  & > *[role=img] {
    width: $icon-size;
    height: $icon-size;
    font-size: $icon-size;

    svg {
      width: $icon-size;
      height: $icon-size;
    }
  }

  .mat-mdc-button-touch-target {
    width: $button-size !important;
    height: $button-size !important;
  }
}

$mat-icon-sizes: (
  '24': (24px, 16px),
  '36': (36px, 24px),
  '72': (72px, 48px),
);

@each $name, $sizes in $mat-icon-sizes {
  .mat-icon-size-#{$name} {
    @include mat-icon-size(nth($sizes, 1), nth($sizes, 2));
  }
}

Simply utilize it as shown below

<button mat-icon-button class="mat-icon-size-72"><mat-icon>edit</mat-icon></button>

I personally prefer using specific numeric values, but feel free to use custom labels like sm, md, lg, and so on.

Answer №10

When you set [inline]="true", as mentioned in the documentation for Angular Material Icon Docs, it allows you to use mat-icon inside a button with type mat-icon-button:

This automatically sizes the icon to match the font size of the element containing the icon.

Here's an example in HTML:

<button mat-icon-button class="material-icons" color="primary">
    <mat-icon [inline]="true">play_circle_filled</mat-icon>
</button>

And here is the corresponding CSS:

.material-icons {
  font-size: 2em
}

The inline Input Property applies the following CSS rules, which are similar to what other answers have already suggested:

.mat-icon.mat-icon-inline {
    font-size: inherit;
    height: inherit;
    line-height: inherit;
    width: inherit;
}

For such a simple component like mat-icon, using this property may seem like more overhead compared to just using CSS in the first place.

Answer №11

To properly style the latest MDC components, utilize the existing CSS variables provided:

button {
      --mdc-icon-button-state-layer-size: 24px;
      --mdc-icon-button-icon-size: 20px;
      padding: 0;
}

Avoid using ng-deep or !important declarations.

Answer №12

  <button md-mini-fab (click)="add()" 
    style="cursor: pointer;textdecoration:none;height:30px;width:30px">                                               
    <md-icon style="margin:-4px 1px;color:white;font-size: 16px;">add</md-icon>
    </button>

This code is functioning flawlessly within an Angular2 environment.

Answer №13

Despite trying various solutions, I couldn't find a way to alter the size of the "touch target," and it seems others are facing the same issue (refer to GitHub problem).

Building upon the guidance provided by Bop's response, I discovered a method that worked for me (utilizing a SASS variable to streamline the process of adjusting the size value in a single location):

button {
  $icon-size: 20px; // Defining SASS variable

  --mdc-icon-button-icon-size: #{$icon-size};
  --mdc-icon-button-state-layer-size: #{$icon-size + 8};
  padding: 0;

  .mat-icon {
    height: $icon-size;
    width: $icon-size;
    font-size: $icon-size;
  }

  // Addressing the issue highlighted in https://github.com/angular/components/issues/27118
  ::ng-deep span.mat-mdc-button-touch-target {
    height: #{$icon-size + 8};
    width: #{$icon-size + 8};
  }
}

Answer №14

My Updated Solution for Angular 18

Note: Utilizing the position property is essential to correctly position the shadow on hover.

.btn {
    position: relative;

    .icon {
        width: 48px;
        height: 48px;
        font-size: 48px;
        position: absolute;
        transform: translateX(-50%) translateY(-50%);
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Maintain Spacing Between Two Elements While Ensuring the Right Element Stays to the Right Even if the First One is Hidden in CSS

Presently, I have two icons being displayed with a space between them using the flex-box property justify-content and value space-between. The issue arises when only one icon is displayed, as I need the V-icon to always remain on the left and the urgent-ic ...

Showing a div with a smooth fade-in effect while switching its display property to inline using jQuery

Currently, I am working on a project that involves implementing a "side pop up". To ensure it doesn't flicker like it does with jQuery's "hide()" method, I want to start by setting the display property to none using CSS. My main question is: - Ho ...

Leveraging both function arguments and the 'this' keyword within a single

I have a JavaScript function that utilizes both the `this` keyword and captures arguments: var Watcher = function() { var callbacks = []; var currentValue = null; this.watch = function (callback) { callbacks.push(callback); if (currentValue ...

Building hierarchical comments in React Native

I'm currently involved in a React Native project that includes a Nested comment section. These comments are retrieved as JSON data and displayed using a FlatList. const App = () => { const [isLoading, setLoading] = useState(true); const [data, ...

Utilize a map image as a texture on a plane within a personalized shader in THREE.js

I'm currently facing an issue where I need to load two images as textures, blend between them in the fragment shader, and apply the resulting color to a plane. However, I am struggling to even display a single texture properly. My process for creatin ...

Change the color of a specific day on the Arshaw Calendar using CSS

Can anyone help me with changing the color of the box in the month view in the arshaw calendar? I've been attempting to do so using this script, but my lack of expertise in javascript is proving to be a hurdle. The goal is for this script to be called ...

Using Angular 2 in combination with three.js OrbitControls allows for enhanced

Exploring the integration of a threejs example within my Angular (CLI) 2 application. First, I installed threejs using the command: npm install three --save Then, I added typings with this command: npm install @types/three --save-dev After completin ...

Just incorporated Angular Material and Angular Animations into my project and encountered a slew of errors

After executing the command line: npm install --save @angular/material @angular/animations This snippet shows my package.json file: { "name": "cerpnew", "version": "0.0.0", "license": "MIT" ...

Interacting Between PHP and Javascript

<form action="../"> <select onchange="window.open(this.options[this.selectedIndex].value,'_top')"> <option value="">Choose a zipcode </option> <option value="92507">92507</option> <option value=" ...

Guide to organizing an HTML table column with a header click using PHP and MySQL

I am currently working on a table that displays data from a MySQL database. I would like to implement functionality that allows users to click on certain columns to sort them in ascending or descending order. However, I am unsure of whether to use PHP, H ...

Using Jasmine to Jest: Mocking Nested function calls

I am currently working on testing my TypeScript functions with Jasmine: //AB.ts export async function A() { } export async function B() { A(); } My goal is to unit test function B by mocking out function A to see if it is called. Here is the code I h ...

The equivalent of ESM for resolving modules using the `createRequire` function with a specified

In the process of developing a JavaScript instrumentation engine, I am currently focused on traversing a source file's Abstract Syntax Tree (AST) and queuing imported modules for instrumentation in a recursive manner. In order to achieve this, it is c ...

Expansive background with adaptable height dimensions

Is there a way to use Foundation to create a header with a full-width image and responsive height using CSS only, or do I need to use JavaScript for this? Can someone assist me with this? This is the code from index.html: <!doctype html> <html ...

Is there a way to automatically change the value of one input box to its negative counterpart when either of the two input boxes have been filled in?

Consider two input boxes: box1 box2 If a user enters a number in one of the input boxes, we want the value of the other input box to automatically change to the opposite sign of that number. For example: User enters 3 in box1. The value of box2 shoul ...

Activate continuous speech identification

Is it possible to activate the capability of recognizing continuous speech through the REST API (using javascript SDK) with the Bing Speech API? The Javascript SDK example available at https://github.com/Microsoft/Cognitive-Speech-STT-JavaScript only seem ...

What is the best way to execute two asynchronous calls sequentially in JavaScript?

When using a common generic function for AJAX calls, the initial request retrieves all data from the server and maintains it within local scope. However, subsequent requests are still hitting the server, even when the data is already available locally. Thi ...

After clearing the option, the onChange function stops functioning

I'm facing an issue with the following code: success: function (data) { $('#' + idDivRefresh).endLoading(); if (data.message != '@Geral.Sucesso') { $('#' + idDropDown + ...

Is having async as false really detrimental?

Splitting my inquiry into two sections. Within my website, I am dynamically generating some divs by utilizing ajax post requests to retrieve data from the database. Following is the structure of my setup. <html> <body> <script type=" ...

Dynamically and asynchronously loading numerous LinkedIn share buttons on a page

On my page, I have a grid of post thumbnails that are fetched via AJAX and can be filtered. When a user clicks on a thumbnail, a carousel opens with the selected post centered. In this carousel, each post has a LinkedIn share button integrated. The issue ...

Showcasing just the initial two lines of a flexbox using CSS and HTML

My current project requires me to develop a search bar that displays search results in a grid format. The items should be arranged next to each other in two rows. If there are additional results, a button labeled +32 should be present to show all results. ...