Tips for aligning mat-card in the center using Angular Material

My login card, designed with Angular materials, seems to be sticking to the left side of the page when I view it.

I've tried using flex and layout-align="center center" but can't seem to get the card centered on the page.

What could I be doing wrong?

Here is the HTML code I am using:

<mat-card class="example-card" layout="row" layout-align="center center">
  <mat-card-header>
    <div class="login-box-header" layout="row" layout-align="center center">
        <img src="https://image.ibb.co/hDqa3p/codershood.png">
    </div>
  </mat-card-header>
  <mat-card-content>
    <form class="example-form">
      <table class="example-full-width" cellspacing="0">
        <tr>
          <td>
            <mat-form-field class="example-full-width">
            <input matInput placeholder="Username" [(ngModel)]="username" name="username" required>
            </mat-form-field>
          </td>
        </tr>
        <tr>
        <td><mat-form-field class="example-full-width">
          <input matInput placeholder="Password" [(ngModel)]="password"type="password" name="password" required>
        </mat-form-field></td>
      </tr></table>
    </form>
    <mat-spinner [style.display]="showSpinner ? 'block' : 'none'"></mat-spinner>
  </mat-card-content>
  <mat-card-actions>
    <button mat-raised-button (click)="login()" color="primary">Login</button>
  </mat-card-actions>
</mat-card>

However, the content still remains off-center:

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

Answer №1

Resolved the issue using a straightforward Flex solution

