HTML Element Style How to - Adjust image size based on the font size








Question

We would like to know how to adjust image size based on the font size.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
body {<!--from   w  ww.j a va 2s  .c  o m-->
  font-size: 62.5%; /*sets 1em to 10px for convinience*/
}

p {
  font-size: 3em;
}

p img {
  height: 1em;
  width: auto;
}
</style>
</head>
<body>
  <p>
    Hello world! <img src="http://www.java2s.com/style/download.png"
  </p>
</body>
</html>

The code above is rendered as follows: