How can I center align my loader inside app-root in Angular2+?

I've successfully added a basic spinner to my <app-root> in the index.html file. This gives the appearance that something is happening behind the scenes while waiting for my app to fully load, rather than showing a blank white page.

However, I'm struggling to center it vertically on the page without affecting the vertical alignment of the app's contents as they load.

Am I overlooking something?

I considered nesting the spinner within another div and setting a specific height for that container to achieve vertical centering, but it doesn't feel like an elegant solution.

I also explored using:

display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

Here is the Codepen for this approach

However, with the current setup, the positioning seems to be based on the top left corner of the overall div, resulting in an offset to the left and bottom, equal to the size of the spinner.

Here is the Codepen for just horizontal centering

index.html

<!doctype html>
<html lang="en">
<head>
  <style>
    .loader {
      border: 16px solid #f3f3f3;
      border-radius: 50%;
      border-top: 16px solid red;
      width: 120px;
      height: 120px;
      animation: spin 1s linear infinite;
      margin: auto;
    }

    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
  </style>
  <meta charset="utf-8">
  <title>BettingUi</title>
  <base href="./">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700|Roboto:400,500" rel="stylesheet">
</head>
<body>
  <!--<app-root>-->
    <!--
      This will display a loader while the app is loading - in case of slow connection
     We cannot use an Angular component here as it will only be displayed after loading
     all components - so direct HTML here is the best option for this.
    -->
    <div class="loader"></div>
  <!--</app-root>-->
</body>

</html>

UPDATE I did eventually find a solution myself:

display: inline-block;
position: absolute;
top: calc(50% - 60px);
left: calc(50% - 60px);

In this solution, I decided to adjust the position by half of the actual width and height of the spinner, since it was being centered based on the top-left corner of the element's defined square dimensions.

Final version on Codepen

Answer №1

.parent{
  height:90vh;
  display:flex;
  postion:absolute;
}

.loader {
      border: 16px solid #f3f3f3;
      border-radius: 50%;
      border-top: 16px solid red;
      width: 120px;
      height: 120px;
      animation: spin 1s linear infinite;
      margin: auto  ;
    }

    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }

.container {
  
  background: grey;
  margin: auto;
  align-items: center;
}

body {
  background: grey;
}
<div class="parent">
  <div class="loader"></div>
</div>

This approach works effectively to achieve the desired spinner effect. My preferred method involves creating a transparent container (such as .parent in this example) with absolute positioning that covers the visible area, and then placing the spinner inside that container.

It's important to note that having a container <div> is essential for alignment purposes. In Angular2, you can encapsulate the spinner, overlay, and related HTML and styles in a separate component, allowing you to simply include <spinner></spinner> in your root index.html.

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

When using Vuejs2, the list of autosize textareas connected to an expanding array of objects does not properly update their values

After binding https://github.com/jackmoore/autosize to textareas within a v-for loop, I've noticed an unexpected data persistence issue while expanding the array linked to that list: While inputs on the left side move downward as intended, new textar ...

What is the process of obtaining the ID and displaying the subsequent field based on that ID in Ruby on Rails?

I'm utilizing rails4 and I am looking for a more efficient way to display the sub-category list based on the category selected in a form. Currently, I have used JavaScript for this functionality but I believe there may be a better solution. Any sugges ...

Return true for cucumber datatable in typescript without fail

