1

This is working

div {
  width: 100px;
  height: 100px;
  background-image:
 -webkit-linear-gradient(top, rgba(0, 0, 0, 0)0%, rgba(0, 0, 0, 0.65) 100%), 
  url('http://i.imgur.com/Hsban3N.jpg');
}
<div id="deco">123</div>

but why when I try to set it using css() of jquery it doesn't seem working? No error in the console at all :

http://jsfiddle.net/xcso27zk/

4 Answers 4

1

From jQuery 1.8.8 on, it prefixes for you, so all you'll need to type is

$('#deco').css({
    'background-image': 'linear-gradient(to bottom, rgba(0,0,0,0) 80%, rgba(0,0,0,.65) 100%), url(' + img + ')'
})

Also. If you type something like this

$('#deco').css({
    'background-image': '-webkit-gradient(linear, left top, left bottom, color-stop(80%, rgba(0,0,0,0)), color-stop(100%, rgba(0,0,0,.65))), url(' + img + ')', 
    'background-image': '-webkit-linear-gradient(top,       rgba(0,0,0,0) 80%, rgba(0,0,0,.65) 100%), url(' + img + ')', 
    'background-image': '   -moz-linear-gradient(top,       rgba(0,0,0,0) 80%, rgba(0,0,0,.65) 100%), url(' + img + ')', 
    'background-image': '     -o-linear-gradient(top,       rgba(0,0,0,0) 80%, rgba(0,0,0,.65) 100%), url(' + img + ')', 
    'background-image': '        linear-gradient(to bottom, rgba(0,0,0,0) 80%, rgba(0,0,0,.65) 100%), url(' + img + ')'
})

The object won't work like you desire in some browsers. You will simply rewrite 'background-image' 4 times, essentially writing useless code.

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

2 Comments

Internet Explorer doesn't support duplicate keys in a javascript object. Even in Chrome, given value = { foo: 0, foo: 1 }; Object.keys(value) returns ['foo'] value['foo'] === 1 The first value, 0, is replaced by the second, 1.
Unfortunately, we can only use jQuery's vendor prefixing for property names. It never attempts to prefix anything in the value.
1

JQuery's css function doesn't accept a set of objects in a single call.

Instead of

$(...).css({},{},{})

Try

$(...).css({})
  .css({})
  .css({})

var img = 'http://i.imgur.com/Hsban3N.jpg';

$('#deco').css({
'background-image': '-moz-linear-gradient(top,rgba(0,0,0,0)80%,rgba(0,0,0,0.65)100%), url(' + img + ')'
}).css({
'background-image': '-webkit-gradient(linear,left top,left bottom,color-stop(80%,rgba(0,0,0,0)),color-stop(100%,rgba(0,0,0,0.65))), url(' + img + ')'
}).css({
'background-image': '-webkit-linear-gradient(top,rgba(0,0,0,0)80%,rgba(0,0,0,0.65)100%), url(' + img + ')'
}).css({
'background-image': '-o-linear-gradient(top,rgba(0,0,0,0)80%,rgba(0,0,0,0.65)100%), url(' + img + ')'
}).css({
'background-image': 'linear-gradient(to bottom,rgba(0,0,0,0)80%,rgba(0,0,0,0.65)100%), url(' + img + ')'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="deco">123</div>

6 Comments

It does accept one object with multiple properties, though. So having a lot of .css()s isn't necessary
In this case, it's required. Because the property name is the same in each .css call, we're unable to combine them into a single multi-property call.
It still isn't required, because having multiple .css() calls with the same property will only just override the previous ones
each rule is conditionally accepted by the browser which is required for cases like multiple vendor prefixes. the last one accepted as valid by the browser is the one that sticks
Didn't know about that! Does this also work for the two -webkit- ones?
|
1

This is how I did this and it seems to be working fine on both FF and Chrome.

var img = $(".stall-banner-img").val();
  $(".stall-page-banner").css({
    'background-image': 'linear-gradient(to bottom,rgba(82, 75, 75, 0.5)80%,rgba(34, 32, 32, 0.5)100%), url(' + img + ')'
  });

Comments

-1

You are passing multiple javascript objects to the css method, but it only takes one javascript object with multiple name-value pairs. So like this:

$('#deco').css({
    'background-image': '-moz-linear-gradient(top,rgba(0,0,0,0)80%,rgba(0,0,0,0.65)100%), url(' + img + ')', 
    'background-image': '-webkit-gradient(linear,left top,left bottom,color-stop(80%,rgba(0,0,0,0)),color-stop(100%,rgba(0,0,0,0.65))), url(' + img + ')', 
    'background-image': 'background-image: -webkit-linear-gradient(top,rgba(0,0,0,0)80%,rgba(0,0,0,0.65)100%), url(' + img + ')', 
    'background-image': '-o-linear-gradient(top,rgba(0,0,0,0)80%,rgba(0,0,0,0.65)100%), url(' + img + ')', 
    'background-image': 'linear-gradient(to bottom,rgba(0,0,0,0)80%,rgba(0,0,0,0.65)100%), url(' + img + ')'
});

with only one set of brackets

2 Comments

Internet Explorer doesn't support duplicate keys in a javascript object.
Duplicate property names cause a syntax error in ECMAScript 5 strict mode. ECMAScript 2015 removes the restriction, but still, the last property overrides the prior. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

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.