What is the best way to centrally position a <mat-card> on the screen using Angular 9?

* I'm struggling to center a <mat-card> on the screen, specifically within the toggle-device-toolbar. It's intended for mobile phones and I could use some guidance. I'm working with angular-material and ng-bootstrap*

Here is the code snippet for the <mat-card>

<div class="container ">
    <mat-card>
    <mat-radio-group aria-label="Select an option" [(ngModel)]="radio_btn">
        <mat-radio-button  [value]="true" >Admin</mat-radio-button>
        <mat-radio-button [value]="false">User</mat-radio-button>
    </mat-radio-group>   
        <div class="row justify-content-center" *ngIf="radio_btn==true;else form2">
            <form class="example-form " [formGroup]="loginForm" (ngSubmit)="send()">
                <mat-form-field class="example-full-width">
                    <input matInput formControlName="Identifier" placeholder="User" >
                </mat-form-field><br>
                <div *ngIf="loginForm.get('Identifier').hasError('Identifier') && loginForm.get('Identifier').touched">Introduce an email</div><br>
                <mat-form-field class="example-full-width">
                    <input matInput formControlName="Password" placeholder="Password" type="password">
                </mat-form-field><br>
                <div *ngIf="loginForm.get('Password').hasError('Password') && loginForm.get('Password').touched">Introduce the correctly password</div><br>
                <button mat-raised-button [disabled]="loginForm.invalid" class="colour_button " type="submit">Login 1</button>
            </form>
        </div>
</mat-card>
</div>

Here is the current appearance:

https://i.sstatic.net/57Q0Z.png

Answer №1

If you're looking to achieve this layout, consider using Flexbox techniques as outlined here.

To style the component accordingly, include the following code:

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

You can also swap out grid for flex and it should still function correctly:

.container {
  display: grid; 
  align-items: center; 
  justify-content: center;
}

Answer №2

Here is my solution

This is the CSS code I used


.center{
    width: 75%;
    margin: 10px auto;
  }

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

Below is the HTML code snippet

<div class="main-div">
    <mat-card class="z-depth center" flex="50" >
        <mat-radio-group aria-label="Select an option" [(ngModel)]="radio_btn">
            <mat-radio-button [value]="true">Admin</mat-radio-button>
            <mat-radio-button [value]="false">User</mat-radio-button>
        </mat-radio-group>   
            <div *ngIf="radio_btn == true; else form2">
                <form class="example-form " [formGroup]="loginForm" (ngSubmit)="send()">
                    <mat-form-field class="example-full-width">
                        <input matInput formControlName="Identifier" placeholder="User">
                    </mat-form-field><br>
                    <div *ngIf="loginForm.get('Identifier').hasError('Identifier') && loginForm.get('Identifier').touched">Please enter an email</div><br>
                    <mat-form-field class="example-full-width">
                        <input matInput formControlName="Password" placeholder="Password" type="password">
                    </mat-form-field><br>
                    <div *ngIf="loginForm.get('Password').hasError('Password') && loginForm.get('Password').touched">Please enter the correct password</div><br>
                    <button mat-raised-button [disabled]="loginForm.invalid" class="colour_button" type="submit">Login</button>
                </form>
            </div>
    </mat-card>
  </div>

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

What is the method to activate a click event on an input file when a button is clicked in Angular 2?

<input type="file" accept="image/*"> <button>Upload file</button> Is there a way to activate the input type=file click event using the button's click event in Angular 2? ...

Modifying the border hue of Material-UI's Select Component

Here is the Select component I've created using materials-UI <FormControl variant="outlined"> <Select value={this.state.value} onChange = {this.handleChange} className={this.props.classes.select} inputProps = {{class ...

Do you think there might be an issue with the CSS coding?

I have designed a user-friendly responsive login form that allows users to easily log in and reset their passwords. However, I am facing an issue where the username and password fields are not displaying on separate lines as intended. I have thoroughly ins ...

What is the best way to change styles in CSS for parent and child elements using selectors?

Hey there! I am currently working on customizing the navigation menus in WordPress and here is the HTML code for the navigation menus: <nav id="primary-navigation" class="site-navigation primary-navigation" role="navigation"> <h1 class="menu- ...

Styling the background of a container in CSS using Bootstrap

