Skip to content

Instantly share code, notes, and snippets.

@hemnathmouli
Created March 1, 2017 10:28
Show Gist options
  • Save hemnathmouli/24f65933ce8faa7bc4cd04ebfe0ccf0b to your computer and use it in GitHub Desktop.
Save hemnathmouli/24f65933ce8faa7bc4cd04ebfe0ccf0b to your computer and use it in GitHub Desktop.
How to create a random color variable with Javascript and HTML?
<html>
<head>
<title>Random Background</title>
<script>
setInterval(function(){
var x = Math.round( Math.random() * 255 );
var y = Math.round( Math.random() * 255 );
var z = Math.round( Math.random() * 255 );
var bg = "background:rgb("+x+", "+y+", "+z+");";
var element = document.getElementById("random-background");
element.style = bg;
}, 1000);
</script>
<style>
#random-background {
width: 100vw;
height: 100vh;
background: white;
margin: 0px;
transition: 1s;
}
</style>
</head>
<body id = "random-background">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment