Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mat-icon does not center in its div

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?

like image 785
Mike Me Avatar asked Apr 04 '18 17:04

Mike Me


People also ask

How do I center an icon inside a div?

Since they are already inline-block child elements, you can set text-align:center on the parent without having to set a width or margin:0px auto on the child. Meaning it will work for dynamically generated content with varying widths . This will center the child within both div containers.

How do you center align icons in CSS?

The most preferred way to center Font Awesome Icons is to assign a center class to each <i> tag. Setting width to 100%, let's each icon cover 100% area horizontally. Also text-align then centers the icon accordingly to the width being used. Example 1: Adding a custom class in all <i> tag.

How do I move the mat icon to the right?

To right align the mat dialog actions buttons, use align="end" attribute on mat-dialog-actions container.

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.


1 Answers

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

To override all material icons: Stackblitz

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

vertical-align: sub could be useful in some cases too, check MDN demo: https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align


With FLEX: 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> 
like image 167
The.Bear Avatar answered Sep 21 '22 12:09

The.Bear