Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple CSS elements to a div using jQuery?

Tags:

I would like to do this:

<div style="float: left;             width: 59px;             background: transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px;"      class="cIconSmall"> </div> 

and I'm thinking I should use this:

$("#YourElementID").css({     float: "left",     width: "59px",     background: "transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px" }); 

Any ideas?

Thanks

like image 759
Patrioticcow Avatar asked Apr 19 '11 18:04

Patrioticcow


People also ask

How can use multiple CSS properties in jQuery?

Apply multiple CSS properties using a single JQuery method CSS( {key1:val1, key2:val2....). You can apply as many properties as you like in a single call. Here you can pass key as property and val as its value as described above.

How do you add multiple CSS?

To define multiple CSS attributes in jQuery, use the css selector or the addClass() method. Let's see how to do it using css selector. Pair up strings representing property names with the corresponding values.

Can we add CSS in jQuery?

jQuery has several methods for CSS manipulation. We will look at the following methods: addClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements.

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.


2 Answers

You're thinking correctly. Using the css(map) method is the way to go.

$(".cIconSmall").css({     float: "left",      width: "59px",      background: "transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px"  }); 

http://api.jquery.com/css/

A map of property-value pairs to set.


Might be nicer as a css class, though... then you can just write $(".cIconSmall").addClass("icon47"); but there's a time for everything...

like image 118
hunter Avatar answered Oct 15 '22 20:10

hunter


$(".yourClass").css({     float: "left",     width: "59px",     background: "transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px" }); 
like image 28
Superman Avatar answered Oct 15 '22 20:10

Superman