What Is jQuery .css() Method?

What Is jQuery .css() Method?

HTML, CSS, and JavaScript are the three primary pillars in designing the structure of any website. When it comes to the designing part CSS plays the most significant role, providing various styles for developing your website.

It is the style sheet that adds the visual elements of any webpage. Thus, the .css() method in jQuery is used to set or return the style properties of the elements.

What Does jQuery .css() Do?

jQuery is widely used in designing a website. The jQuery .css() method is used to assign or return style properties for the selected elements. It returns the specified CSS property value of only the first matched element.

Whereas the .css() method sets the specified CSS property for all the matched elements.

The .css() method is the most basic and easy to implement in jQuery. It can assign one or multiple CSS properties and values at a time.

How Does jQuery .css() Work?

The jQuery .css() method is used to assign various styling properties for designing a website. It can assign properties using single or multiple values as well as using a function.

jQuery .css() also returns the single property value of the element.

Syntax:

Returns the CSS
Property Value:

$(selector).css(property)

Set the Single
CSS Property and Value:

$(selector).css(property,value)

Set CSS
Property and Value using a Function:

$(selector).css(property,function(index,currentvalue))

Set Multiple
CSS Properties and Values:

$(selector).css({property:value,property:value, ...})

Here the “property” specifies the name of the property e.g: color, font-size, etc and the “value” specifies the value of the property e.g: red, 20px, etc.

Also, the “function” specifies the function that returns the value for the CSS property, “index” returns the index position of the element, and the “currentvalue” returns the current value of the property. 

Example:

A simple example to set text color as red using .css() method.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 
$("button").click(function(){
   
$("p").css("color", "red");
  });
});
</script>
</head>
<body>
 
<button>Set Font-color as Red</button>
<h1>
<p>Example for .css() Method.</p>
<p>Text 1</p>
<p>Text 2</p>
</h1>
</body>
</html>

What Is jQuery .css() Method?

What Are The Ways To Use Properties With The .css() Method?

There are three ways to use CSS property:

  1. Return CSS Property Value
  2. Set CSS Property and Value using a Function
  3. Set Multiple CSS Properties and Values

1. Return CSS Property Value

You can return the specified CSS property value of the element that has
matched.

Example:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>css demo</title>
  <style>
  div {
    height: 50px;
    margin: 5px;
    padding: 5px;
    float: left;
  }
  #box1 {
    width: 150px;
    color: yellow;
    background-color: blue;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<p id="result">&nbsp;</p>
<div id="box1">Click Here To Get Values</div>
<script>
$( "div" ).click(function() {
  var html = [ "The CSS Property Value:" ];
 
  var styleProps = $( this ).css([
    "width", "height", "color", "background-color"
  ]);
  $.each( styleProps, function( prop, value ) {
    html.push( prop + ": " + value );
  });
 
  $( "#result" ).html( html.join( "<br>" ) );
});
</script>
</body>
</html>

What Is jQuery .css() Method?

2. Set
CSS Property and Value using a Function

With this
method, you can assign the CSS property and value using a function.

Example:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 
$("button").click(function(){
   
$("p").css("border-width", function(i){
      return i + 50;
    });
  });
});
</script>

3.Set multiple CSS Properties and Values

You can assign
multiple CSS properties and values for selected elements. Here is an example to
assign multiple CSS property and value on <p> elements.

Example:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 
$("button").click(function(){
   
$("p").css({
     
"color": "white",
     
"background-color": "red",
     
"font-family": "Times New Roman",
     
"font-size": "30px",
     
"padding": "10px"
    });
  });
});
</script>
</head>
<body>
<h1>Set Multiple CSS Properties and Values</h1>
<button>Set CSS Properties</button>
<p>Example to Set Multiple CSS Properties And Values</p>
</body>
</html>

What Is jQuery .css() Method?

Conclusion

In this guide, we saw what is jQuery .css() method and how it works. We also learned about the different ways to use properties with the .css() method. You can explore more with the examples in this tutorial and learn more about jQuery .css() method.

To learn more about CSS, you can visit here! And to keep learning jQuery, subscribe to our blog!

You can also learn some interesting topics such as jQuery selectors, jQuery Events, jQuery migrate, etc. on our blog.