How To Get Css Value In Jquery

jQuery is a popular and powerful JavaScript library that simplifies various tasks like handling events, creating animations, and managing DOM elements. One of the common tasks that web developers perform is getting and setting CSS values for different elements. In this blog post, we will learn how to get the CSS value of an element using jQuery.

Getting CSS Values using jQuery

jQuery provides a method called .css() that can be used to get or set the CSS values of an element. In this post, we will focus on getting the CSS values. To get the CSS value of an element using jQuery, you simply need to pass the property name as a string to the .css() method. For example, if you want to get the font-size of a paragraph element, you can do the following:

    $(document).ready(function() {
        var fontSize = $('p').css('font-size');
        console.log("The font size of the paragraph is: " + fontSize);

In the example above, we first wait for the document to be ready using $(document).ready(), and then we select the paragraph element using $(‘p’). After that, we use the .css() method to get the font-size value, and finally, we log the result to the console.

Getting Multiple CSS Values

You can also get multiple CSS values at once by passing an array of property names to the .css() method. The method will then return an object containing the property values. Here’s an example of how you can get the width and height of a div element:

    $(document).ready(function() {
        var dimensions = $('div').css(['width', 'height']);
        console.log("The width of the div is: " + dimensions.width);
        console.log("The height of the div is: " + dimensions.height);

In this example, we pass an array containing the strings ‘width’ and ‘height’ to the .css() method. The method then returns an object with the properties ‘width’ and ‘height’ and their respective values. We then log the values to the console.


In this blog post, we learned how to get the CSS value of an element using jQuery. By using the .css() method, you can easily retrieve the values for various CSS properties. This method is useful when you need to manipulate the appearance of elements dynamically or analyze the styling applied to different elements on a webpage. Happy coding!