AD JB Toolbox 01 EN

jQuery .css(): Read, Set, and Clear Styles

.css() reads the first matched element’s computed style or sets inline styles on every matched element. A computed value can differ from the string originally written in the stylesheet.

Syntax and behavior

const color = $("#box").css("color");
const styles = $("#box").css(["color", "width"]);
$("#box").css({ color: "#a00000", padding: "12px" });

CSS stands for Cascading Style Sheets and controls presentation and layout. A getter incorporates applied styles and inheritance: a color may be returned as rgb(...), or a percentage width as pixels. Pass an array of property names to retrieve a property-value object. Set styles with a property/value pair or an object; a setter returns the jQuery object.

The official jQuery .css() documentation describes additional call signatures and argument types.

Complete runnable example

Save the following code as a UTF-8 example.html file and open it in a browser. It loads the full jQuery 4.0.0 build from an external URL, so an internet connection is required. The $(function () { ... }) callback waits for the document to be ready before running the example.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery css example</title>
  <style>
    body { margin: 24px; font-family: sans-serif; line-height: 1.6; }
    .marked { background: #fff0b3; border: 2px solid #835b00; }
    .active { color: #a00000; font-weight: bold; }
    button, input, select { font: inherit; }
    #output { white-space: pre-wrap; overflow-wrap: anywhere; }
  </style>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<p id="box" style="color: #0000ff;">Paragraph to style</p>
<button id="change" type="button">Change styles</button>
<button id="reset" type="button">Clear changes</button>
  <p id="output" aria-live="polite"></p>
  <script>
    $(function () {
      $("#change").on("click", function () {
        $("#box").css({ color: "#a00000", padding: "12px", border: "2px solid #835b00" });
        $("#output").text("padding-top: " + $("#box").css("padding-top"));
      });
      $("#reset").on("click", function () {
        $("#box").css({ color: "", padding: "", border: "" });
        $("#output").text("Inline changes cleared");
      });
    });
  </script>
</body>
</html>

Expected result

Change styles applies red text, padding, and a border, and shows padding-top: 12px. Clear changes removes these inline properties so other CSS and inheritance apply again. It does not restore the original blue inline color, because the example did not save that value separately.

Important details and troubleshooting

  • Explicit units such as "12px" or "1.2em" make length values clear. Since jQuery 4.0, automatic px addition for numeric values is limited to a defined set of properties, not every CSS property.
  • Read specific properties such as margin-top or border-top-width instead of depending on shorthand-property serialization.
  • Use the exact name for CSS custom properties, for example .css("--accent", "#a00000"). Do not apply camelCase conversion to their names.
  • Use class methods to keep repeated UI states in one stylesheet. Read HTML attributes with .attr(), rather than treating them as CSS properties.

Compare the behavior with .addClass() and CSS classes, .attr() and HTML attributes, .width() and content-box measurements.

More in This Category
jQuery .css(): Read, Set, and Clear Styles

jQuery .css(): Read, Set, and Clear Styles

Read computed styles, set and clear inline properties, and handle CSS units and numeric values in jQuery 4.0.

jQuery .add(): Combine Element Selections

jQuery .add(): Combine Element Selections

Combine selections without changing the original collection, and distinguish selecting elements from inserting them into the document.

jQuery :button Selector: Which Elements Does It Match?

jQuery :button Selector: Which Elements Does It Match?

Check how :button treats button elements, ordinary button inputs, and submit or reset inputs, with a working selection example.

jQuery .wrap(): Wrap Each Element Separately

jQuery .wrap(): Wrap Each Element Separately

Give each selected element its own wrapper, check the returned selection, and avoid duplicate IDs or repeated nesting.

jQuery .addClass(): Add One or More CSS Classes

jQuery .addClass(): Add One or More CSS Classes

Add one or more classes while preserving existing names, generate classes with a callback, and check why styling may not change.

jQuery .before(): Insert Content as the Previous Sibling

jQuery .before(): Insert Content as the Previous Sibling

Insert or move content immediately before a target, and compare before with inside insertion and insertBefore syntax.

jQuery .addBack(): Include the Previous Traversal Selection

jQuery .addBack(): Include the Previous Traversal Selection

Include the previous traversal collection and understand which elements the optional selector filters.

jQuery .find(): Select Matching Descendants

jQuery .find(): Select Matching Descendants

Search through descendant levels while excluding the container, and compare find with children, filter, and has.

jQuery .has(): Select Elements with Matching Descendants

jQuery .has(): Select Elements with Matching Descendants

Filter the original selection by matching descendants, and see why has and find return different elements.

jQuery .after(): Insert Content as the Next Sibling

jQuery .after(): Insert Content as the Next Sibling

Insert or move content immediately after a target, and compare after with inside insertion and insertAfter syntax.