Skip to main content

Add CSS Properties to an Element Using jQuery

Submitted by admin on Thu, 12/16/2021 - 14:36
jquery-add-css

In this jQuery tutorial we will learn how to add css properties to any element (div / span / paragraph etc.) using jQuery.

Using jQuery css() method, we can add new CSS properties to any element.

Syntax

For adding just one css property, use below syntax.


$('element').css('property', 'value');

For adding multiple css properties, use below syntax.


$('element').css({
  'property-one': 'value',
  'property-two': 'value'
  }
);

Example

In below example, I am going to change the color of text under div of class newcolor to blue.


<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Add CSS Properties Using jQuery</title>
    <script src="//code.jquery.com/jquery-3.4.1.min.js"></script>
    <script>
    $(document).ready(function(){
      $('.newcolor').css('color', 'blue');
    })
    </script>
  </head>
  <body>
    <div class="newcolor">This text is blue color</div>
  </body>
</html>

Category

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.