CSS Layout How to - Center a text inside a div with an image








Question

We would like to know how to center a text inside a div with an image.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
figure {<!--from www  . j a  va2 s.c o  m-->
  border: 1px solid black;
  display: inline-block;
  text-align: center;
  padding: 20px;
}

figure img {
  float: left;
  height: 126px;
}

figcaption {
  float: right;
  height: 126px;
  line-height: 126px;
  margin-left: 50px;
}
</style>
</head>
<body>
  <figure>
    <img src="http://www.java2s.com/style/download.png">
    <figcaption>A Disk</figcaption>
  </figure>
</body>
</html>

The code above is rendered as follows: