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.
<main :class="darkMode == 'dark' ? 'dark' : ''">
<div id="app" :style="{ boxShadow, borderColor }">
<div id="swatch-container">
<div id="swatch" :style="{ backgroundColor, borderColor }"></div>
<form id="dark-mode-toggle">
<input type="radio" v-model="darkMode" id="light" name="dark-mode" value="light">
<label for="light"><span class="sr">Light mode</span></label>
<input type="radio" v-model="darkMode" id="dark" name="dark-mode" value="dark">
<label for="dark"><span class="sr">Dark mode</span></label>
</form>
</div>
<form id="app-form" action="" :style="{ borderColor }">
<h1>Automatic RGB/Hex color converter</h1>
<div id="value-wrap" class="group">
<label for="value">Enter a color:</label>
<div>
<input v-model="value" @input="validate" id="value" type="text" placeholder="Any RGB or hex value" />
</div>
</div>
<div class="group">
<ul>
<li><strong>Format detected:</strong> {{ fromFormat }}</li>
<li><strong>Converting to:</strong> {{ toFormat }}</li>
</ul>
</div>
<div class="flex group">
<label for="result">Converted value:</label>
</div>
<div class="flex">
<input ref="output" id="result" tabindex="-1" type="text" :value="convertedValue" />
<button @click.prevent="copy" :disabled="!convertedValue" type="button" ref="copyButton">{{ copyButtonText }}</button>
</div>
</form>
</div>
</main>
$gray: #333;
$cardBG: #f8f8e8;
$bg: #ddd;
$orange: #dd8800;
$size: 8;
*,
*:before,
*:after {
box-sizing: inherit;
color: $gray;
font-size: inherit;
background: transparent;
transition: color 0.3s, background 0.3s;
font-family: inherit;
}
*:focus,
input[type="radio"]:focus + label {
outline: 2px dashed $orange;
outline-offset: 2px;
}
.sr {
position: absolute;
clip: rect(1px, 1px, 1px, 1px);
width: 1px;
height: 1px;
left: -100vh;
opacity: 0;
}
body {
margin: 0;
}
main {
display: grid;
place-items: center;
min-height: 100vh;
font-family: "Spartan";
font-size: 18px;
box-sizing: border-box;
color: #333;
background: $bg;
text-align: left;
}
#app {
width: 100%;
max-width: 26rem;
padding: 4rem 1rem;
border: 2px solid;
box-shadow: 0.5rem 0.5rem 0 rgba(0, 0, 0, 0.25);
position: relative;
background: $cardBG;
margin: 3rem auto 0;
h1 {
font-size: 1.8rem;
}
#swatch-container {
#dark-mode-toggle {
display: flex;
align-items: center;
justify-content: center;
margin-top: 0.5rem;
position: relative;
z-index: 2;
background: transparent;
input[type="radio"] {
@extend .sr;
& + label {
width: 1.25rem;
height: 1.25rem;
display: block;
border-radius: 1.25rem;
border: 2px solid;
background: $cardBG;
position: relative;
&[for="dark"] {
background: $gray;
}
&:not(:last-child) {
margin-right: 0.5rem;
}
}
&:checked + label:after {
content: "";
border-radius: 2rem;
width: calc(1.5rem + 4px);
height: calc(1.5rem + 4px);
border: 2px solid;
display: block;
background: transparent;
left: -6px;
top: -6px;
position: absolute;
}
}
}
}
#swatch {
width: #{$size}rem;
height: #{$size}rem;
border-radius: #{$size}rem;
border: 2px solid;
position: absolute;
top: #{$size * -0.5}rem;
left: calc(50% - #{$size * 0.5}rem);
background-color: $cardBG;
}
ul {
padding: 0;
list-style-type: none;
li + li {
margin-top: 0.5rem;
}
}
#app-form {
margin: auto;
input,
button {
padding: 0.5em;
border-radius: 0.25em;
border: 2px solid;
font-size: 1rem;
}
input,
button {
font-family: "Fira Code", monospace;
}
button {
padding: 0.5em 1em;
font-weight: bold;
&:disabled {
opacity: 0.4;
}
}
label,
input,
.label {
display: block;
width: 100%;
}
input {
padding: 0.75em;
}
label,
.label {
margin: 0.25em 0;
font-weight: bold;
}
#result {
border: none;
border-radius: 0;
padding-left: 0;
border-bottom: 2px solid;
}
.flex {
display: flex;
align-items: stretch;
flex-wrap: wrap;
@media (min-width: 440px) {
flex-wrap: nowrap;
& > *:not(:last-child) {
margin-right: 1rem;
}
}
}
.group {
margin-top: 3rem;
text-align: left;
}
.method {
font-weight: bold;
}
}
//#app
@media (min-width: 440px) {
padding: 4rem 3rem;
}
@media (min-width: 768px) {
display: grid;
grid-template-columns: auto 1fr;
grid-gap: 3rem;
max-width: 768px;
align-items: center;
text-align: left;
margin: 0 auto;
#swatch {
$size: 12;
position: relative;
left: unset;
top: unset;
width: #{$size}rem;
height: #{$size}rem;
border-radius: #{$size}rem;
}
#app-form {
padding: 2rem 0 2rem 3rem;
border-left: 2px dashed;
}
}
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.8s;
}
.fade-enter,
.fade-leave-to {
opacity: 0;
transform: translateY(2rem);
}
#dark-mode-switch {
position: absolute;
z-index: 3;
width: 3rem;
height: 2rem;
border: 2px solid;
left: 0.5rem;
top: 0.5rem;
border-radius: 3rem;
}
main.dark {
color: #fff;
$gray: #fff;
$cardBG: #333;
$bg: #222;
$orange: #dd8800;
background: $bg;
* {
color: $gray;
}
#app {
background: $cardBG;
box-shadow: 0.5rem 0.5rem 0 rgba(0, 0, 0, 0.7);
#swatch-container {
#dark-mode-toggle {
input[type="radio"] {
&:checked + label:after {
border: 2px solid #fff;
}
}
}
#swatch {
background-color: $cardBG;
}
}
}
}
const noMethod = { from: null, to: null };
const app = new Vue({
data: () => ({
base16: "0123456789abcdef",
hexRange: [],
value: "",
method: noMethod,
copied: false,
darkMode: "light"
}),
created() {
const digits = this.base16.split("");
digits.forEach((outer) => {
digits.forEach((inner) => {
this.hexRange.push(outer + inner);
});
});
},
watch: {
value(next, prev) {
if (
next.match(/^rgba/) ||
(next.match(/,/g) && next.match(/,/g).length == 3)
) {
this.method = { from: "RGBA", to: "Hex + alpha" };
} else if (["r", "R"].includes(next.substr(0, 1)) || next.includes(",")) {
this.method = { from: "RGB", to: "Hex" };
} else if (next.match(/^#?([a-f0-9]{4}$|[a-f0-9]{8})/i)) {
this.method = { from: "Hex + alpha", to: "RGBA" };
} else if (next.match(/[a-f0-9]+/i)) {
this.method = { from: "Hex", to: "RGB" };
} else {
this.method = noMethod;
}
}
},
methods: {
convert(val) {
if (val.match(/^rgb.*/) || val.match(/.*,.*/)) {
return this.rgbToHex(val);
} else {
return this.hexToRGB(val);
}
},
hexToRGB(val) {
//Remove '#' from string if present
if (val.substr(0, 1) === "#") {
val = val.substr(1);
}
//Make sure we have the right number of characters
if ([3, 4, 6, 8].includes(val.length)) {
//Allow for hex shorthand (3 or 4 characters)
const division = val.length >= 6 ? 2 : 1;
const color = {
r: { from: val.substr(0, division) },
g: { from: val.substr(division, division) },
b: { from: val.substr(division * 2, division) },
a: { from: val.substr(division * 3, division) }
};
//Set single-character shorthands to double-character hexes
if (division === 1) {
["r", "g", "b", "a"].forEach((col) => {
color[col].from += color[col].from;
});
}
//Matches the hex values to their 0-255 counterparts
this.hexRange.forEach((hex, index) => {
Object.keys(color).forEach((key) => {
if (color[key].from.toLowerCase() == hex) {
color[key].to = index;
}
});
});
//See if an alpha value was provided (return RGBA or RGB)
if (color.a.to) {
return `rgba(${color.r.to}, ${color.g.to}, ${color.b.to}, ${(
color.a.to / 255
).toFixed(2)})`;
} else {
return `rgb(${color.r.to}, ${color.g.to}, ${color.b.to})`;
}
} else {
return null;
}
},
rgbToHex(val) {
//Don't try to do stuff if we don't have two commas and a number
if (!val.match(/.*,.*,[0-9\s]+/)) return;
//Split up the RGB(A) string on commas and match numbers
const rgba = val.split(",");
const vals = rgba.map((color) => color && color.match(/[0-9\.]+/));
let invalid = false;
//Make sure none of the numbers are larger than 255
vals.forEach((val) => {
if (val > 255) invalid = true;
});
//Make sure we have 3 or 4 numbers (RGB or RGBA)
if ([3, 4].includes(vals.length)) {
let finalHex = "";
vals.forEach((color, colorIndex) => {
//If we're dealing with the alpha value…
if (colorIndex == 3)
color = Math.ceil(color * 255 > 255 ? 255 : color * 255);
this.hexRange.forEach((hex, hexIndex) => {
if (color == hexIndex) finalHex += hex;
});
});
return invalid ? null : `#${finalHex}`;
}
},
validate(key) {
//Only allow the characters that go into RGBA and/or hex volors
this.value = this.value.replace(/[^#a-f0-9rg\(\),\s\.]/, "");
},
copy() {
this.$refs.output.select();
document.execCommand("copy");
this.copied = true;
this.$refs.copyButton.focus();
setTimeout(() => {
this.copied = false;
}, 1200);
}
},
computed: {
convertedValue() {
return this.convert(this.value);
},
fromFormat() {
return this.method && this.method.from ? this.method.from : "…";
},
toFormat() {
return this.method && this.method.to ? this.method.to : "…";
},
copyButtonText() {
return this.copied ? "Copied!" : "Copy";
},
boxShadow() {
return `.5rem .5rem 0 ${this.convert(this.value) || "rgba(0,0,0,.25)"}`;
},
borderColor() {
return this.convert(this.value) || "inherit";
},
backgroundColor() {
return this.convert(this.value) || "";
}
}
});
app.$mount(document.querySelector("main"));
Also see: Tab Triggers