CSS Layout How to - Make image cover div








Question

We would like to know how to make image cover div.

Answer


<!DOCTYPE html>
<html>
<head>
<title>Image wrapper</title>
<style type='text/css'>
#wrapper {<!--from  www  .j a  v a2 s  . c o m-->
  width: 60%;
  background-color: #aaa;
}

#wrapper img {
  width: 100%;
  border: 1px dashed red;
  display: table;
}
</style>
</head>
<body>
  <div id="wrapper">
    <img
      src="http://placehold.it/200x200" />
  </div>
</body>
</html>

The code above is rendered as follows: