HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs 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 its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<h1 class="text">It's Too Dark!!</h1>
<div class="container">
<div class="colorpicker">
<div class="color">
<label id="red">
R : <span class="value">0</span>
</label>
<input type="range"
style="--slider-thumb-color: #fff;"
id="redSlider" class="slider"
min="0" max="255"
step="1" value="0" />
</div>
<div class="color">
<label id="green">
G : <span class="value">0</span>
</label>
<input type="range"
style="--slider-thumb-color: #fff;"
id="greenSlider" class="slider"
min="0" max="255"
step="1" value="0" />
</div>
<div class="color">
<label id="blue">
B : <span class="value">0</span>
</label>
<input type="range"
style="--slider-thumb-color: #fff;"
id="blueSlider" class="slider"
min="0" max="255"
step="1" value="0" />
</div>
</div>
</div>
<div class="support">
<a href="https://twitter.com/DevLoop01" target="_blank"><i class="fab fa-twitter-square"></i></a>
<a href="https://codepen.io/dev_loop/" target="_blank"><i class="fab fa-codepen"></i></a>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100%;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
background: #000;
font-family: "montserrat";
overflow: hidden;
}
h1.text {
color: #fff;
font-size: 2.5rem;
margin-bottom: 40px;
transition: all 0.15s ease-in-out;
white-space: nowrap;
}
.container {
width: 300px;
height: 200px;
}
.colorpicker {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
padding: 15px 30px;
border-radius: 15px;
}
.color {
position: relative;
background: rgba(255, 255, 255, 0.15);
width: 100%;
height: 60px;
margin: 8px 0;
display: flex;
flex-direction: column;
border-radius: 15px;
transition: all 0.15s ease-in-out;
label {
position: absolute;
top: -10px;
left: 20px;
color: #fff;
transition: all 0.15s ease-in-out;
}
input {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
$range-handle-size: 20px !default;
$range-track-color: rgba(255, 255, 255, 0.314) !default;
$range-track-height: 2px !default;
$range-label-width: 60px !default;
.slider {
-webkit-appearance: none;
width: calc(100% - (#{$range-label-width + 0px}));
height: $range-track-height;
border-radius: 5px;
background: $range-track-color;
outline: none;
padding: 0;
margin: 0;
cursor: pointer;
transition: all 0.15s ease-in-out;
&::-webkit-slider-thumb {
-webkit-appearance: none;
width: $range-handle-size;
height: $range-handle-size;
border-radius: 50%;
background: var(--slider-thumb-color);
cursor: pointer;
transition: all 0.15s ease-in-out;
&:hover {
transform: scale(1.1);
}
}
&::-moz-range-thumb {
width: $range-handle-size;
height: $range-handle-size;
border: 0;
border-radius: 50%;
background: var(--slider-thumb-color);
cursor: pointer;
transition: background 0.15s ease-in-out;
&:hover {
transform: scale(1.1);
}
}
}
.support{
position: fixed;
right: 10px;
bottom: 10px;
padding: 10px;
display: flex;
}
a{
margin: 0 20px;
color: #fff;
font-size: 2rem;
transition: all 400ms ease;
}
a:hover{
color: #222;
}
console.clear();
// Selecting all the icons - Twitter & Codepen
const socialIcons = Array.from(document.querySelectorAll('.fab'));
const sliderProps = {
fill: "#fff",
background: "rgba(255, 255, 255, 0.214)",
};
// Selecting All the Slider Containers
const sliderContainers = Array.from(document.querySelectorAll(".color"));
const displayText = document.querySelector(".text");
// This function is responsible to create the trailing color and setting the fill.
function applyFill(slider) {
const percentage = (100 * (slider.value - slider.min)) / (slider.max - slider.min);
const bg = `linear-gradient(90deg, ${sliderProps.fill} ${percentage}%, ${sliderProps.background} ${percentage +
0.1}%)`;
slider.style.background = bg;
}
// Using Event Listener to apply the fill and also change the value of the text also the bgcolor based on brightness.
sliderContainers.forEach(sliderContainer => {
const rangeInput = sliderContainer.querySelector("input");
const rangeValue = sliderContainer.querySelector(".value");
rangeInput.addEventListener("input", event => {
rangeValue.innerHTML = event.target.value;
applyFill(event.target, rangeValue);
let redSlider = document.getElementById("redSlider").value;
let greenSlider = document.getElementById("greenSlider").value;
let blueSlider = document.getElementById("blueSlider").value;
// use ML to determine result
let networkObject = createNetworkObject(redSlider, greenSlider, blueSlider);
let MLresult = network.run(networkObject);
MLresult = networkLabel(MLresult);
// change colors
changeBackgroundColor(redSlider, greenSlider, blueSlider);
changeElementsColor(MLresult);
});
});
// Neural Network part
const network = new brain.NeuralNetwork();
// train algorithm with known data
network.train([
{ input: { r: 0.62, g: 0.72, b: 0.88 }, output: { light: 1 } },
{ input: { r: 0.1, g: 0.84, b: 0.72 }, output: { light: 1 } },
{ input: { r: 0.74, g: 0.78, b: 0.86 }, output: { light: 1 } },
{ input: { r: 0.33, g: 0.24, b: 0.29 }, output: { dark: 1 } },
{ input: { r: 0.31, g: 0.35, b: 0.41 }, output: { dark: 1 } },
{ input: { r: 1, g: 0.42, b: 0.52 }, output: { dark: 1 } },
{ input: { r: 0, g: 0, b: 1 }, output: { dark: 1 } },
{ input: { r: 0.8, g: 0.44, b: 1 }, output: { dark: 1 } },
{ input: { r: 0, g: 0.44, b: 1 }, output: { dark: 1 } },
{ input: { r: 0.3, g: 0.6, b: 1 }, output: { dark: 1 } },
{ input: { r: 0.1, g: 0.6, b: 0 }, output: { dark: 1 } },
]);
// creating an object for neural network
function createNetworkObject(r, g, b) {
let networkObject = { r: null, g: null, b: null };
networkObject.r = r / 255;
networkObject.g = g / 255;
networkObject.b = b / 255;
return networkObject;
}
// Machine learning probability label - determining the result : Light OR Dark
function networkLabel(result) {
if (result.light > result.dark) {
result = "light";
} else {
result = "dark";
}
return result;
}
// Change the background color based on the slider value.
function changeBackgroundColor(r, g, b) {
document.body.style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
}
// change colors on page relative to background
function changeElementsColor(result) {
sliderContainers.forEach(sliderContainer => {
const slider = sliderContainer.querySelector("input");
if (result == "light") {
sliderProps.fill = "#000";
sliderProps.background = "rgba(0, 0, 0, 0.214)";
applyFill(slider);
sliderContainer.style.background = "rgba(0, 0, 0, 0.15)";
sliderContainer.querySelector("label").style.color = "#000";
slider.style.setProperty("--slider-thumb-color", "#000");
displayText.style.color = "#000";
displayText.innerHTML = "Now It's Bright!!";
socialIcons.forEach(icon => {
icon.style.color = '#000'
})
} else if (result == "dark") {
sliderProps.fill = "#fff";
sliderProps.background = "rgba(255, 255, 255, 0.214)";
applyFill(slider);
sliderContainer.style.background = "rgba(255, 255, 255, 0.15)";
sliderContainer.querySelector("label").style.color = "#fff";
slider.style.setProperty("--slider-thumb-color", "#fff");
displayText.style.color = "#fff";
displayText.innerHTML = "It's Quite Dark!!";
socialIcons.forEach(icon => {
icon.style.color = '#fff'
})
}
});
}
Also see: Tab Triggers