I am facing an issue where the following step definition always returns true even for incorrect data from the dataTable. Can someone assist me in correcting the syntax in TypeScript with Chai assertions? Then(/^Verify the following details in report$/, a ...

Linux web server blocking requests across different domains, even with correct Access-Control-Allow-Origin headers set

I am facing an issue with my Ubuntu web server running a React app. There are two files on the server that handle requests made by the website, but for some reason, clients are unable to make requests to the server. Server.js: const express = require(&apos ...

Display a color picker within an HTML div with fixed positioning to prevent scrolling within the element

Within a div, I have text boxes that trigger a color picker to be displayed when clicked. Currently, the color picker appears at the bottom of the text box, but I would like it to be positioned on the left or top without requiring scrolling. Can anyone a ...

Is there a way to compel the browser to re-cache an image?

In my website, each user has a cover image on their profile page with the constant name main_cover.jpg. The issue I am facing is that when a user uploads a new image, the old image remains cached in the browser and I have to refresh the page (ctrl+f5) to ...

The current issue lies with the lack of functionality in the Ajax post feature

Seeking assistance here. I've encountered a frequent issue on stackoverflow and despite attempting various methods, none seem to be working for me. Could it be that I'm doing something fundamentally wrong? Here is the code snippet in question: $ ...

Struggling to efficiently handle imported JSON data using VUE.JS JavaScript?

Struggling to extract specific information from JSON data that I need to import. Here is the sample data I'm working with: I am trying to extract details like the name, description, and professor for each entry. This is how I'm importing the d ...

Incorporating HTML5 & CSS3: Leveraging various Pseudo classes and the translateX/Y properties

As a graphic designer dabbling in HTML5, I have to admit that coding is not my strong suit. Despite that, I've been able to troubleshoot most of my issues thus far. However, I've hit a bit of a snag. The problem arises when clicking on a thumbna ...

AngularJs tip: Harness the power of parallel and sequential function calls that have interdependencies

service (function () { 'use strict'; angular .module('app.user') .factory('myService', Service); Service.$inject = ['$http', 'API_ENDPOINT', '$q']; /* @ngInject ...

jQuery Mobile - Implementing Persistent Toolbars along with a custom back button functionality

UPDATE Here is a fiddle of the problem: https://jsfiddle.net/9k449qs2/ - attempt to select the header using your picker, but every time you click it will select the whole page instead. I am currently working on a project that includes a persistent header ...

Executing a function by clicking on an element with the `ng-click` directive within a Bootstrap modal

I'm working on an app that allows users to submit posts for review. When a user clicks the add post button, I have a Bootstrap modal pop up to confirm their choice. Within this modal, there is a "confirm" button that should trigger a function. Strang ...

Encountering a 'oembed is null' error in the firebug console while using JQUERY OEMBED

There seems to be an issue with oembed causing an error message in firebug's console window. Specifically, the error is displayed as: oembed is null oembedContainer.html(oembed.code); This error occurs when clicking on a link that leads to the jquer ...

Refresh gif without having to reload it in Internet Explorer 11

I'm attempting to create a feature where a gif restarts when clicked by the user, without needing to reload it (due to the heavy size of the gif which is preloaded for my application). The current code functions flawlessly on Chrome and other "modern ...

Show a loading image after clicking on an image or button using JavaScript

I recently created an App using PhoneGap and JqueryMobile. Transitioning between multiple HTML pages by clicking on images seems to be causing slow loading times, leaving end users unaware of what's happening in the background. I am looking for a way ...

Automating the Process of File Downloads Using Ajax and PHP

I am currently working on a form that requires users to enter a secret key. Once the key is submitted, it is verified against a database. If the key is found, the relevant file associated with the key should be downloaded automatically. I have successfully ...

How to retrieve the values of a property from a JavaScript object

Looking for a fresh perspective on this issue. I'm currently retrieving historical data from the cryptocompare API, but I'm having trouble accessing the received values. https://i.sstatic.net/oH6hz.png When I hardcode it as res.data.OMG.USD, I ...

Which is better: Angular module for each page or component for each page?

We are getting started with Angular 8 and have a vision for a web application that includes: A navigation bar at the top with dropdown menu items leading to different UI pages. The main section will display each page, such as: - Sales section ...

The type of props injected by WithStyles

When working on my class component, I utilize material UI withStyles to inject classes as a property. export default withStyles(styles)(myComponent) In this process, const styles = ( (theme:Theme) => createStyles({className:CSS_PROPERTIES}) I am att ...

Softening the edges of a table

Could you assist me in rounding the corners of my table? I attempted using the basic border-radius, however, it only separated the border at the corners. Do you think this issue is due to my browser (Firefox) or could it be a bug? You can view my Jsfiddl ...