Random Background Color with JavaScript | Plantpot

Random Background Color with JavaScript

Web Dev
Sponsored links


Full Screen


Sponsored links




<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JavaScript Random Background Color</title>
    <link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
    <link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="color.js"></script>
    <div id="container">
      <div class="box">
        <div id="color" class="color"></div>
        <button onclick="backgroundColor()">Change Color</button>


@charset "utf-8";
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
html {
  font-size: 16px;
body {
  font-family: futura-pt, sans-serif;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
#container {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
.box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 200px;
  height: 160px;
  background-color: #fff;
.color {
  width: 100%;
  color: #3d3935;
  font-size: 1.5rem;
  text-align: center;
button {
  width: 180px;
  margin-top: 10px;
  padding: 10px;
  border: none;
  border-radius: 4px;
  background-color: #000;
  color: #fff;
  font-size: 1.25rem;
  outline: none;
  cursor: pointer;


var r;
var g;
var b;
var color;

function backgroundColor() {
  r = Math.floor(Math.random() * 256);
  g = Math.floor(Math.random() * 256);
  b = Math.floor(Math.random() * 256);
  color = `rgb(${r},${g},${b})`;
  document.getElementById("container").style.background = color;
  document.getElementById("color").innerHTML = color;

window.onload = function() {