18

Eventually, I wish to dynamically alter gradients based on different things, but how do I get jquery to apply a css3 gradient?

 //works
 $(element).css("background-image", "url(http://www.google.co.uk/images/logos/ps_logo2.png)");  

 //doesn't work
 $(element).css("background-image","-webkit-gradient(linear,left bottom,right bottom,color-stop(0.50, rgb(194,231,255)),color-stop(0.50, rgb(255,255,255))");

 //doesn't work
 $(element).css("background-image","-moz-linear-gradient(left center,rgb(194,231,255) 28%,rgb(255,255,255) 28%");

What am I missing? I've also tried

 $(element).css({background: "-webkit-gradient(linear,left bottom,right bottom,color-stop(0.50, rgb(194,231,255)),color-stop(0.50, rgb(255,255,255)"});

Are these approaches not valid?

3
  • 2
    Your CSS doesn't work when applied directly either. Commented May 16, 2011 at 12:03
  • Possible duplicate? stackoverflow.com/questions/5735521/jquery-css-gradient/5735588 Commented May 16, 2011 at 12:04
  • Note also that if you're trying to apply CSS styles with invalid whitespace in them, they may simply not apply (or at least this is what happened for me in Chrome.) In which case you'll want to go through and remove all extra whitespace in the inspector until the gradient updates, and then update your javascript accordingly Commented Mar 25, 2020 at 20:07

6 Answers 6

22

Your second approach looks OK... Maybe you need to css styles for non-webkit browsers as well... Cross-Browser CSS Gradient

This works for me in Chrome

$('#block').css({
    background: "-webkit-gradient(linear, left top, left bottom, from(#ccc), to(#000))" 
});

Also have a look at: http://www.colorzilla.com/gradient-editor/

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

Comments

4

Another option is to use the jQuery addClass method. This allows you to dynamically add and remove classes and therefore any formatting associated with that class including gradients.

Comments

3

Here is a small piece of example...

$("p").css({background:'linear-gradient(red,blue,red)'});

Comments

1

I'm using the hyphenated syntax in the JSON format (I always use the JSON format to be consistent). And it's working fine in both Chrome and Firefox.

For example:

$("#googleFeed div:even").css({
    'background':'-webkit-linear-gradient(top,white,black)',        
});

Comments

0

try this

<div class="mygradient" />

$( '.mygradient' ).css( 
    "background-image", 
    "linear-gradient( to right, #dc8a8a 50%, red 10% )" 
);

Comments

-2

When using .css() in jquery, I believe you have to use the shortened versions of the attributes. For example, margin-left would be marginLeft

$(element).css("backgroundImage","-webkit-gradient(linear,left bottom,right bottom,color-stop(0.50, rgb(194,231,255)),color-stop(0.50, rgb(255,255,255))");

4 Comments

side note, web-kit gradient isn't a background image its a background
and it should be background-image when you are using the syntax you provided. When you write multiple properties you should use the camelCase ... just like this : $(element).css({ backgroundImage : 'whatever'; })
This is only true when using the object notation to set properties, such as .css({backgroundImage:blah}), not the implementation he's using of .css('background-image', blah)
Also, Motown is wrong, it's background-image, and background alone is only a shorthand way of setting the background-image.

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.