CSS Layout How to - Center span in div








Question

We would like to know how to center span in div.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#container {<!--from   www  .ja v a 2s . c om-->
  display: table-cell;
  vertical-align: middle;
  border: 1px solid red;
  height: 300px;
  background-color: green;
  width: 400px;
  text-align: center;
  line-height: 300px; /* Same as height */
}

#container>span {
  background-color: lightblue;
  height: 50px;
  width: 100px;
  line-height: normal;
  display: inline-block;
  vertical-align: middle;
}
</style>
</head>
<body>
  <div id="container">
    <span>content</span>
  </div>
</body>
</html>

The code above is rendered as follows: