Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular material icon mat-icon is not displaying correctly

I am creating Navigation drawer in my angular 4 app using angular-material. Initially, I am creating a toolbar to have header and menu button(mat-icon is menu)

Here is my code,

<mat-toolbar class="example-header" color="primary">
    <button mat-icon-button><mat-icon>menu</mat-icon></button>
<span class="header-name">Hello</span>
</mat-toolbar>

But I am getting the output as follows,

enter image description here

But I wanted output something like this,

enter image description here

The difference in the output is, the mat-icon button has grey background whereas the required doesn't. Please correct me.

The app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AgmCoreModule } from '@agm/core';
import { MapsAPILoader } from '@agm/core';
import { FormsModule } from '@angular/forms';
import { GoogleMapsAPIWrapper } from '@agm/core';
import { AppComponent } from './app.component';
import 'hammerjs';
import {
  MatAutocompleteModule,
  MatButtonModule,
  MatButtonToggleModule,
  MatCardModule,
  MatCheckboxModule,
  MatChipsModule,
  MatDatepickerModule,
  MatDialogModule,
  MatExpansionModule,
  MatFormFieldModule,
  MatGridListModule,
  MatIconModule,
  MatInputModule,
  MatListModule,
  MatMenuModule,
  MatNativeDateModule,
  MatPaginatorModule,
  MatProgressBarModule,
  MatProgressSpinnerModule,
  MatRadioModule,
  MatRippleModule,
  MatSelectModule,
  MatSidenavModule,
  MatDrawer, MatDrawerContainer, MatDrawerContent,
  MatSliderModule,
  MatSlideToggleModule,
  MatSnackBarModule,
  MatSortModule,
  MatTableModule,
  MatTabsModule,
  MatToolbarModule,
  MatTooltipModule,

  MatStepperModule,
  MATERIAL_SANITY_CHECKS
} from '@angular/material';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    BrowserAnimationsModule,
    MatToolbarModule,
    MatSidenavModule,
    MatIconModule
  ],
  providers: [GoogleMapsAPIWrapper],
  bootstrap: [AppComponent]
})
export class AppModule { }
like image 741
Anil Avatar asked Apr 09 '18 04:04

Anil


People also ask

How do I change the size of the mat-icon?

Since Angular Material uses 'Material Icons' Font-Family, the icon size depends on font-size. Therefore, if you want to modify the size of the icon then you change its font-size in your CSS file. You have to set the width and height too.

What is mat-icon in angular?

mat-icon makes it easier to use vector-based icons in your app. This directive supports both icon fonts and SVG icons, but not bitmap-based formats (png, jpg, etc.).

Why is my icon not working in Angular Material?

If you have overwritten some existing Angular Material styling or any other styling that somehow affects the icon, it may cause an issue. Move the icon code outside of any styling and test. So I just moved it to the child element. Below is part of Angular Material grid.

How to import maticonmodule in angular?

We have to import MatIconModule from Angular Material Modules. These icons are not limited to angular,In HTML webpages just use <i></i> in place of <mat-icon> tag.

How to use font ligature as an icon in Angular Material?

<mat-icon> is part of angular material module called MatIconModule .We can use font ligature as an icon by putting the ligature text in <mat-icon> component. For example <mat-icon>home</mat-icon> displays home icon. We have to import MatIconModule from Angular Material Modules.

How to add material icons in HTML page using maticonmodule?

We have to import MatIconModule from Angular Material Modules. These icons are not limited to angular,In HTML webpages just use <i></i> in place of <mat-icon> tag. All the material icons packaged into a single font file. All we need to do is adding the below Google web fonts css in our html page.


1 Answers

In your app.module.ts, you forgot to import MatButtonModule into your module. (By the way, you shouldn't import every single module if you don't even use it. It's a bad idea!)

@NgModule({
  imports: [
    MatButtonModule
    // ...
  ]
})
like image 55
Edric Avatar answered Sep 29 '22 17:09

Edric