• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Add Style to HTML Element Using jQuery

Add Style to HTML Element Using jQuery

December 1, 2021 Leave a Comment

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:

<div id="div">
  <p class="p">This is a paragraph.</p>
</div>

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:

<div id="div" style="background-color:green;">
  <p class="p">This is a paragraph.</p>
</div>

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.


<div id="div1">
  <p id="click-me">Click Me to Add a Style to the Paragraph Below</p>
  <p class="p">This is the paragraph we will add styling to.</p>
</div>

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:


<div class="html-code-output">
<div id="div1">
  <p id="click-me">Click Me to Add a Style to the Paragraph Below</p>
  <p class="p">This is the paragraph we will add styling to.</p>
</div>
</div>

<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:


<div id="div">
  <p id="click-me">Click Me to Add Inline CSS to the Div</p>
</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:


<div class="html-code-output">
<div id="div">
  <p id="click-me">Click Me to Add Inline CSS to the Div</p>
</div>
</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.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Scroll to Div
  • 2.  Using jQuery to Capitalize the First Letter of a String
  • 3.  jQuery parent – Get the Parent Element of a Div
  • 4.  Using jQuery to Get Value of Select On Change
  • 5.  Using jQuery to Disable a Button
  • 6.  jQuery Get Last Child
  • 7.  jQuery before – Insert HTML Before Another Element
  • 8.  How to Use jQuery to Change Button Text
  • 9.  jQuery rotate – How to Rotate an Image using jQuery
  • 10.  Using jQuery to Increment Value on Click

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy