Cojocari Cristian Cojocari Cristian - 1 year ago 64
Javascript Question

Jquery .css is not working,css,js,console,output

I'm trying to set the body to my window's height and weight, but I tried a lot of times to do it. I don't know what's wrong, I did the console.log(width + ' ' + height) and I got the datas, but I can't add that property to css.
What am I doing wrong?

var width = $(document).width();
var height = $(document).height();
$('body').css('background-size', width + 'px' + ' ' + height + 'px');

body {
background-size: 20px 20px;

<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>


Answer Source

Could you not just do...

var width = $(document).width();
var height = $(document).height();

If you want the body to be the same size as the window size.

If you want the background size to be the same size as the window, then your current code is working. If you want your code to run everytime the user changes the windows size you can wrap this function around your code:

$(window).resize(function () { 
    //code goes here 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download