HTML Element Style How to - Fit image to container








Question

We would like to know how to fit image to container.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.layout-a {<!--from w  w  w  .  ja v  a  2 s. c  o  m-->
  width: 360px;
  margin: 0 auto;
  text-align: center;
}

.layout-a img {
  width: 100%;
}

.layout-a p {
  text-align: left;
}
</style>
</head>
<body>
  <div class="layout-a">
    <img src="http://placehold.it/200x200" />
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla
      ut nibh tincidunt, tempus ligula vitae, semper turpis.</p>
  </div>
</body>
</html>

The code above is rendered as follows: