To set the background-image using CSS property, We’re using .css() method of jQuery. To understand example first understand the method.
JQuery .css() method:
This method set/return one or more style properties for the specified elements.
Syntax:
-
Return a CSS property:
$(selector).css("propertyname"); -
Set a CSS property:
$(selector).css("propertyname", "value");
Example-1: In this example, background image is set by the background-image property using JQuery’s .css() method.
<!DOCTYPE html> <html> <head> <title> JQuery | Setting background-image using CSS property. </title> <style> #div { background-repeat: no-repeat; margin-left: 120px; } </style> <script src= </script> </head> <body style="text-align: center;"> <div id="div"> <h1 style="color: green;"> GeeksForGeeks </h1> <p> This is text of the div box, . </p> <br> <button> click to set </button> </div> <br> <br> <script> $('button').on('click', function() { $('#div') .css('background-image', 'url(' + + ')' ); $('h1').css('color', 'black'); }); </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example-2: In this example, background image and background-repeat properties are set by the background property using JQuery’s .css() method.
<!DOCTYPE html> <html> <head> <title> JQuery | Setting background-image using CSS property. </title> <style> #div { margin-left: 120px; } </style> <script src= </script> </head> <body style="text-align: center;"> <div id="div"> <h1 style="color: green;"> GeeksForGeeks </h1> <p> This is text of the div box, . </p> <br> <button> click to set </button> </div> <br> <br> <script> $('button').on('click', function() { $('#div') .css('background', 'url(' + content / uploads / 20190515121004 / gfgbg1.png '+ ' ) no - repeat ' ); $('h1').css('color', 'black'); }); </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- HTML | DOM Style backgroundImage Property
- How to select all elements that contains some specific CSS property using jQuery ?
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- How to remove CSS style of <style> tag using JavaScript/jQuery ?
- How to check an element has certain CSS style using jQuery ?
- How to change Input characters to Upper Case while typing using CSS/jQuery ?
- How to create a CSS rule or class at runtime using jQuery ?
- How to create responsive image gallery using HTML, CSS, jQuery and Bootstrap?
- How to create followspot effect using HTML CSS and jQuery ?
- How to get all CSS styles associated with an element using jQuery ?
- QR Code Generator using HTML, CSS and jQuery
- How to fire event if CSS class is changed using jQuery ?
- How to override the CSS properties of a class using another CSS class ?
- CSS | transition-property Property
- JavaScript TypeError - Setting getter-only property "x"
- jQuery | css() Method
- jQuery | Get and Set CSS Classes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


