jQuery Style How to - Change the background image size








Question

We would like to know how to change the background image size.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.10.1.js'></script>
<style type='text/css'>
body {<!--from w w w.j a v a 2 s .  c o  m-->
  background: url('http://www.java2s.com/style/download.png') no-repeat;
}
</style>
<script type='text/javascript'>
$(function(){
    ratio = 0.2;
    $("body").css({
        "background-size": 1046 * ratio,
        "-webkit-background-size": 1046 * ratio+'px auto',
    });
});
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: