HTML Element Style How to - Center div with background








Question

We would like to know how to center div with background.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
div {<!--from   w  w w.j av  a2 s.  co m-->
  width: 500px;
  margin: 0 auto;
  background: #eee;
}
</style>
</head>
<body>
  <div>hello</div>
</body>
</html>

The code above is rendered as follows: