2

How can I set a CSS property on the html element with jQuery?

I'm trying to do something like...

$('html').css({'background': 'url(/images/bg.png) top center'})

EDIT: For those that will ask why not just set it on the body or some other work around, this needs to be set like this as it fixes a webkit bug in iOS 4

EDIT 2: I guess this is doing what it should already. It must be something with the finicky bug that is causing this to act differently then when I set it via external CSS, strange

9
  • Isn't background CSS properties supposed to go in body rather than html? Commented Nov 3, 2011 at 22:46
  • Try setting it on the body, not the html! Commented Nov 3, 2011 at 22:47
  • read edit! i asked this question for a reason! Commented Nov 3, 2011 at 22:47
  • I use styles (including background) on <html> all the time, have no idea what these guys are talking about. This should already work fine, your image path is probably wrong. Commented Nov 3, 2011 at 22:48
  • 2
    @fancy, why dance all around this issue? You posted valid code that should be working and talk about working around a Webkit bug. Please, for the readers' benefit, explain this Webkit bug so we can get to the root of the issue. Commented Nov 3, 2011 at 23:03

3 Answers 3

1

Simply like that:

$('html').css('background', 'url(/images/bg.png) top center')

Sign up to request clarification or add additional context in comments.

Comments

0

Try the following

$('theSelector').css('background', 'url(/images/bg.png) top center');

For the theSelector portion you can use the most appropriate jQuery selector for your situation

Comments

0

There's nothing syntactically incorrect about your jQuery code.

EDIT: Try setting it to the body and the html:

$('html, body').css({'background': 'url(/images/bg.png) top center'})

If you're only doing one change, it's best to use the singular form:

$('html, body').css('background', 'url(/images/bg.png) top center')

2 Comments

Those are two different elements, and both can have backgrounds. There's no reason you can't style the <html> element.
Clearly it's not working for him. Hence the suggestion to try setting both html and body. I don't see a need to vote this down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.