HTML Element Style How to - Position div to stay in center of visible screen








Question

We would like to know how to position div to stay in center of visible screen.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.white_content {<!--   w  ww .  jav  a 2s  . co  m-->
  position: fixed;
  top: 50%;
  left: 50%;
  width: 562px;
  height: 380px;
  margin: -190px 0 0 -281px;
  z-index: 1002;
  overflow: hidden;
  background-color: red;
}
</style>
</head>
<body>
  <div class="white_content"></div>
</body>
</html>

The code above is rendered as follows: