You can use the style property for this. For example, if you want to change the border:

document.elm.style.border = "3px solid #FF0000";

Similarly, for color:

document.getElementById("p2").style.color="blue";

The best thing is to define a class and do this:

document.getElementById("p2").className = "classname";

(Cross-browser artifacts must be considered accordingly.)

Answer from Ved on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_htmldom_css.asp
JavaScript DOM CSS
jQuery Selectors jQuery HTML jQuery ... Interview Prep JS Bootcamp JS Certificate JS Reference ... The HTML DOM allows JavaScript to change the style of HTML elements....
🌐
Medium
geraldclarkaudio.medium.com › javascript-changing-css-styles-bd2165ea0e7e
JavaScript — Changing CSS Styles - Gerald Clark - Medium
June 25, 2024 - JavaScript — Changing CSS Styles In the last article I covered changing the style of an html element by directly changing the style attribute using setAttribute. There is a drawback to this method …
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › CSS_Object_Model › Using_dynamic_styling_information
Using dynamic styling information - Web APIs | MDN
October 17, 2025 - To get to the style objects from the document, you can use the Document.styleSheets property and access the individual objects by index (e.g., document.styleSheets[0] is the first stylesheet defined for the document, etc.). You can also access the sheet property on a specific <style> element to get its associated stylesheet object. In this example the background of the page is set to red using CSS. The JavaScript then accesses the property using the CSSOM and changes the background to cornflowerblue.
🌐
CodingNomads
codingnomads.com › change-style-css-javascript
Change CSS Styles with JavaScript
Discover how to dynamically alter web elements using inline styles and CSS classes in DOM manipulation through JavaScript.
🌐
Kirupa
kirupa.com › html5 › setting_css_styles_using_javascript.htm
Setting CSS Styles using JavaScript | KIRUPA
One way is by setting a CSS property directly on the element. The other way is by adding or removing class values from an element which may result in certain style rules getting applied or ignored.
🌐
Microsoft Learn
learn.microsoft.com › en-us › microsoft-edge › devtools › css › css-in-js
Style editing for CSS-in-JS frameworks - Microsoft Edge Developer documentation | Microsoft Learn
Many CSS-in-JS frameworks and libraries use the CSS Object Model APIs under the hood to construct styles. You can edit styles that were added in JavaScript using the CSSStyleSheet interface, which is a way to create and distribute reusable styles when using Shadow DOM.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript dom › javascript style
JavaScript Style: Setting Inline CSS Properties of an HTML Element
November 15, 2024 - The style property returns the read-only CSSStyleDeclaration object that contains a list of CSS properties. For example, to set the color of an element to red, you use the following code: element.style.color = 'red';Code language: JavaScript (javascript)
Find elsewhere
🌐
Udacity
udacity.com › blog › 2021 › 06 › javascript-css.html
Changing Element Styling with Javascript CSS | Udacity
June 28, 2021 - This article discusses how to modify HTML element styling with Javascript CSS, followed by the potential benefits and drawbacks of Javascript CSS compared to external CSS files. Finally, this article provides guidelines for choosing when to use Javascript CSS. Every HTML element in the Javascript DOM contains a Javascript object property called style.
🌐
Crestron
sdkcon78221.crestron.com › sdk › Crestron_HTML5UI › Content › Topics › Reference › Development › Change-CSS.htm
Change CSS Dynamically with JavaScript | Crestron® HTML5 User Interface Developer Microsite
This topic documents how to dynamically update the styling applied to your elements by manipulating the CSS at runtime using JavaScript. Using the DOM API, you can access the specific elements of your page. Through the DOM, access is provided to a style object that describes the style of an element.
🌐
Dummies
dummies.com › article › technology › programming-web-design › javascript › changing-css-with-javascript-140946
Changing CSS with JavaScript | dummies
July 3, 2025 - After you've selected an element, you can change its style by attaching the style property to the selector, followed by the style you want to change. To change the color of the left eye, you can use this JavaScript: document.getElementById(...
🌐
Techaltum
tutorial.techaltum.com › javascript-css.html
JavaScript style Property | how to change css in javascript
December 28, 2025 - JavaScript style property is used to add, get and change css in javascript. style is the property of HTMLElement Node in DOM.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLElement › style
HTMLElement: style property - Web APIs | MDN
This object can be used to get and set the inline styles of an element. A live CSSStyleProperties object.
🌐
W3Schools
w3schools.com › js › js_jquery_css.asp
JavaScript / jQuery CSS Styles
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
SheCodes
shecodes.io › athena › 4587-how-to-style-javascript-with-css
[JavaScript] - How to Style JavaScript with CSS - SheCodes | SheCodes
Learn how to style your JavaScript elements with CSS by using document.querySelector and style JS object.
🌐
Tabnine
tabnine.com › home › set style to an html element using javascript
Set Style to an HTML element using JavaScript - Tabnine
July 25, 2024 - To set the inline style, simply set the style attribute on the HTML element as follows: ... In the example above, the <h2> element’s text will appear blue on the user’s screen. Note: Inline CSS is the most specific, and thus has precedence over the other two approaches.
🌐
JavaScript.info
javascript.info › tutorial › browser: document, events, interfaces › document
Styles and classes
September 29, 2022 - JavaScript can modify both classes and style properties. We should always prefer CSS classes to style.
🌐
W3Schools
w3schools.com › jsref › dom_obj_style.asp
HTML DOM Style object
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
W3C
w3.org › wiki › Dynamic_style_-_manipulating_CSS_with_JavaScript
Dynamic style - manipulating CSS with JavaScript - W3C Wiki
July 9, 2015 - In this article we will look at how to dynamically update the styling applied to your elements by manipulating your CSS at runtime using JavaScript. It uses the same kind of technique that we’ve already seen, but there are a few special considerations to keep in mind when working with the CSS DOM. The browser provides an interface to interact with style sheets — in your JavaScript code you can access a list of your style sheets by using document.styleSheets.