How To Add Css In Jquery

Are you looking for an easy way to add CSS to your HTML elements using jQuery? Look no further! In this tutorial, we will walk you through the process of adding CSS styles to your web elements using jQuery. With just a few lines of code, you’ll be able to manipulate the appearance of your website elements with ease.

What is jQuery?

jQuery is a popular JavaScript library that makes it easier to work with HTML documents, handle events, create animations, and perform other common tasks on the web. It is designed to simplify the client-side scripting of HTML and is widely used by developers around the world.

Why use jQuery to add CSS?

There are many benefits to using jQuery for adding CSS to your web elements. Some of these include:

  • It simplifies the process of adding and modifying CSS styles in your code.
  • It allows you to easily chain multiple CSS property changes together, making your code more concise and readable.
  • It provides cross-browser support, ensuring that your CSS changes work consistently across different browsers.

Adding CSS with jQuery: A Step-by-Step Guide

Now that we’ve covered the basics of what jQuery is and why you might want to use it to add CSS, let’s walk through the process step by step. For this example, let’s say we want to change the color and font size of a paragraph element with the ID ‘my-paragraph’.

Step 1: Include the jQuery library in your project

First, you’ll need to include the jQuery library in your HTML file. You can either download the library and host it on your own server, or include it from a Content Delivery Network (CDN) like Google or Microsoft. In this example, we’ll use the Google CDN:

    <script src=""></script>

Step 2: Create your jQuery script

Next, we’ll create a new script element in our HTML file and add our jQuery code. First, let’s create the script element:

        // Your jQuery code will go here

Now, let’s add our jQuery code to change the color and font size of the paragraph element with the ID ‘my-paragraph’:

        $(document).ready(function() {
                'color': 'blue',
                'font-size': '20px'

Here’s what’s happening in the code above:

  • We’re using the $(document).ready() function to ensure that our jQuery code only runs once the HTML document has finished loading.
  • We’re then selecting the paragraph element with the ID ‘my-paragraph’ using the $(‘#my-paragraph’) selector.
  • Finally, we’re using the .css() method to apply the color and font size changes. This method accepts an object containing the CSS properties and their corresponding values.

Step 3: Test your code

Once you’ve added your jQuery code, save your HTML file and open it in your web browser. You should see the paragraph element with the ID ‘my-paragraph’ change its color to blue and its font size to 20 pixels.


Adding CSS styles to your HTML elements using jQuery is a simple and efficient way to enhance the appearance and functionality of your website. With just a few lines of code, you can easily modify the CSS properties of your web elements and create dynamic, interactive websites. Happy coding!