CSS Layout How to - Position div at the center horizontally








Question

We would like to know how to position div at the center horizontally.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
div[class|=ui-grid] {<!--  w  w w  . j av a2 s.c o  m-->
  background: red;
  text-align: center;
  padding: 30px;
}

div[class|=ui-block] {
  background: blue;
  font: 10px Verdana;
  color: #fff;
  padding: 10px;
  vertical-align: middle;
  display: inline-block;
}

.ui-block-a {
  height: 100px;
}

.ui-block-b {
  height: 50px;
}

.ui-block-c {
  height: 200px;
}
</style>
</head>
<body>
  <div class="ui-grid-b">
    <div class="ui-block-a">Block A</div>
    <div class="ui-block-b">Block B</div>
    <div class="ui-block-c">Block C</div>
  </div>
</body>
</html>

The code above is rendered as follows: