51

I am very new to css and I cannot align the mat-icon (the thought bubble) to the div class=img (the green square on the left) - see the attached image. At this moment the icon is a little bit near the top

For adding the svg icon I used Material Angular - MatIconRegistry class (don't know if it matters)

My result: enter image description here

Here is my html:

<div class="container">
  <div class="img"><mat-icon svgIcon="info"></mat-icon></div>
  Some text here!
</div>

Here is my css:

$conainer-min-width: 256px !default;
$conainer-max-width: 512px !default;
$conainer-height: 50px !default;

div.container {
  max-width: $container-max-width;
  min-width: $container-min-width;
  height: $container-height;
  margin: auto;
  color: #000000;
  background-color: #ffffff;
  line-height: $container-height;
  text-align: center;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  border-width: 1px;
  border-style: solid;
  border-color: #9e9e9e;
  padding-right: 20px;
  position: fixed;
  z-index: 1000;
  left: 0;
  right: 0;
  top: 30px;
}


div.img {
  width: $container-height;
  height: $container-height;
  float: left;
  display: block;
  margin: 0 auto;
  border-right-width: 1px;
  border-right-style: solid;
  border-right-color: #9e9e9e;
  line-height: $container-height;
  justify-content: center;
  background-color: #3f9c35;
}

Any thoughts on how to fix this?

Mike Me
  • 2,192
  • 3
  • 20
  • 32

4 Answers4

101

You can add vertical align to the mat-icon element or .mat-icon class:

To override all material icons with global CSS: Stackblitz

.mat-icon {
    vertical-align: middle;
}

ATENTION: If your text have capital letters, add negative margin to prevent visual issues on some browsers (Stackblitz demo).
Also you can simply use vertical-align: sub or vertical-align: bottom if fits better with your requirements. Check following alignment MDN demo


Using FLEX with a container element for each icon/text: Stackblitz

CSS:

.icon-text {
    display: flex;
    align-items: center;
}

HTML:

<div class="icon-text">
   <mat-icon>home</mat-icon>
   <span>Some text here 1</span>
</div>

To style a particular material icon with utility class: Stackblitz

In some global css file:

.v-align-middle { 
    vertical-align: middle;
}

HTML:

<div>
   <mat-icon class="v-align-middle">home</mat-icon>
   <span class="v-align-middle">Some text here 1</span>
</div>

** some css frameworks already have classes like this, eg: bootstrap 4 **


To style a particular material icon: Stackblitz

CSS:

.custom-class mat-icon {
    vertical-align: middle;
}

/** or using the class .mat-icon */
.custom-class .mat-icon {
    vertical-align: middle;
}

HTML:

<div class="custom-class">
   <mat-icon>home</mat-icon>
   <span>Some text here 1</span>
</div>
The.Bear
  • 5,621
  • 2
  • 27
  • 33
  • As suggested [here](https://github.com/google/material-design-icons/issues/121), set `display: flex;` in parent div may give you better control. – bob Sep 20 '18 at 03:22
  • Thank you for the solution. After some tests I personaly like 'vertical-align: sub' better. The icons are a bit too low with 'vertical-align: middle'. – Mariano LEANCE Nov 09 '20 at 00:22
  • How come this doesn't work if there is a `height` style on the parent container? – trebor Apr 10 '22 at 00:06
  • @trebor if you are using the global override, use `line-height` in the parent container to get a vertical center text/icon. DEMO: https://stackblitz.com/edit/angular-pbpfnh-oe4xm5?file=app%2Ficon-overview-example.css – The.Bear Apr 11 '22 at 02:06
  • good works. However, in my case, only the 2nd solution with flex work. – ZZZ Aug 19 '22 at 21:55
6

You can use the flex Layout to align the icon

<div fxLayout="row" fxLayoutAlign="center">
  <mat-icon fxFlexAlign="center">supervisor_account</mat-icon>
</div>

Read more about flexLayout in the documentation, fxLayout row is the same as:

div {
  display:flex;
  flex-direction:row;
  justify-content:center; /* For fxLayoutAlign="center" */
}

Then you further align the flex item to center i.e fxFlexAlign="center"

mat-icon {
  display:flex;             
  align-self:center; 
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Novak254
  • 439
  • 6
  • 14
2

What works for me is to use flex-container (justify-content / align-items to center) along with these properties font-size, widthand height - depending on your font size, which drives the other two properties:

::ng-deep .mat-icon {
  font-size: 48px;
  width: 48px;
  height: 48px;
}
Felix
  • 3,999
  • 3
  • 42
  • 66
0

I believe your problem is typo on these variable names:

$conainer-min-width: 256px !default;
$conainer-max-width: 512px !default;
$conainer-height: 50px !default;

which doesn't match the variables used here:

div.container {
max-width: $container-max-width;
min-width: $container-min-width;
height: $container-height; }

I think it should work once you change conainer to container :)

A. U.
  • 52
  • 3
  • Thank you for the feedback, but it is not . the original was something in a foreign language and I changed and replaced it and added the typo. So this is not the issue – Mike Me Apr 04 '18 at 18:31
  • you can see @The.Bear answer - he provided in a platform. – Mike Me Apr 04 '18 at 18:49