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 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.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<div id="app">
<div>
<div id="container" class="container" @mouseup="selectEnd" @mouseleave="selectEnd" @mousemove="dragPicker">
<p class="title">Big Color Picker</p>
<div class="inner">
<div class="colorpicker-wrap">
<div id="color-area" class="colorpicker" @mousedown="colorSelect" :style="pickerStyle">
<div :style="{left:colorCursor.left + 'px',top:colorCursor.top + 'px'}" class="colorpicker-cursor" :class="colorCursorState"></div>
</div>
</div>
<div id="huepicker" class="huepicker" @mousedown="hueSelectStart">
<div @mousedown="hueCursorClick" class="huepicker-cursor" :style="{top:hueCursorPos + 'px'}"></div>
</div>
<div class="preview_area">
<div class="preview-wrap">
<div class="preview" :style="{backgroundColor:'#' + rgb2hex,opacity:alpha}"></div>
</div>
<div class="colorcode-wrap">
<div class="colorcode-boxes-wrap">
<div class="colorcode-boxes">
<div class="colorcode-box">
<span>R</span><input type="text" :value="rgb.r" @keydown="colorNumInput('r', arguments[0])" @blur="colorNumInput('r', arguments[0])">
</div>
<div class="colorcode-box">
<span>G</span><input type="text" :value="rgb.g" @keydown="colorNumInput('g', arguments[0])" @blur="colorNumInput('g', arguments[0])">
</div>
<div class="colorcode-box">
<span>B</span><input type="text" :value="rgb.b" @keydown="colorNumInput('b', arguments[0])" @blur="colorNumInput('b', arguments[0])">
</div>
<div class="colorcode-box">
<span>A</span><input type="text" :value="alphaInt" @keydown="colorNumInput('a', arguments[0])" @blur="colorNumInput('a', arguments[0])">
</div>
</div>
<div class="colorcode-boxes">
<div class="colorcode-box">
<span>H</span><input type="text" :value="hsv.h" @keydown="colorNumInput('h', arguments[0])" @blur="colorNumInput('h', arguments[0])">
</div>
<div class="colorcode-box">
<span>S</span><input type="text" :value="hsv.s" @keydown="colorNumInput('s', arguments[0])" @blur="colorNumInput('s', arguments[0])">
</div>
<div class="colorcode-box">
<span>V</span><input type="text" :value="hsv.v" @keydown="colorNumInput('v', arguments[0])" @blur="colorNumInput('v', arguments[0])">
</div>
</div>
</div>
<input type="text" class="colorcode-rgba" :value="rgbaCode" @blur="rgbaCodeInput" @keyup.13="rgbaCodeInput" @focus="focusAndSelect">
<input type="text" class="colorcode-hex" :value="'#' + rgb2hex" @blur="hexInput" @keyup.13="hexInput" @focus="focusAndSelect">
</div>
</div>
</div>
</div>
</div>
</div>
.container {
position: absolute;
width: 710px;
top: 20px;
left: 20px;
padding: 0px 13px 35px 35px;
text-align: center;
border-radius: 12px 12px 5px 5px;
background: rgb(53, 53, 53);
box-shadow: rgba(0, 0, 0, 0.5) 0px 5px 30px,
rgba(255,255,255,1) 0 1px 1px inset,
rgba(0,0,0,0.3) 0 -2px 1px inset;
}
.title {
display: inline-block;
text-align: center;
color: rgb(204, 204, 204);
font-family: arial;
padding-bottom: 3px;
width: 100%;
margin:14px 0;
}
.inner {
display: flex;
justify-content: space-between;
}
.colorpicker-wrap {
background-color:#fff;
background-image: linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc),
linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc);
background-position: 0 0, 12px 12px;
background-size: 24px 24px;
}
.colorpicker {
position: relative;
overflow: hidden;
width: 410px;
height: 410px;
background-color: rgb(255, 0, 0);
background-image: linear-gradient(rgba(0, 0, 0, 0), rgb(0, 0, 0)), linear-gradient(to right, rgb(255, 255, 255), rgba(255, 255, 255, 0));
}
.colorpicker-cursor {
pointer-events: none;
position: absolute;
width: 17px;
height: 17px;
left: 400px;
top: -9px;
box-sizing: border-box;
background: linear-gradient(transparent 46%, white 46%, white 53%, transparent 53%), linear-gradient(90deg, transparent 47%, white 47%, white 53%, transparent 53%);
border: 1px solid white;
border-radius: 50%;
}
.colorpicker-cursor.black {
border-color:black;
background: linear-gradient(transparent 46%, black 46%, black 53%, transparent 53%), linear-gradient(90deg, transparent 47%, black 47%, black 53%, transparent 53%);
}
.huepicker {
position: relative;
width: 70px;
height: 410px;
background-image: linear-gradient(
hsl(360,100%,50%),
hsl(300,100%,50%),
hsl(240,100%,50%),
hsl(180,100%,50%),
hsl(120,100%,50%),
hsl(60,100%,50%),
hsl(0,100%,50%)
);
}
.huepicker-cursor {
position: absolute;
right: -13px;
border-right: 10px solid rgb(255, 255, 255);
border-top: 7px solid transparent;
border-bottom: 7px solid transparent;
}
.preview-wrap {
margin-right: 20px;
margin-bottom: 20px;
background-color:#fff;
background-image: linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc),
linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%, #ccc);
background-position: 0 0, 10px 10px;
background-size: 20px 20px;
}
.preview {
width: 150px;
height: 120px;
background: rgb(255, 0, 0);
}
.colorcode-wrap {
color: rgb(255, 255, 255);
font-size: 17px;
}
.colorcode-boxes-wrap {
display: flex;
flex-direction: row;
justify-content: flex-start;
}
.colorcode-boxes {
flex-basis: 41%;
margin-right: 8px;
margin-bottom: 15px;
}
input {
border-radius: 2px;
border: 1px solid #777;
background: #555;
color: #FFF;
}
.colorcode-box span {
display: inline-block;
width: 18px;
font-size: 15px;
}
.colorcode-box input{
display: inline-block;
padding: 5px 7px;
width: 28px;
margin-bottom:10px;
margin-left: 3px;
font-size: 15px;
}
.colorcode-rgba {
display: block;
width: 136px;
margin-bottom:15px;
padding: 8px 5px;
font-size: 13px;
}
.colorcode-hex {
display: block;
width: 136px;
padding: 5px;
font-size: 17px;
}
new Vue({
el: "#app",
data: function() {
return {
rgb: {
r: 255,
g: 0,
b: 0
},
hsv: {
h: 360,
s: 100,
v: 100
},
alpha: 1,
colorPickerSize: 410,
hueChangeFlg: false,
hueCursorClickFlg: false,
hueCursorSize: 14,
colorChangeFlg: false,
colorCursorSize: 16,
container: {
left: 40,
top: 40
},
mouseDownFlg: false
};
},
methods: {
//色相領域クリック
hueSelectStart: function(event) {
this.hueChangeFlg = true;
var ajustY = 0;
if (this.hueCursorClickFlg) {
ajustY = this.hueCursorPos + this.hueCursorSize/2;
}
this.hsv.h = Math.round(
Math.abs((event.offsetY + ajustY) * this.hueUnit - 360)
);
this.rgb = this.hsv2rgb;
},
//色相カーソルクリック
hueCursorClick: function() {
this.hueCursorClickFlg = true;
},
//カラーピッカークリック
colorSelect: function(event) {
this.colorChangeFlg = true;
this.hsv.s = Math.round((event.offsetX) * this.colorUnit);
this.hsv.v = Math.round(
Math.abs((event.offsetY) * this.colorUnit - 100)
);
this.rgb = this.hsv2rgb;
},
//ドラッグ時
dragPicker: function(event) {
var cx = event.pageX;
var cy = event.pageY - document.documentElement.scrollTop;
var container = document.getElementById("container");
var colorArea = document.getElementById("color-area");
var conT = container.offsetTop;
var conL = container.offsetLeft;
//色相ドラッグ
if (this.hueChangeFlg) {
var hueArea = document.getElementById("huepicker");
var ht = hueArea.offsetTop + conT;
var hh = hueArea.clientHeight;
var hueCursorPos = cy - ht;
if (hueCursorPos < 0) {
hueCursorPos = 0;
} else if (hueCursorPos > hh) {
hueCursorPos = hh;
}
this.hsv.h = Math.round(Math.abs(this.hueUnit * hueCursorPos - 360));
this.rgb = this.hsv2rgb;
}
//カラーピッカードラッグ
if (this.colorChangeFlg) {
var ct = colorArea.offsetTop + conT;
var cl = colorArea.offsetLeft + conL;
var ch = colorArea.clientHeight;
var cw = colorArea.clientWidth;
var colorCursorPosY = cy - ct;
if (colorCursorPosY < 0) {
colorCursorPosY = 0;
} else if (colorCursorPosY > ch) {
colorCursorPosY = ch;
}
var colorCursorPosX = cx - cl;
if (colorCursorPosX < 0) {
colorCursorPosX = 0;
} else if (colorCursorPosX > cw) {
colorCursorPosX = cw;
}
this.hsv.s = Math.round(colorCursorPosX * this.colorUnit);
this.hsv.v = Math.round(
Math.abs(colorCursorPosY * this.colorUnit - 100)
);
this.rgb = this.hsv2rgb;
}
},
//マウスアップで全フラグを解除
selectEnd: function() {
this.hueChangeFlg = false;
this.colorChangeFlg = false;
this.hueCursorClickFlg = false;
},
//16進数コード入力
hexInput: function(event) {
var hex = event.target.value;
hex = hex.replace(/#/, "");
if (!hex.match(/[A-Fa-f0-9]+/)) {
return false;
} else {
if (hex.length != 6 && hex.length != 3) {
return false;
} else {
if (hex.length == 3) {
hex =
hex.charAt(0) +
hex.charAt(0) +
hex.charAt(1) +
hex.charAt(1) +
hex.charAt(2) +
hex.charAt(2);
}
}
}
this.rgb = this.hex2rgb(hex);
this.hsv = this.rgb2hsv;
},
//RGBA HSB入力時
colorNumInput: function(color, event) {
if (event.type != "keydown" && event.type != "blur") {
return false;
} else {
var val = event.target.value;
if (!val.match(/\d+/)) {
return false;
}
val = Number(val);
if (event.type == "keydown") {
if (
event.key != "Enter" &&
event.key != "ArrowUp" &&
event.key != "ArrowDown" &&
!event.shiftKey
) {
return false;
} else {
var changeNum = 1;
if (event.shiftKey) {
changeNum = 10;
}
if (event.key == "ArrowUp") {
val += changeNum;
} else if (event.key == "ArrowDown") {
val -= changeNum;
}
}
}
if (color == "r" || color == "b" || color == "g") {
if (val > 255) {
val = 255;
} else if (val < 0) {
val = 0;
}
if (color == "r") {
this.rgb.r = val;
} else if (color == "g") {
this.rgb.g = val;
} else if (color == "b") {
this.rgb.b = val;
}
this.hsv = this.rgb2hsv;
} else if (color == "a") {
if (val > 100) {
val = 100;
} else if (val < 0) {
val = 0;
}
this.alpha = (val / 100).toFixed(2);
} else if (color == "h" || color == "s" || color == "v") {
if (color == "h") {
if (val > 360) {
val = 360;
} else if (val < 0) {
val = 0;
}
this.hsv.h = val;
} else if (color == "s") {
if (val > 100) {
val = 100;
} else if (val < 0) {
val = 0;
}
this.hsv.s = val;
} else if (color == "v") {
if (val > 100) {
val = 100;
} else if (val < 0) {
val = 0;
}
this.hsv.v = val;
}
this.rgb = this.hsv2rgb;
}
}
},
//rgbaコード入力時
rgbaCodeInput: function(event) {
var rgbaCode = event.target.value;
rgbaCode = rgbaCode.replace(/rgba?/, "");
rgbaCode = rgbaCode.replace(/\(/, "");
rgbaCode = rgbaCode.replace(/\)/, "");
rgbaCode = rgbaCode.replace(/\ /, "");
var rgbaCodeAry = rgbaCode.split(",");
if (rgbaCodeAry.length < 3) {
return false;
} else {
if (
!rgbaCodeAry[0].match(/\d+/) ||
!rgbaCodeAry[1].match(/\d+/) ||
!rgbaCodeAry[2].match(/\d+/)
) {
return false;
}
}
this.rgb.r = rgbaCodeAry[0];
this.rgb.g = rgbaCodeAry[1];
this.rgb.b = rgbaCodeAry[2];
this.alpha = rgbaCodeAry[3];
this.hsv = this.rgb2hsv;
},
//テキストボックスフォーカス
focusAndSelect: function(event) {
event.target.select();
},
//16進数→RGB値
hex2rgb: function(hex) {
return {
r: parseInt(hex.slice(0, 2), 16),
g: parseInt(hex.slice(2, 4), 16),
b: parseInt(hex.slice(4), 16)
};
}
},
computed: {
rgbaCode: function() {
var alpha = this.alpha;
if (alpha == 1.0) {
alpha = 1;
} else if (alpha == 0.0) {
alpha = 0;
}
return (
"rgba(" +
this.rgb.r +
"," +
this.rgb.g +
"," +
this.rgb.b +
"," +
alpha +
")"
);
},
hueUnit: function() {
return 360 / this.colorPickerSize;
},
hueCursorPos: function() {
return Math.round(
Math.abs(this.hsv.h - 360) / this.hueUnit - this.hueCursorSize / 2
);
},
colorUnit: function() {
return 100 / this.colorPickerSize;
},
colorCursor: function() {
return {
left: this.hsv.s / this.colorUnit - this.colorCursorSize / 2,
top:
Math.abs(this.hsv.v / this.colorUnit - this.colorPickerSize) -
this.colorCursorSize / 2
};
},
//カラーピッカーのカーソルの位置によって色を変える
colorCursorState: function() {
var color = "white";
if (this.hsv.h <= 201 && this.hsv.h >= 22) {
if (this.hsv.v >= 50) {
color = "black";
} else {
color = "white";
}
} else {
if (this.hsv.v >= 50 && this.hsv.s <= 71) {
color = "black";
} else {
color = "white";
}
}
return color;
},
alphaInt: function() {
return Math.round(this.alpha * 100);
},
pickerStyle: function() {
return {
backgroundColor: "hsl(" + this.hsv.h + ",100%,50%)",
opacity: this.alpha
};
},
//RGB→16進数
rgb2hex: function() {
this.rgb.r = Math.round(this.rgb.r);
this.rgb.g = Math.round(this.rgb.g);
this.rgb.b = Math.round(this.rgb.b);
return (
("00" + this.rgb.r.toString(16)).slice(-2) +
("00" + this.rgb.g.toString(16)).slice(-2) +
("00" + this.rgb.b.toString(16)).slice(-2)
);
},
//RGB→HSV
rgb2hsv: function() {
var rr,
gg,
bb,
r = this.rgb.r / 255,
g = this.rgb.g / 255,
b = this.rgb.b / 255,
h,
s,
v = Math.max(r, g, b),
diff = v - Math.min(r, g, b),
diffc = function(c) {
return (v - c) / 6 / diff + 1 / 2;
};
if (diff == 0) {
h = s = 0;
} else {
s = diff / v;
rr = diffc(r);
gg = diffc(g);
bb = diffc(b);
if (r === v) {
h = bb - gg;
} else if (g === v) {
h = 1 / 3 + rr - bb;
} else if (b === v) {
h = 2 / 3 + gg - rr;
}
if (h < 0) {
h += 1;
} else if (h > 1) {
h -= 1;
}
}
if (this.hsv.h == 360 && h == 0) {
h = 1;
}
return {
h: Math.round(h * 360),
s: Math.round(s * 100),
v: Math.round(v * 100)
};
},
//HSV→RGB
hsv2rgb: function() {
var h = this.hsv.h;
var s = this.hsv.s;
var v = this.hsv.v;
var max = v;
var min = max - s / 255 * max;
var rgb = { r: 0, g: 0, b: 0 };
if (h == 360) {
h = 0;
}
s = s / 100;
v = v / 100;
if (s == 0) {
rgb.r = v * 255;
rgb.g = v * 255;
rgb.b = v * 255;
return rgb;
}
var dh = Math.floor(h / 60);
var p = v * (1 - s);
var q = v * (1 - s * (h / 60 - dh));
var t = v * (1 - s * (1 - (h / 60 - dh)));
switch (dh) {
case 0:
rgb.r = v;
rgb.g = t;
rgb.b = p;
break;
case 1:
rgb.r = q;
rgb.g = v;
rgb.b = p;
break;
case 2:
rgb.r = p;
rgb.g = v;
rgb.b = t;
break;
case 3:
rgb.r = p;
rgb.g = q;
rgb.b = v;
break;
case 4:
rgb.r = t;
rgb.g = p;
rgb.b = v;
break;
case 5:
rgb.r = v;
rgb.g = p;
rgb.b = q;
break;
}
rgb.r = Math.round(rgb.r * 255);
rgb.g = Math.round(rgb.g * 255);
rgb.b = Math.round(rgb.b * 255);
return rgb;
}
}
});
Also see: Tab Triggers