.main-div{
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

and encapsulating everything with a specific class

<!-- Adding classes main-div and example-card in the .css file -->
<div class="main-div">

<mat-card class="example-card">
  <mat-card-header>
    <div class="login-box-header">
        <!-- Temporary image hosted on image.ibb-->
        <img src="https://image.ibb.co/hDqa3p/codershood.png">
    </div>
  </mat-card-header>
  <mat-card-content>
    <form class="example-form">
      <table class="example-full-width" cellspacing="0">
        <tr>
          <td>
            <mat-form-field class="example-full-width">
            <input matInput placeholder="Username" [(ngModel)]="username" name="username" required>
            </mat-form-field>
          </td>
        </tr>
        <tr>
        <td><mat-form-field class="example-full-width">
          <input matInput placeholder="Password" [(ngModel)]="password"type="password" name="password" required>
        </mat-form-field></td>
      </tr></table>
    </form>
    <mat-spinner [style.display]="showSpinner ? 'block' : 'none'"></mat-spinner>
  </mat-card-content>
  <mat-card-actions>
    <button mat-raised-button (click)="login()" color="primary">Login</button>
  </mat-card-actions>
</mat-card>

Answer №2

Consider the following CSS methods:

  1. The first approach is to use the following code:
.example-card{
    display:inline-block;
}

Next, create a cover div for "mat-card" with a class name of "mat-card-cvr".

.mat-card-cvr{
   width: 100%;
   text-align:center;
}

2. The second method involves using the following style rules:

.example-card{
    margin: 0 auto;
    width: 40%;
}
  1. The third approach requires the following CSS code:
.example-card{
    margin: 0 auto;
    width: 40%;
    display: inline-block;
    vertical-align: middle;
}

After that, create a cover div for "mat-card" with the class name "mat-card-cvr".

.mat-card-cvr{
    width: 100%;
    height: 500px;
    line-height: 500px;
    text-align: center;
}
  1. Finally, the fourth method involves the following styles:
.mat-card-cvr{
    top:50%;
    width:100%;
    text-align:center;
    position:fixed;
}
    
.example-card{
    margin:0 auto;
    display:inline-block;
}

Answer №3

To easily organize rows and columns, utilize the provided mat-grid-list material. Implement it in your code like this:

<mat-grid-list cols="1" rows="1" rowHeight="2:1">
    <mat-grid-tile>
        <mat-card>
        Insert your form content here
        </mat-card>
    </mat-grid-tile>
</mat-grid-list>

For further details, refer to the documentation https://material.angular.io/components/grid-list/overview

Answer №4

Witness the magic:

.logo-center{
   margin: 20px auto;
   display: block;
}

Answer №5

I attempted the solution suggested by OP, but I also needed to manage the width, so I ended up using nested div elements along with a mat-card component.

.centering-div{
  display: flex;
  justify-content: center;
}

.inner-container {
  width: 80%;
  max-width: 100%;
  overflow: auto;
}
.my-card {
  max-width: 100%;
  color: rgb(63, 81, 181);
}

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

Utilizing long polling to retrieve information from the database and present it on the webpage

I need a way to fetch the top three values from the database and have them displayed on the page. The content should be refreshed every hour. invite.php: <html> <head></head> <body> <div class="page- ...

How to create a full-page background image using Bootstrap?

Take a look at this example: In the provided example, there is a background landing page that expands to fit the width of the viewport, remains responsive, and does not take up the full width of the page. How can you utilize bootstrap to manually code an ...

What is the best way to transfer array information from an ASP.NET page to JavaScript code?

i am using a chart template that includes javascript code, and here is an excerpt from it: // Toggling Series var datasets = { "Iran": { label: "IR", data: [ [1988, 483994], [1989, 4790 ...

The data from the Subscribe API call is gradually loading within the ngOnInit() function

When using Angular 8, I am experiencing slow data retrieval when making API calls in the ngOnInit() function. The issue arises when trying to pass this data as @Input from one component module to another - it initially comes through as undefined for a minu ...

Angular ng2-chart: Donut chart [plugins] is not a recognized property of 'canvas' TEXT CENTER

Using Angular Core 5.1.6 and ng2-chart 1.6.0 in Visual Studio 2019 Community edition has been causing some issues for me. The code works perfectly fine in StackBlitz, but when I try to transfer it to my project, it starts complaining about plugins. Removin ...

Retrieve the dimensions of an image once rendering is complete, using Angular

I'm currently working on obtaining the rendered size of an image within a component. By utilizing the (load) event, I can capture the size of the image as it appears at that particular moment (pic1), as well as its "final" size after the page has fini ...

Responsive image gallery using css grid

I am facing a challenge creating a responsive CSS grid image gallery with 18 divs inside a container in my HTML code. Each div has its own unique class. My first issue arises when I attempt to span the first image horizontally using a class called .vertica ...

Creating a treeview that loads data on demand: A step-by-step guide

Is it possible to create a tree structure that loads data on demand without using external libraries? I have a backend with methods for fetching root level nodes and children of parent nodes. The goal is to build a tree where clicking on a node triggers ...

Updating a subscribed observable does not occur when pushing or nexting a value from an observable subject

Help needed! I've created a simple example that should be working, but unfortunately it's not :( My onClick() function doesn't seem to trigger the console.log as expected. Can someone help me figure out what I'm doing wrong? @Component ...

Understanding the res.render method in JavaScript can be a bit tricky at first

In my spare time, I have been immersing myself in coding lessons and have encountered some puzzling aspects of the code: Firstly, there is a confusion surrounding the action attribute in HTML Secondly, this particular piece of code is causing me some b ...

Move the cursor over the text to reveal an image

Hello, I'm trying to replicate the same animation effect as seen on these websites: and . Specifically, when hovering over the "selected works" section, an image is displayed. I suspect it's using a JavaScript library, but I can't seem to i ...

To create a complete layout without relying on fixed positioning, ensure that the header, container, and footer collectively

Is there a method to ensure the header, container, and footer encompass the entire layout without using fixed positioning in CSS? Check out this HTML fiddle for reference. <div class="wrapper"> <header> <img src="https://www.ecobin.co ...

Maintain the same javascript variable throughout multiple pages

I am working on a project that involves utilizing a database along with 2 drop down menus. Through the use of JavaScript, I am able to capture the value selected from the drop down menus and then pass it on to my PHP script. The PHP script retrieves releva ...

Incorporating a slider into an HTML website

I'm currently working on designing a web page and I want to include a sliding input feature, although I am not entirely sure if it is referred to as a slider. ...

html table layout disrupted due to immovable column

I am facing a challenge in creating a table where the first column is fixed while the rest of the columns are scrollable horizontally. Although I was able to implement this successfully, it does not seem to work when integrated into my website. Whenever ...

A visual element positioned centrally within a row, directly beneath a paragraph tag

Every solution I attempt fails to work correctly, at times I achieve centering the image but then the figcaption ends up crammed into a small corner. <!DOCTYPE html> <html lang="en> <head> <meta charset="UTF-8> <link r ...

What is the best way to determine the quantity of identical items in the django templating language?

My goal is to create a table that organizes items by type, all retrieved from my database. Currently, I can display the items and their corresponding types without any issues in a table format. However, I want to avoid repeating the same type multiple time ...

The <hr> line in Bootstrap is placed in a peculiar manner

Take a look at the fiddle here: https://jsfiddle.net/mv5ut6sw/ On my website, I have a set of links displayed in a <div> at the top. To visually separate these links from the rest of the page, I want to add horizontal lines above and below the conta ...

Conceal the Button when the TextBox does not contain valid input

I'm trying to create a textbox with an email pattern that hides a span (click) if the pattern is invalid. I have the following code snippet in place, but it doesn't seem to work as expected: <input type="text" placeholder="Signup for Mailin ...

Failing Cypress component test: When leveraging an index.ts file for importing and exporting services

Tech stack: Angular v15 and Cypress v12. My example component that I'm testing: import { Component } from '@angular/core'; import { UserHttp } from '../../services'; @Component({ selector: 'example-view', templateUr ...