Skip to main content

Add CSS Class to an Element Using jQuery

Submitted by admin on Thu, 12/16/2021 - 18:37
jquery-add-css-class

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

Using jQuery addClass() method, we can add new CSS class to any element.

Syntax


$('element').addClass('class-name');

To add more than one class, use below syntax.


$('element').addClass('class-one class-two');

Example

In below example, we will add a class named newclass to header region.


<script>
$(document).ready(function(){
  $('header').addClass('newclass');
})
</script>

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.