Skip to Content

How do you add random colors in HTML?

Adding random colors in HTML can be a great way to add visual interest and dynamism to your web pages. Whether you want to randomly color table rows, text, or page backgrounds, there are a few different approaches you can take using JavaScript and CSS in your HTML documents.

Using JavaScript to Generate Random Colors

One of the easiest ways to add random colors in HTML is by using JavaScript to generate random hex color codes. Here’s a simple example to get a random hex color:

let color = '#' + Math.floor(Math.random()*16777215).toString(16);

This generates a random hex code like #a197c2. To use this color, you can set the style color property of an element:

<div style="color: " + color + ";">I'm randomly colored!</div>

You can also create a function to generate a random color each time it is called:

function getRandomColor() {
  let color = '#' + Math.floor(Math.random()*16777215).toString(16);
  return color;

And use it whenever you need a random color:

<div style="background-color: " + getRandomColor() + ";">Random background!</div>

Randomly Coloring Table Rows

To randomly color table rows, you can loop through each row and assign a random color using JavaScript:

<table id="dataTable">
  <tr><td>Row 1</td></tr>
  <tr><td>Row 2</td></tr>
  <tr><td>Row 3</td></tr>

  let table = document.getElementById("dataTable");
  for (let i = 0, row; row = table.rows[i]; i++) { = getRandomColor();

This loops through each table row and sets a random background color on it. The end result is a table with rows colored with different random colors.

Randomly Styling Text

You can also use JavaScript to randomly style and color text elements. For example:

<p id="randomText">This text will be randomly styled!</p>

  let text = document.getElementById("randomText");
  let styles = ["italic", "bold", "underline"];
  let randomStyle = styles[Math.floor(Math.random()*styles.length)]; = randomStyle; = getRandomColor();

This randomly selects an italic, bold, or underline style and applies it to the paragraph text, along with a random color. You can expand on this to include random sizing, shadows, opacity, and other CSS styles.

Random Background Colors

You can also dynamically set random background colors for page elements. For example:

<div id="container">
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>

  let container = document.getElementById("container"); = getRandomColor();

This will randomly color the background of the entire <div> container element.

You can even randomly change background colors at intervals to make them dance!

<div id="colorDance"></div>

  let colors = ["red", "green", "blue", "yellow"];
  function dance() {
    let div = document.getElementById("colorDance");
    let randomColor = colors[Math.floor(Math.random() * colors.length)]; = randomColor;
  setInterval(dance, 500); // Change color every 500ms

Using CSS Variables

Another way to generate random colors in HTML/CSS is by using CSS custom properties (variables).

Define a variable in your CSS:

:root {
  --random-color: #a197c2; 

And use it in any style:

div {
  background-color: var(--random-color);

You can then update this variable in your JavaScript:"--random-color", getRandomColor());

And any element using that variable will be updated with the new random color.

Randomizing Colors on Scroll

You can use JavaScript events like onscroll to randomly change colors each time the user scrolls the page.

For example:

  window.addEventListener("scroll", () => { = getRandomColor();

This will make the background color shift with each scroll. You can apply this random color change to many different events like onclick, onmouseover, onkeyup, and more.


Adding random colors in HTML, CSS, and JavaScript can bring your web pages to life with fun, dynamic effects. The key is using JavaScript to generate random hex codes or select from predefined color sets. You can then apply these colors to text, backgrounds, borders, and any other elements.

Some ways to use random colors:

  • Color table rows
  • Style text
  • Set background colors
  • Use CSS variables
  • Change on events like scroll

With a little creativity, you can find many uses for random colors to add visual interest, communicate data, and improve the user experience.

Method Example Notes
JavaScript random color let color = '#' + Math.floor(Math.random()*16777215).toString(16); Generates random hex color code
Style element <div style="color: " + color + ";">I'm random!</div> Applies generated color
Color table rows = getRandomColor(); Loops through rows
Random text style = randomStyle; italic, bold, underline
CSS variables :root { --random-color: #a197c2; } Update with JavaScript

With some creativity, you can find many uses for random color generation to create fun, engaging web page experiences.