Hit enter after type your search item

5 demos of jQuery to Add, change, remove CSS for div, menu, table and more

The jQuery method to set CSS properties

The CSS properties of the elements in the web page can be added, changed and removed (at run time) by using the jQuery css method.

The $.css can also be used to get the CSS properties of the first matched element in DOM.

I will show you a few demos to use $.css jQuery method in a div, a menu, HTML table, list, accordion and Bootstrap navbar – so keep reading.


Syntax of $.css method

To set CSS single property (add or modify):


To set multiple properties at one call:


See the following examples for having a better idea for real-time implementation. Click on demo links to see online with each example.

Starting with a simple div to change CSS

In this demo, a div is created with a few properties as web page loads. The properties are specified in the <style> section of head tag. As you click on the button, the CSS properties of div will be changed. See online:

jQuery CSS div

See online demo and code

You saw, a div with a solid border with black color and without any background displayed as the demo page loaded. If you clicked the button “Change CSS”, the following properties were changed:


A few points to notice

  • This line of code is placed in the click event of the button.
  • You have to separate multiple CSS properties by a comma.
  • A semicolon is not used after the value as we do in the CSS style section.

A jQuery css method demo in a menu to change/remove CSS

In this example, a menu is displayed with initial CSS properties set in the style section with the bluish theme. As demo page loads, you can see three buttons that you can click to change the colors of the menu.

On the click event of each button, the $.css method is attached to change the color of links class, which hierarchy, in that case is: div ul li a.

See this demo online first:

jQuery CSS

See online demo and code

Following jQuery code is used in the script section:


You may notice, the border-radius is kept 5px for the menu at the initial stage. If you click on the “Change to theme1” button, it remains the same.

That means, the properties that are used in the $.css method will replace the previous values while that are not used remain intact. So, the border-radius stayed the same.

Similarly, if you click at the “Green theme” button, the border-radius is changed in $.css method. Now it will remain same if you click red or first theme buttons because these buttons are not setting the border-radius property.

There is another button, Restore initial setting. Clicking this will return menu to its original style with the same properties as set in the <style> section of the head tag.

In that button, following script is used:


So we can conclude a few points here:

  • All the properties used in the initial stage (that are set at the style tag, inline or external CSS) remains the same unless you modify it in $.css method.
  • If you change only a few properties out of all initial properties, the other properties will stay the same. For example, if text color was set as white with the black background and you changed the background to red. The color of the text will remain white.
  • You may also use CSS 3 properties in $.css method like I used the border-radius property.
  • The jQuery css method can be used to add and change the CSS properties in the specified elements.
  • You may remove the properties added/modified by using the $.removeAttr method, as done by using the “Reset Default” button in the demo.

An example of using $.css in HTML table

In this demo, a table with initial CSS style is created with dummy data. To give it a variation, I have used check boxes instead of the buttons to call $.css jQuery method to add, change or remove CSS properties.

jQuery CSS table

See online demo and code

As you click the first checkbox, “Add Header background and other properties”, it will add background-color in the table header. Also, the border-radius is added while the border is removed. The color property is changed what was set initially.

The table header is accessible by using following:


Where demotbl is the main table class!

Similarly, no background color was given initially to <td>. As you click on the second checkbox to add a background, it will not only add background color but also increase the font-size, text-align and change the border properties as well.

Following jQuery code is used in the click event of the second checkbox to change CSS:


The last two checkboxes, in the second row, are to remove the properties added or changed by using the jQuery $.css method. Alternatively, you can say it will change CSS to default or initial state.

Note: I have only shown this demo for the understanding purpose. In pro websites, you should change/default by selecting and unselecting the same checkbox.

Using $.css with jQuery UI accordion

Not only, you may change CSS of simple elements like div, tables etc. but also other components of jQuery UI by using the $.css method.

In that demo, an accordion is created by using the jQuery UI library. Initially, the accordion is displayed with default CSS as used in the library with three panels.

After the accordion, two buttons are given to change the CSS of panels and content area. See the demo online with a little description after it:

jQuery CSS UI accordion

See online demo and code

Quite a few properties are changed as you click “change header” and “change content” buttons. In the click events of buttons, the built-in UI accordion classes are used that overridden the default properties:



You can see, the ui-accordion .ui-accordion-header class will handle the header part of the accordion where background, text color, border, display and border-radius properties are used.

While the ui-accordion .ui-accordion-content deals with the content inside the accordion panels. A few properties of CSS are also changed by using the jQuery $.css method.

A final demo – Add/Modify CSS for Bootstrap navbar component

In the final demo, I will show you changing/adding the CSS properties in the Bootstrap navbar component.

For that, a navbar is created by using the Bootstrap libraries. It uses default classes that are built-in in the framework’s CSS.

By using the $.css method, I will show you how you can change a few properties. See the online demo first:

jQuery CSS Bootstrap

See online demo and code

The demo page is loaded with a default navbar style of Bootstrap. You can also see six Bootstrap buttons below the navigation bar. Each button is given a built-in class of navbar as well, that also define its color.

As you click on any button, the navbar’s theme is also changed which is matched to the color of the button. For example, clicking on the primary button will change the navbar theme to blue and danger to red and so on.

To accomplish that, $.css method of jQuery is used in the click event of each button. Look at this code in the head section:


You can see, in each button the navbar-default and navbar-default .navbar-nav > li > a classes are used in the $css method. These are built-in classes in Bootstrap framework’s CSS. I am just overriding the properties in the click event of each button.

For that demo, only a few properties are changed, however, there are many other properties that you can work with, for example, drop-down links are not changed in that demo. In pro website, this should be managed as well. Similarly, form’s classes can also be changed by using the css jQuery method.

You can see detail about other classes in its own chapter here.

The last button will turn the navbar into default theme, which is again done by using the removeAttr method of jQuery, as in above example. So it just removed any CSS style that is applied by clicking the buttons.

This div height required for enabling the sticky sidebar