HTML Element Style How to - Automatic image stretch/squeeze/crop in both directions








Question

We would like to know how to automatic image stretch/squeeze/crop in both directions.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
#container-DIV {<!--from   w  w w  .  j a v a2 s.co m-->
  width: 80%;
  height: 400px;
  background-image:url('http://www.java2s.com/style/download.png');
  background-repeat: no-repeat;
  background-size: cover;
}
</style>
</head>
<body>
  <div id="container-DIV"></div>
</body>
</html>

The code above is rendered as follows: