Position the center of an Angular Material icon in the center

Seeking help to perfectly center an Angular Material icon inside a rectangular shape. Take a look at the image provided for reference.

https://i.sstatic.net/oFf7Q.png

The current positioning appears centered, but upon closer inspection, it seems slightly off-center. It appears that the edge of the icon is aligned rather than the center itself, creating this misalignment. I'm looking for a way to center the actual center of the icon rather than just the outer edges.

I've considered calculating the precise position within the code, but I'd prefer to explore alternative solutions as this method seems delicate and time-consuming.

Referencing the template structure:

<div
    class="rectangle"
    (click)="clicked(item)"
    [style.backgroundColor]="item.color"
    *ngIf="item.itemType == allEnumTypes.ClickButton"
    title="{{ item.warning }}"
  >
    <mat-icon
      *ngIf="item.icon"
      fontIcon="{{ item.icon }}"
      [style.color]="item.iconColor"
      class="large-icon"
      style="padding-top: 20%; padding-right: 30%"
    ></mat-icon><br /><br />
    <span class="select-button-text" [style.color]="item.fontColor" style="padding-bottom: 0%">{{ item.display }}</span>
  </div>

This is the CSS applied to the rectangle class:

.rectangle {
  width: 139px;
  height: 125px;
  border: 2px solid #bfc0c2;
  opacity: 1;
  text-align: center !important;
  background: #ffffff 0% 0% no-repeat padding-box;
}

Your insight in identifying the issue would be greatly appreciated. Thank you!

Answer №1

I find using css-flex really helpful for centering elements

  .center{
    display:flex;
    flex-direction:column;
    justify-content:center;
    align-items:center;
  }

If you have a rectangle

.rectangle {
    width: 139px;
    height: 125px;
    border: 2px solid #bfc0c2;
  }

You can use

<div class="center rectangle">
  <!--notice the margin-top is equivalent to line-height-->
  <div class="center" style="margin-top:1.25rem">
    <mat-icon
      aria-hidden="false"
      style="font-size: 3rem;width:3rem;height:3rem"
      aria-label="Example home icon"
      >home</mat-icon
    >
    <div style="line-height: 1.25rem">Home</div>
  </div>
</div>

or if you prefer to center both the icon and label together

<div class="center rectangle">
    <mat-icon
      aria-hidden="false"
      style="font-size: 3rem;width:3rem;height:3rem"
      aria-label="Example home icon"
      >home</mat-icon
    >
    <div style="line-height: 1.25rem">Home</div>
</div>

Check out the example on stackblitz

NOTE: Remember to adjust font-size, width, and height to change mat-icon size

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

Achieving Dynamic Center Alignment for One or Two DIVs

My goal is to create a page layout with three DIVS arranged as Left|Center|Right, while still being able to display different combinations such as Center, Left|Center, or Center|Right in the center of a wrapper div. I have been using jQuery toggle to swit ...

Ways to center Bootstrap panel in the middle of a webpage

Is there a way to center this entire panel on the web page? I could use some guidance with this. Does anyone have recommendations for a good book to learn Bootstrap design? <div class="panel panel-default" style="max-width:500px;margin-left:auto;margi ...

Cut out a passage by using a polyline shape

I am looking to create a unique effect by clipping a text like a heading1 using an svg polyline. The concept is to place the H1 text behind the polyline background and make it appear frosted or blurred. I have successfully achieved this in the past but see ...

Creating CSS-in-JS Components in React JS without External Stylesheet interference

import React, { Component } from "react"; import './navbar.css' class NavBar extends Component { render() { return ( <div> navbar content </div> ); } } export default NavBar; If I were to ...

Getting Form Value in Component.ts with Angular 5

How can I incorporate an input form into my component while constructing a form? <div class="row"> <div class="col-md-6 offset-md-3 text-center> <h2> Login Form </h2> <form (ngSubmit)="OnSubmit(login.value,password.value)" #l ...

Transitioning a JavaScriptIonicAngular 1 application to TypescriptIonic 2Angular 2 application

I am currently in the process of transitioning an App from JavaScript\Ionic\Angular1 to Typescript\Ionic2\Angular2 one file at a time. I have extensively researched various guides on migrating between these technologies, completed the A ...

Receive Angular error messages in another foreign dialect

If I set up an Angular Reactive form to include validation like this this.formGroup = this._formBuilder.group({ formControl: new FormControl(null, [Validators.required]) }); Which means the input is required. When attempting to submit with a blank inpu ...

Modifying the color of a GIF animated image using CSS

I'm currently working on an Angular application and I have a transparent GIF Loader image that I downloaded. I've been trying to figure out how to change the color of the GIF image, but so far, I've only been successful in resizing it. I sea ...

When dynamically selecting an item from a dropdown menu, the object property does not display as expected when using the patchValue

When attempting to set the value for a sort object with specific type and format, I encountered an issue where it was not being rendered. Below is my code snippet using patch to set the value: let arr = <FormArray>this.myForm.controls.users; arr.c ...

Vue - making one element's width match another element's width

Trying to dynamically adjust the innermost element's width to match the outermost element's width with Vue: <div id="banner-container" class="row"> <div class="col-xs-12"> <div class="card mb-2"> <div ...

Trigger a click event to alter the child div elements

I have a <div> that expands when clicked and shrinks back when clicked again. Inside this div are images that should only appear once the div expands. My issue is ensuring that all images except the first one are hidden until the div expands. https: ...

Having trouble removing or adding a class to an HTML element?

I have a collection of three colored buttons. My goal is to allow users to select one button at a time, triggering it to be highlighted while the others are unselected. Each button corresponds to an object with a property called isSelected that can be set ...

What is the best way to switch the direction of the arrows based on the sorting order?

Is there a way to dynamically change the direction of arrows based on sorting, similar to the example shown here? sortingPipe.ts: import { SprBitType } from '../spr-bit-type/sprBitType'; import { Pipe, PipeTransform } from '@angular/core& ...

The case of the elusive Firefox overflow: hidden glitch

Recently, I integrated an image slider into my web design and it displayed perfectly on Internet Explorer and Chrome. However, when viewed on Firefox, I encountered a strange problem where the images in the slider were being pushed towards the right side o ...

Positioning of DIV elements in relation to each other

I'm currently enrolled in Codecademy's HTML & CSS course and I'm finding the topic of positioning a bit perplexing. Here is an example of my HTML file: <!DOCTYPE html> <html> <head> <style> div { pos ...

"Ensure that the horizontal border line is properly aligned with the header div

I have a CSS-defined header with the following properties: .page-header { display: flex; flex-direction: row; justify-content: space-between; align-content: stretch; align-items: center; padding: 5px 5px 5px 20px margin-left: auto; margin-r ...

Having issues with the ASP.NET Web Api and Angular Frontend integration

I recently tried following a guide on ASP.NET Web Api and Angular, which you can find here. While I followed the steps closely, the only difference was that I used .NET version 7 instead of version 6 for the Web Api. When I ran the project for the first ti ...

How to submit a form in Angular2 without reloading the page

I have a straightforward form to transmit data to my component without refreshing the page. I've tried to override the submit function by using "return false," but it doesn't work as expected, and the page still refreshes. Here is the HTML: ...

Guide to optimizing the display of a responsive iframe across various devices such as mobile and desktop

I have implemented the following CSS in an iframe to make it responsive. It works well on desktop browsers, but on mobile browsers, the iframe only displays half of the screen. Can you please review the code below and provide guidance on how to fix this is ...

Injecting pipes into directives in Angular: A guide

I've developed a custom directive that formats input values based on the provided pipe parameter (@Input) in Angular reactive forms. For this functionality, I had to import the necessary pipes (currently just one) and implement a switch mechanism to ...