Example 1 — Add Class to Last Paragraph
Official jQuery demo: highlight the last p with class selected.
$("p").last().addClass("selected"); 
jQuery CSS methods let you change how elements look — add reusable classes with .addClass(), set inline properties with .css(), and read dimensions. This hub links to 17 CSS method tutorials with try-it labs and FAQs.
Append classes
Stylesheet vs inline
String or array
Dynamic names
remove + add
Full index
Styling in jQuery usually means one of two things: toggling CSS classes defined in your stylesheet, or setting inline style properties directly on elements. Class-based styling keeps presentation in CSS files where it belongs; inline styles are handy for computed values at runtime.
The CSS category covers class manipulation (.addClass(), and later .removeClass(), .toggleClass()), inline styles (.css()), and size helpers (.width(), .height()). They all return a jQuery object (except getters) so you can chain further calls.
If a look already exists as a CSS rule — like .active or .error — use .addClass() instead of .css(). Your HTML stays clean and styles stay in one place.
Minimal class-add workflow:
$("button").addClass("active");
$("li").addClass("selected highlight");
$("div").removeClass("hidden").addClass("visible"); Two ways to change appearance — classes reuse stylesheet rules; inline styles set properties directly:
Search by method name or browse by category. Each tutorial includes syntax, try-it examples, and FAQs.
Add, remove, toggle, and test CSS classes on matched elements.
| Method | Description | Tutorial |
|---|---|---|
.addClass() | Add one or more CSS classes to each matched element — string, space-separated list, array, or callback. | Open |
.hasClass() | Return true if any matched element has the given CSS class — useful for conditionals and filtering. | Open |
.removeClass() | Remove one, several, or all CSS classes from each matched element — string, array, or callback. | Open |
.toggleClass() | Add or remove CSS classes on each matched element — flip on/off or force with a boolean state. | Open |
Read rendered CSS values and write inline style properties on matched elements.
| Method | Description | Tutorial |
|---|---|---|
.css() | Get computed CSS properties from the first matched element or set inline styles on every matched element. | Open |
jQuery.cssHooks | Extend .css() and .animate() with custom get/set handlers for CSS properties — vendor prefixes, cssNumber, and fx.step. | Open |
jQuery.cssNumber | Unitless CSS property allowlist — controls whether .css() auto-appends px to numeric setter values (jQuery 3.x and earlier). | Open |
Read and set element, window, and document height and width.
| Method | Description | Tutorial |
|---|---|---|
.height() | Get the content height of the first matched element as a unitless number, or set height on every matched element. | Open |
.width() | Get the content width of the first matched element as a unitless number, or set width on every matched element. | Open |
.innerHeight() | Get or set height including padding but not border — content box plus vertical padding. | Open |
.outerHeight() | Get or set height including padding and border — optionally margin with outerHeight(true). | Open |
.innerWidth() | Get or set width including padding but not border — content box plus horizontal padding. | Open |
.outerWidth() | Get or set width including padding and border — optionally margin with outerWidth(true). | Open |
Read document coordinates with .offset() and offset-parent coordinates with .position().
| Method | Description | Tutorial |
|---|---|---|
.offset() | Get or set the top and left coordinates of matched elements relative to the document. | Open |
.position() | Get the top and left coordinates of the first matched element relative to its offset parent. | Open |
Read and set horizontal and vertical scroll positions on overflow elements.
| Method | Description | Tutorial |
|---|---|---|
.scrollLeft() | Get or set the horizontal scroll position of matched elements as a unitless pixel number. | Open |
.scrollTop() | Get or set the vertical scroll position of matched elements as a unitless pixel number. | Open |
Include jQuery 3.7+ and open the Try-it links to run each snippet in the browser.
Official jQuery demo: highlight the last p with class selected.
$("p").last().addClass("selected"); Pass a space-separated string to apply two classes in one call.
$("p").last().addClass("selected highlight"); jQuery CSS methods give you a clean API for presentation changes. Start with .addClass() when you want to reuse stylesheet rules — it appends class names without wiping existing ones.
Before jQuery 1.12, .addClass() manipulated the className property. Since 1.12/2.2 it uses the class attribute, so the method works on SVG and XML documents too — not just HTML.
Learn how to append CSS classes with string, array, and callback syntax.
6 people found this page helpful