Angular 2 Material - How To Center Progress Spinner

user2085143 picture user2085143 · Dec 29, 2016 · Viewed 76.5k times · Source

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.

Answer

Andriy picture Andriy · Dec 29, 2016

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 4 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>

  • SAME HEIGHT AND LINE HEIGHT + TEXT ALIGN

.wrapper {
  line-height: calc(100vh - 20px);
  height: calc(100vh - 20px);
  text-align: center;
  background: red;
}
.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>