HTML Element Style How to - Show only part of an image








Question

We would like to know how to show only part of an image.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
div {<!-- w ww. jav  a  2 s. c om-->
  width: 200px;
  background: url(http://www.java2s.com/style/download.png) no-repeat;
}

.bg-2 {
  height: 150px;
}
</style>
</head>
<body>
  <div class="bg-1">Lorem ipsum.</div>
  <br>
  <div class="bg-2">Lorem ipsum.</div>
</body>
</html>

The code above is rendered as follows: