0

I'm using Typescript, React.js and Bootstrap 3.

I've got an overlay div that I position over the top of my detail content while I'm loading data from the server.

In the overlay, I've got an animated spinner, implemented as a span/glyphicon.

In the following code, the textAlign works, but the verticalAlign doesn't.

Can anyone tell what's wrong with this - what do I need to do to make the spinner be centered vertically as well as horizontally?

let loadingOverlay = <div style={{
  position: 'absolute',
  opacity: 0.7,
  width: '100%',
  height: '100%',
  color: 'white',
  backgroundColor: 'DarkGrey',
  zIndex: 1,
  border: 0, padding: 0, margin: 0,
  textAlign: 'center',
  verticalAlign: 'middle',

}}>
  <span 
    className="glyphicon glyphicon-refresh"
    style={{
      fontSize: 50,
      animation: 'spin 1s infinite linear'
    }}
  />
</div>;

let detailPanel = <div className="well" style={{margin: 2, padding: 5}}>
  <div style={{position: 'relative'}}>
    {workingStatus && loadingOverlay}
    {detailForm}
  </div>
  ...
</div>;
Shorn
  • 19,077
  • 15
  • 90
  • 168
  • from my experience, vertical align only seems to work with `display: table-cell` – A. L May 09 '17 at 07:12
  • May I suggest check my answer in this question: http://stackoverflow.com/questions/41721586/how-do-i-responsively-center-text-inside-of-a-div/41721874#41721874? May it helps... – Kenzo_Gilead May 09 '17 at 07:16

2 Answers2

2

You can use flexbox for this

let loadingOverlay = <div style={{
  position: 'absolute',
  opacity: 0.7,
  width: '100%',
  height: '100%',
  color: 'white',
  backgroundColor: 'DarkGrey',
  zIndex: 1,
  border: 0, padding: 0, margin: 0,
  textAlign: 'center',
  verticalAlign: 'middle',
  display:flex;
  align-items: center;
  justify-content: center;

}}>
  <span 
    className="glyphicon glyphicon-refresh"
    style={{
      fontSize: 50,
      animation: 'spin 1s infinite linear'
    }}
  />
</div>;

let detailPanel = <div className="well" style={{margin: 2, padding: 5}}>
  <div style={{position: 'relative'}}>
    {workingStatus && loadingOverlay}
    {detailForm}
  </div>
  ...
</div>;
Super User
  • 9,448
  • 3
  • 31
  • 47
1

You can use top: 50% and transform: translateY(-50%) to get the icon centered vertically. Like this:

let loadingOverlay = <div style={{
  position: 'absolute',
  opacity: 0.7,
  width: '100%',
  height: '100%',
  color: 'white',
  backgroundColor: 'DarkGrey',
  zIndex: 1,
  border: 0, padding: 0, margin: 0,
  textAlign: 'center',

}}>
  <span 
    className="glyphicon glyphicon-refresh"
    style={{
      fontSize: 50,
      animation: 'spin 1s infinite linear',
      top: '50%',
      transform: 'translateY(-50%)'
    }}
  />
</div>;

let detailPanel = <div className="well" style={{margin: 2, padding: 5}}>
  <div style={{position: 'relative'}}>
    {workingStatus && loadingOverlay}
    {detailForm}
  </div>
  ...
</div>;

You can see a working html demo below. I've removed the inline styles and added id's for readability:

#outer {
  position: relative;
  height: 200px;  /*added for demo*/
}

#inner {
  position: absolute;
  opacity: 0.7;
  width: 100%;
  height: 100%;
  color: white;
  background-color: DarkGrey;
  z-index: 1;
  border: 0;
  padding: 0;
  margin: 0;
  text-align: center;
}

#inner span {
  font-size: 50;
  animation: spin 1s infinite linear;
  top:50%;
  transform: translateY(-50%);
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div id="outer">
  <div id="inner">
    <span class="glyphicon glyphicon-refresh"></span>
  </div>
</div>
Chris
  • 57,622
  • 19
  • 111
  • 137