Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Writing CSS with JavaScript

Currently this is what my css looks like in JavaScript:

  $('body').css({
    'background-image' : "url('" + backgrounds[currentBackground] + "')"
  });

I want to add a few more in there so it looks like this css:

body{
background-image: url('') no-repeat center center fixed;                 background-size: cover;
}

So how would I write all that in the javascript css I have already written?

3 Answers

You could do something like this:]

$('body').css({"background": "url('http://www.worldswallpapers.org/wp-content/uploads/2014/02/Nature-Wallpapers-2014-1.jpg') no-repeat center center fixed", "background-size": "cover"});

You just have to put in whatever dynamic way it looks like you're generating a background image. I just used a static one.

I will say this though, I'd set as many of these CSS elements in CSS as possible for maintainability reasons. I get to do dynamic backgrounds you have to use Javascript/Jquery, but the rest I'd at least considered limiting your inline CSS to as few as possible.

http://jsfiddle.net/eA6Ek/ (Codepen is broken right now )

As Kevin noted, you have to switch to the shorthand background property.

I agree with Kevin that if the other properties aren't dynamic then move those styles to your css file.

To integrate with your existing code you could do:

$('body').css({'background' : "url('" + backgrounds[currentBackground] + "') center center / cover no-repeat fixed"
  });

You can also include the background-size property in the shorthand but it must come after the position and the two have to be separated with a forward slash.

The jQuery css method accepts an object of property-value pairs as its parameter:

$('body').css({
   'background' : "url('" + backgrounds[currentBackground] + "') no-repeat center center fixed",
   'background-size': 'cover'
});

You can even expand it to something like this, if you want to define every property separately:

$('body').css({
   'background-image' : "url('" + backgrounds[currentBackground] + "')",
   'background-position': "center center",
   'background-repeat': "no-repeat",
   'background-attachment': 'fixed',
   'background-size': 'cover'
});

Hi Dino,

I think your first code sample wouldn't be valid since you're using background-image instead of background

Right you are. Serves me right for pasting instead of typing. :)

Thanks Kevin! That worked. I generally would write this in my css for the background, but I am having the body background image loop through images, so I needed it set in the JS. Though I probably could put the background-size in the css since that stays the same through each bg image. Thanks again!