📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials jQuery in Practice CSS and Classes

CSS and Classes

4 min read Quiz at the end
Control styles with addClass(), removeClass(), and toggleClass(). css() reads or sets inline styles directly. Prefer adding and removing classes so your visual logic stays in CSS where it belongs.

Styling with jQuery

$('p').css('color', 'red');
$('p').css({ color: 'red', fontSize: '18px' });

$('div').addClass('active');
$('div').removeClass('active');
$('div').toggleClass('visible');
$('div').hasClass('active');  // true/false
Topic Quiz · 5 questions

Test your understanding before moving on

1. Which method adds a CSS class?
💡 .addClass() appends one or more classes to matched elements.
2. Which method toggles a class?
💡 .toggleClass() adds the class if absent, removes it if present.
3. How do you set multiple CSS properties?
💡 Pass an object to .css() to set multiple properties at once.
4. Which method checks if an element has a class?
💡 .hasClass() returns true if the element has the specified class.
5. Which method removes a CSS class?
💡 .removeClass() removes one or more classes from matched elements.