Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Material - How To Center Progress Spinner

I have implemented the Angular 2 progress spinner from the below link

https://github.com/angular/material2/tree/master/src/lib/progress-spinner

I would like to have it centered, however, the only way I can seem to get it to work is to remove the

display: block 

from the CSS. However, this causes the spinner to appear huge on the page.

Any advice would be great.

like image 237
user2085143 Avatar asked Dec 29 '16 22:12

user2085143


People also ask

What is the use of spinner in angular?

Ngx spinner is a library for loading spinner for Angular, which is meant to inform the user that data loading is in progress. Basically Loader is used to show the loading state of data in application.


2 Answers

just add margin rule:

<md-progress-spinner style="margin:0 auto;" 
                     mode="indeterminate"></md-progress-spinner>

plunker: http://plnkr.co/edit/sEiTZt830ZE7rqjq9YXO?p=preview

UPDATE

Just wanted to share and demonstrate 5 other general centering solutions

  • FLEX:

.wrapper {
  display: flex; 
  justify-content: center; 
  align-items: center;
  height: calc(100vh - 20px);
  background: red;
}
.inner {
  background: green;
  color: white;
  padding: 12px;
}
<div class="wrapper">
  <div class="inner">INNER CONTENT</div>
</div>
  • GRID:

.wrapper {
  display: grid; 
  place-items: center;
  height: calc(100vh - 20px);
  background: red;
}
.inner {
  background: green;
  color: white;
  padding: 12px;
}
<div class="wrapper">
  <div class="inner">INNER CONTENT</div>
</div>
  • LINE HEIGHT + TEXT ALIGN (will not work as desired for multiple lines, use white-space: nowrap; to ensure one line)

.wrapper {
  line-height: calc(100vh - 20px);
  text-align: center;
  background: red;
  white-space: nowrap;
}
.inner {
  background: green;
  color: white;
  padding: 12px;
  display: inline;
}
<div class="wrapper">
  <div class="inner">INNER CONTENT</div>
</div>
  • USING ABSOLUTE, TOP, LEFT and TRANSFORM TRANSLATE

.wrapper {
  height: calc(100vh - 20px);
  background: red;
  position: relative;
}
.inner {
  background: green;
  color: white;
  padding: 12px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div class="wrapper">
  <div class="inner">INNER CONTENT</div>
</div>
  • USING ABSOLUTE, TOP, LEFT, BOTTOM, RIGHT and MARGIN AUTO (mentioned by György Balássy). Note: inner div width needs to be set.

.wrapper {
  height: calc(100vh - 20px);
  background: red;
  position: relative;
}
.inner {
  background: green;
  color: white;
  padding: 12px;
  position: absolute;
  inset: 0;
  margin: auto;
  height: max-content;
  width: max-content;
}
<div class="wrapper">
  <div class="inner">INNER CONTENT</div>
</div>
like image 152
Andriy Avatar answered Oct 19 '22 17:10

Andriy


This CodePen helped me to create a page-centered spinner with Material Design in Angular 4: https://codepen.io/MattIn4D/pen/LiKFC

Component.html:

<div class="loading-indicator">
  <mat-progress-spinner mode="indeterminate" color="accent"></mat-progress-spinner>
</div>

Component.css:

/* Absolute Center Spinner */
.loading-indicator {
  position: fixed;
  z-index: 999;
  height: 2em;
  width: 2em;
  overflow: show;
  margin: auto;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}

/* Transparent Overlay */
.loading-indicator:before {
  content: '';
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.3);
}
like image 54
György Balássy Avatar answered Oct 19 '22 16:10

György Balássy