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.