Currently, I am utilizing bootstrap and trying to find an elegant solution for adding a background to my container. Specifically, I am working with the bootstrap class container, as opposed to container-fluid. The official Bootstrap documentation advises ...

What is the reason for the lack of variable assignment within the forEach method in Angular?

I am struggling with assigning a value to the variable "this.qdDias" and returning it. After using subscribe, I am unable to retrieve the value at the end of the method. Despite seeing the value in the console.log(this.qdDias), it becomes undefined when re ...

Reset all divs to their default state except for the one that is currently active using jQuery

Here's the plan for my script: Once a user clicks on a "learn more" button, it will reveal the relevant information in the corresponding box. I'm aiming to implement a feature where if you click on another "learn more" button while one box is a ...

Testing a reusable component in Angular using unit testing techniques

Currently, I am creating a test for an AppleComponent which has the following type: <T,U extends BananaComponent<T>>. This component also contains BananaComponent<T>. Target Component export class AppleComponent<T,U extends BananaCom ...

Tips for verifying error messages in angular for nested form group fields

Hey there, I'm new to Angular and I'm working on implementing form validations. Below is the code I'm using and encountering an issue with nested form group fields. The error I'm getting in the log is "TypeError: Cannot read property &a ...

Chrome causing a background-size cover issue

I encountered an issue with this code. It runs smoothly on Firefox 50, but encounters failures on Chrome Version 54.0.2840.99 m (64-bit) on Windows. .test { background: url(datas/images/3.jpg) no-repeat center center fixed; background-size: cover ...

Numerous Progressive Web Apps within one domain

Can I host several PWAs on the same endpoint? For example, if I have three different PWAs under one root, can the root host these PWAs in a way that switching between them via links or menus does not open a new window? Currently, when I click on a differen ...

Strange spacing below body element discovered while browsing website in Firefox 7

Currently, I am facing an issue on my website in Firefox 7. A margin is causing the gradient in #wrapper to shift upwards from the bottom, disrupting the layout. Despite resetting the margin to 0 on body and html, I am unable to pinpoint the root of the pr ...

How to easily switch between multiple side navigation menus using Angular 4

Below is the structure of my components: app --app.component.html --app.component.ts -dashboard --dashboard.component.html --dashboard.component.ts -sales --sales.component.html --sales.component.ts -taxreports --taxreports.c ...

Stylish News Carousel using CSS and HTML

I've been working on creating a news slider for my website, but I'm encountering an issue. Despite completing the HTML and CSS, I'm struggling to make the 'showcase' rotate using JQuery. I've tried various guides and instructi ...

Learn how to set up browser targeting using differential loading in Angular 10, specifically for es2016 or newer versions

Seeking advice on JS target output for compiled Angular when utilizing differential loading. By default, Angular compiles TypeScript down to ES5 and ES2015, with browsers using either depending on their capabilities. In order to stay current, I've b ...

Having trouble with an unusual Protractor issue: Jasmine test cases not working properly?

UPDATE: The question was modified on September 17, 2018 Whenever my test case passes, everything looks good on the output screen as it displays 'passed'. However, when it fails, I encounter a lengthy red error message that seems to indicate an i ...

Unable to find values for all parameters on the ContactEdit page

While exploring the functionality of local storage, I encountered an error when trying to inject it into my constructor: 'Can't resolve all parameters for ContactEdit page ?, [object Object], [object Object], [object Object], [object Object] ...

Is there a way to display multiple Bootstrap 5 modals simultaneously without automatically closing the previous ones?

My current project utilizes bootstrap 5.2, which is a departure from the previous version, bootstrap 3. In bootstrap 3, it was possible to open multiple modals on top of each other, creating an overlapping effect. However, in bootstrap 5 and 5.2, the behav ...

Utilize JavaScript to implement CSS using an "if" statement

I am currently working on implementing iOS web app properties into my website. My goal is to create a <div> at the top of the page and apply specific CSS styles when a certain condition is met in a JavaScript script. Unfortunately, I am facing issue ...

Executing a function to erase the stored value in local storage during an Angular unit test

Looking to verify whether the localStorage gets cleared when I execute my function. Component ngOnInit() { // Logging out when reaching login screen for login purposes this.authService.logout(); } authService logout() { // Removing logged i ...