HTML Element Style How to - Fit images (both landscape and portrait) into square thumbnail








Question

We would like to know how to fit images (both landscape and portrait) into square thumbnail.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.thumb {<!--from w  w w  . j  a v  a2  s.c  o m-->
  width: 100px;
  height: 100px;
  background: center;
  background-size: cover;
}
</style>
</head>
<body>
  <div class="thumb"
    style="background-image: url('http://placehold.it/250x500')"></div>
  <br />
  <div class="thumb"
    style="background-image: url('http://placehold.it/500x200')"></div>
  <br />
  <div class="thumb"
    style="background-image: url('http://placehold.it/100x200')"></div>
</body>
</html>

The code above is rendered as follows: