To add a style to a HTML element using jQuery, the simplest way is to use the css() method.

$("#div").css("background-color","green");

Let’s say I have the following HTML:

This is a paragraph.

If we want to set a css property and add style to the div, we can use the jQuery css() method to do this with the following Javascript code.

$("#div").css("background-color","green");

The resulting HTML would be as follows:

This is a paragraph.

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div").css("background-color","green");

Add Style to HTML Element Using jQuery With a Click

Many times when creating a web page and the user experience, we want to add different styling and formatting based on an interaction with another element on the web page.

We can change the css by setting a css property and add style to a HTML element using jQuery very easily by combining the addClass() method with a click event.

Let’s say we have the following HTML code and we want to give the user the ability to add a style to the paragraph. The new style will underline the paragraph content.


Click Me to Add a Style to the Paragraph Below

This is the paragraph we will add styling to.

We can utilize both the jQuery click() method and jQuery css() method to set a css property and add a style to the paragraph and make the contents underlined.

Below is the Javascript code which will allow the user to be able to set the css property for the paragraph:

$("#click-me").click(function(){
    $(".p").css("text-decoration","underline");
}); 

The final code and output for this example of how to add style using jQuery and Javascript is below:

Code Output:

Click Me to Add a Style to the Paragraph Below

This is the paragraph we will add styling to.

Full Code:


Click Me to Add a Style to the Paragraph Below

This is the paragraph we will add styling to.

<script> $("#click-me").click(function(){ $(".p").css("text-decoration","underline"); }); </script>

Using jQuery to Set Multiple CSS Properties to HTML Element

We can use the jQuery css() method to add multiple css properties to an HTML element very easily.

The key to adding multiple css properties to our HTML elements is to make a list of key and value pairs separated by commas in the css() method call. {“key1″:”value1”, “key2″:”value2”,…}

For example, if we want to add thecss properties “font-size” and “display” to a specific div, we can do so with the following Javascript code:

$("#div").css({"font-size":"14px","display":"block"});

Let’s say we have the following HTML:


Click Me to Add Inline CSS to the Div

If we want to add css properties to make the font-size bigger and change the background color of the div after a click, we just need to do the following in our Javascript code:

$("#click-me").click(function(){
    $("#div").css({"font-size":"20px","background-color":"green"});
}); 

The final code and output for this example of how to add multiple css properties using jQuery and Javascript is below:

Code Output:

Click Me to Add Inline CSS to the Div

Full Code:


Click Me to Add Inline CSS to the Div

<script> $("#click-me").click(function(){ $("#div").css({"font-size":"20px","background-color":"green"}); }); </script>

Hopefully this article has been useful for you to understand how to use jQuery add style and multiple css properties to a HTML element using jQuery.

Categorized in:

jQuery,

Last Update: February 26, 2024