Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <nav>
    <h1 class='title'>Color flipper</h1>
    <h1 class="state"></h1>
  </nav>

  <div class="container">
    <h3 id='simple'>Simple</h3>
    <h3 id='complex'>Complex</h3>
  </div>

  <div class='container cta'>
    <h1>Generate colour! </h1>
  </div>

  <div class="container color-box"></div>


  <script src="script.js"></script>

</body>

</html>
              
            
!

CSS

              
                html {
	font-size: 10px;
	box-sizing: border-box;
}

body {
	margin: 0px;
	padding: 0px;
}

.title {
	padding: 0;
	margin: 0;
	color: white;
	font-family: 'Montserrat', sans-serif;
	font-size: 4rem;
	letter-spacing: .5rem;
}

.state {
	color: white;
	font-family: 'Montserrat', sans-serif;
	font-size: 2rem;
	letter-spacing: .5rem;
}

h3 {
	border: 1px solid black;
	padding: 1.5rem 4rem;
	font-family: 'Montserrat', sans-serif;
	font-size: 1.8rem;
	/* font-weight: 300; */
	transition: 0.4s;
	cursor: pointer;
}

h3:hover {
	background-color: black;
	color: white;
}

nav {
	width: 100%;
	min-height: 8rem;
	background-color: #6296fc;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	min-width: 60%;
	text-align: center;
}

.container {
	display: flex;
	margin: 5rem auto;
	border: 1px solid black;
	width: 50%;
	justify-content: space-around;
	align-items: center;
	min-width: 60%;
	max-width: 80%;
	min-height: 15vh;
}

.cta {
	color: black;
	font-family: 'Montserrat', sans-serif;
	font-size: 1.8rem;
	transition: .3s;
}

.cta:hover {
	background-color: black;
	color: white;
}

.color-box {
	min-height: 30vh;
	max-height: 50vh;
}

@media only screen and (max-width: 600px) {
	.container {
		flex-direction: column;
	}
}

@media only screen and (max-width: 370px) {
	.title {
		font-size: 3rem;
	}
	.state {
		font-size: 2.5rem;
	}
}

              
            
!

JS

              
                // Variable

let generateButton = document.querySelector('.cta')
let colorBox = document.querySelector('.color-box')
let simpleButton = document.querySelector('#simple')
let complexButton = document.querySelector('#complex')
let r = ''
let g = ''
let b = ''
let colors = ['red', 'lightblue', 'yellow', 'coral', 'beige']
let stateDisplay = document.querySelector('.state')
let state = 'complex'

//Simple or complex

stateDisplay.textContent = `State selected: ${state}`

simpleButton.addEventListener('click', () => {
  state = 'simple'
  stateDisplay.textContent = `State selected: ${state}`
})


complexButton.addEventListener('click', () => {
  state = 'complex'
  stateDisplay.textContent = `State selected: ${state}`
})

//Function

const generateNumber = () => {
  return Math.floor(Math.random() * 256)
}

const generateColor = () => {
  r = generateNumber()
  g = generateNumber()
  b = generateNumber()
  return `${r}, ${g}, ${b} `
}

const generateColorEasy = () => {
  let number = Math.floor(Math.random() * colors.length)
  return colors[number]
}

//Main function

generateButton.addEventListener('click', () => {
  if (state === 'complex') {
    let rgbValue = generateColor()
    generateButton.textContent = `${state}, ${rgbValue} `
    colorBox.style.backgroundColor = `rgb(${rgbValue})`
  }

  else if (state === 'simple') {
    colorBox.style.backgroundColor = generateColorEasy()
    generateButton.textContent = `${state}, ${colorBox.style.backgroundColor} `
  }
})



// 



              
            
!
999px

Console