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.
<section class="container">
<h1>HTML Colour Parsing Demo</h1>
<div class="input-group">
<label for="colorInput">Enter text to parse:</label>
<input type="text" id="colorInput" placeholder="Enter any text (e.g., chucknorris)" />
</div>
<div id="steps"></div>
<div class="color-preview">
<div class="color-box" id="colorBox"></div>
<div>
<p>Final color: <strong id="colorCode"></strong></p>
</div>
</div>
<small>Note that this won't properly handle hard-coded colour values such as 'red' or 'blue'.</small>
</section>
body {
font-family: sans-serif;
background: #ffffff;
}
.container {
padding: 2rem;
max-width: 80ch;
margin: 2rem auto;
}
input[type="text"] {
width: 100%;
padding: 0.5rem;
font-size: 1rem;
margin-bottom: 1rem;
}
.step-title {
font-weight: bold;
margin-bottom: 0.5rem;
color: #333;
}
.step-content {
font-family: monospace;
background: #d4d4d4;
padding: 0.5rem;
border-radius: 4px;
overflow-wrap: break-word;
white-space: normal;
}
.color-preview {
display: flex;
gap: 1rem;
align-items: center;
padding: 1rem;
background: #d4d4d4;
margin-top: 1rem;
}
.color-box {
width: 60px;
height: 60px;
}
function visualizeIEColor(input) {
const steps = [];
let workingValue = input;
// Step 1: Initial Cleanup
const originalHasHash = workingValue.startsWith("#");
if (originalHasHash) {
steps.push({
title: "1. Initial Cleanup",
content: `Remove octothorpe: "${workingValue}" → "${workingValue.slice(1)}"`,
value: workingValue.slice(1)
});
workingValue = workingValue.slice(1);
} else {
steps.push({
title: "1. Initial Cleanup",
content: "Step skipped: No octothorpe (#) found at start of value",
value: workingValue
});
}
// Step 2: Replace Invalid Characters
const nonHexPattern = /[^0-9a-f]/gi;
const hasNonHex = nonHexPattern.test(workingValue);
if (hasNonHex) {
const replacedValue = workingValue.replace(nonHexPattern, "0");
steps.push({
title: "2. Replace Invalid Characters",
content: `Replace non-hex characters: "${workingValue}" → "${replacedValue}"`,
value: replacedValue
});
workingValue = replacedValue;
} else {
steps.push({
title: "2. Replace Invalid Characters",
content: "Step skipped: All characters are valid hexadecimal (0-9, A-F)",
value: workingValue
});
}
// Step 3: Standardise Length
const needsPadding = workingValue.length === 0 || workingValue.length % 3 !== 0;
if (needsPadding) {
let standardizedLength = workingValue;
while (standardizedLength.length === 0 || standardizedLength.length % 3 !== 0) {
standardizedLength += "0";
}
steps.push({
title: "3. Standardise Length",
content: `Pad to multiple of 3: "${workingValue}" → "${standardizedLength}"`,
value: standardizedLength
});
workingValue = standardizedLength;
} else {
steps.push({
title: "3. Standardise Length",
content: `Step skipped: Length (${workingValue.length}) is already a multiple of 3`,
value: workingValue
});
}
// Step 4: Split into RGB Components
const partLength = Math.floor(workingValue.length / 3);
const parts = [
workingValue.slice(0, partLength),
workingValue.slice(partLength, partLength * 2),
workingValue.slice(partLength * 2)
];
steps.push({
title: "4. Split into RGB Components",
content: `Split into:\nRed: "${parts[0]}"\nGreen: "${parts[1]}"\nBlue: "${parts[2]}"`,
value: parts.join(",")
});
// Step 5: Handle Length
let processedParts = [...parts];
let lengthStepContent = [];
let lengthProcessingNeeded = false;
// 5a: Truncate to 8 if longer
if (partLength > 8) {
lengthProcessingNeeded = true;
const beforeParts = [...processedParts];
processedParts = parts.map(part => part.slice(-8));
lengthStepContent.push(
`Truncate to 8 characters:\nRed: "${beforeParts[0]}" → "${processedParts[0]}"\nGreen: "${beforeParts[1]}" → "${processedParts[1]}"\nBlue: "${beforeParts[2]}" → "${processedParts[2]}"`
);
}
// 5b: Remove leading zeros while length > 2 and all start with 0
let leadingZerosRemoved = false;
while (
processedParts[0].length > 2 &&
processedParts.every(part => part.startsWith("0"))
) {
lengthProcessingNeeded = true;
leadingZerosRemoved = true;
const beforeParts = [...processedParts];
processedParts = processedParts.map(part => part.slice(1));
lengthStepContent.push(
`Remove leading zeros:\nRed: "${beforeParts[0]}" → "${processedParts[0]}"\nGreen: "${beforeParts[1]}" → "${processedParts[1]}"\nBlue: "${beforeParts[2]}" → "${processedParts[2]}"`
);
}
// 5c: Truncate to first 2 if still longer
if (processedParts[0].length > 2) {
lengthProcessingNeeded = true;
const beforeParts = [...processedParts];
processedParts = processedParts.map(part => part.slice(0, 2));
lengthStepContent.push(
`Truncate to first 2 characters:\nRed: "${beforeParts[0]}" → "${processedParts[0]}"\nGreen: "${beforeParts[1]}" → "${processedParts[1]}"\nBlue: "${beforeParts[2]}" → "${processedParts[2]}"`
);
}
steps.push({
title: "5. Handle Length",
content: lengthProcessingNeeded
? lengthStepContent.join("\n\n")
: `Step skipped: Components already at correct length (${processedParts[0].length} characters)\nRed: "${processedParts[0]}"\nGreen: "${processedParts[1]}"\nBlue: "${processedParts[2]}"`,
value: processedParts.join(",")
});
// Step 6: Final Assembly
const finalColor = processedParts.join("");
steps.push({
title: "6. Final Assembly",
content: `Combine RGB components: ${processedParts.join(" + ")} = ${finalColor}`,
value: finalColor
});
return {
steps,
finalColor
};
}
function updateVisualization() {
const input = colorInput.value;
const { steps, finalColor } = visualizeIEColor(input);
const stepsContainer = document.getElementById("steps");
stepsContainer.innerHTML = steps
.map(
(step) => `
<div class="step">
<div class="step-title">${step.title}</div>
<pre class="step-content">${step.content}</pre>
</div>
`
)
.join("");
const colorHex = "#" + finalColor;
colorBox.style.backgroundColor = colorHex;
colorCode.textContent = colorHex;
}
const colorInput = document.getElementById("colorInput");
const colorBox = document.getElementById("colorBox");
const colorCode = document.getElementById("colorCode");
colorInput.addEventListener("input", updateVisualization);
colorInput.value = "chucknorris";
updateVisualization();
Also see: Tab Triggers