JavaScript how to make text color

Learn how to add color to text with Javascript! Follow this simple example to get started.

Changing Text Color using JavaScript

JavaScript is a great tool for adding interactive elements to websites. One of the most common uses is to change the color of text. With JavaScript, you can use the style.color property to change the color of any HTML element.


//Set the element you want to change
let myElement = document.getElementById('myElement');

//Change the color of the element
myElement.style.color = "#FF0000";

The style.color property takes any valid CSS color. You can use both named colors (like red) and hex values (like #FF0000).

You can also use the style.backgroundColor property to change the background color of an element. This property also takes any valid CSS color.


//Change the background color of the element
myElement.style.backgroundColor = "#0000FF";

In addition to changing the color of text and backgrounds, you can also use JavaScript to apply other styles. For example, you can use the style.fontSize property to change the font size of an element:


//Change the font size of the element
myElement.style.fontSize = "24px";

With JavaScript, you can change the color and other styles of any HTML element. This can be a great way to add interactive elements to your website.

Answers (0)