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.
#app
*
*:after
box-sizing border-box
-webkit-tap-highlight-color transparent
body
align-items center
background #fff
display flex
justify-content center
min-height 100vh
flex-direction column
font-family -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif
cursor var(--cursor)
.hsl-slider
--handle-size 50
width 280px
height 20px
border 4px solid #111
border-radius 4px
position relative
max-width 70vw
cursor pointer
background 'linear-gradient(90deg, hsl(0, %s, %s), hsl(36, %s, %s), hsl(72, %s, %s), hsl(108, %s, %s), hsl(144, %s, %s), hsl(180, %s, %s), hsl(216, %s, %s), hsl(252, %s, %s), hsl(288, %s, %s), hsl(324, %s, %s), hsl(360, %s, %s))' % (calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%))
&__arc
height calc(var(--handle-size) * 2px + 4px)
width calc(var(--handle-size) * 2px + 4px)
position absolute
top 50%
left calc(var(--value, 0) * 1%)
transform translate(-50%, -50%)
border-radius 100%
opacity 0.2
border 4px solid #c6c6c6
z-index -1
-webkit-clip-path polygon(0 0, 100% 0, 50% 50%, 100% 100%, 0 100%, 50% 50%)
clip-path polygon(0 0, 100% 0, 50% 50%, 100% 100%, 0 100%, 50% 50%)
&__handle
height calc(var(--handle-size) * 1px)
width calc(var(--handle-size) * 1px)
border-radius 100%
background 'hsl(%s, %s, %s)' % (var(--hue, 0) calc(var(--saturation, 100) * 1%) calc(var(--lightness, 50) * 1%))
border 4px solid #111
touch-action none
position absolute
top 50%
left calc(var(--value, 0) * 1%)
transform translate(-50%, -50%)
cursor var(--cursor)
&--lightness
&--saturation
left 50%
width 100%
height 100%
background transparent
border 0
transform translate(-50%, -50%) rotate(90deg) rotate(calc(var(--angle) * 1deg)) translate(0, calc(var(--handle-size) * 1px))
&:before
display none
&:after
content ''
position absolute
height 50%
width 50%
border 4px solid #111
border-radius 100%
top 50%
left 50%
transform translate(-50%, -50%)
&--lightness:after
background 'hsl(0, 0%, %s)' % (calc(var(--lightness, 50) * 1%))
&--saturation:after
background 'linear-gradient(90deg, hsl(0, %s, 50%), hsl(36, %s, 50%), hsl(72, %s, 50%), hsl(108, %s, 50%), hsl(144, %s, 50%), hsl(180, %s, 50%), hsl(216, %s, 50%), hsl(252, %s, 50%), hsl(288, %s, 50%), hsl(324, %s, 50%), hsl(360, %s, 50%))' % (calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%) calc(var(--saturation, 50) * 1%))
.container
position relative
.values
font-size .5rem
position absolute
color #ccc
top calc(100% + 100px)
const { React, ReactDOM, PropTypes, w3color } = window
const { useEffect, useState, useRef } = React
const { render } = ReactDOM
const rootNode = document.getElementById('app')
/**
* Grab the angle between two points
* @param {Object} event - pointer event for current coords
* @param {Object} element - element to use as center point
* @param {Number} buffer - buffer so that angle doesn't allow handle + track overlap
*/
const getAngle = (event, element, buffer) => {
const { clientX: x, clientY: y } =
event.touches && event.touches.length ? event.touches[0] : event
const {
x: handleX,
y: handleY,
width: handleWidth,
height: handleHeight,
} = element.getBoundingClientRect()
const handleCenterPoint = {
x: handleX + handleWidth / 2,
y: handleY + handleHeight / 2,
}
const angle =
(Math.atan2(handleCenterPoint.y - y, handleCenterPoint.x - x) * 180) /
Math.PI
return Math.max(buffer, Math.min(180 - buffer, Math.abs(angle)))
}
const getInitialAngle = (value, buffer) => {
return ((180 - buffer * 2) / 100) * value + buffer
}
const HslSlider = ({
hue: propsHue = 180,
saturation: propsSaturation = 100,
lightness: propsLightness = 50,
handleSize = 50,
BUFFER = 40,
onChange,
}) => {
const [hue, setHue] = useState(propsHue)
const [cursor, setCursor] = useState('grab')
const [lightness, setLightness] = useState(propsLightness)
const [saturation, setSaturation] = useState(propsSaturation)
const [saturationAngle, setSaturationAngle] = useState(
getInitialAngle(saturation, BUFFER)
)
const [lightnessAngle, setLightnessAngle] = useState(
180 + (180 - getInitialAngle(lightness, BUFFER))
)
const handleRef = useRef(null)
const trackRef = useRef(null)
/**
* Updates the hue based on the pointer position
* @param {Number} x - Where pointer is in relation to hue track
*/
const updateHue = e => {
// Return if we are tapping a handle
if (e.target.dataset.hslSliderHandle) return
const { clientX: x } = e.touches && e.touches.length ? e.touches[0] : e
const {
left: trackLeft,
width: trackWidth,
} = trackRef.current.getBoundingClientRect()
const newValue = (x - trackLeft) / trackWidth
setHue(Math.max(0, Math.min(360, newValue * 360)))
}
/**
*
* @param {Object} event - pointer event
*/
const updateLightness = event => {
const angle = getAngle(event, handleRef.current, BUFFER)
const lightness = ((angle - BUFFER) / (180 - BUFFER * 2)) * 100
setLightnessAngle(180 + (180 - angle))
setLightness(lightness)
}
const updateSaturation = event => {
const angle = getAngle(event, handleRef.current, BUFFER)
const saturation = ((angle - BUFFER) / (180 - BUFFER * 2)) * 100
setSaturation(saturation)
setSaturationAngle(angle)
}
/**
* Convenience method to handle event listening on handles
* This will remove the event listeners on pointerUp
* @param {Function} onMove - onMove handler for handle
*/
const handleUp = onMove => {
const up = () => {
setCursor('grab')
document.documentElement.style.setProperty('--cursor', 'initial')
window.removeEventListener('mousemove', onMove)
window.removeEventListener('touchmove', onMove)
window.removeEventListener('mouseup', up)
window.removeEventListener('touchend', up)
}
return up
}
/**
* Convenience method to assign event listening for handles
* @param {Function} onMove - onMove handler for handle
* @param {Bool} stopPropagation - defines whether to stop event propagation
*/
const handleDown = (onMove, stopPropagation) => e => {
if (stopPropagation) e.stopPropagation()
setCursor('grabbing')
document.documentElement.style.setProperty('--cursor', 'grabbing')
window.addEventListener('mousemove', onMove)
window.addEventListener('touchmove', onMove)
window.addEventListener('mouseup', handleUp(onMove))
window.addEventListener('touchend', handleUp(onMove))
}
useEffect(() => {
if (onChange) onChange({ hue, saturation, lightness })
}, [hue, saturation, lightness])
useEffect(() => {
trackRef.current.addEventListener('click', updateHue)
return () => {
trackRef.current.removeEventListener('click', updateHue)
}
}, [])
return (
<div
ref={trackRef}
className="hsl-slider"
style={{
'--handle-size': handleSize,
'--lightness': lightness,
'--saturation': saturation,
}}>
<div
className="hsl-slider__arc"
style={{
'--value': Math.max(0, Math.min(100, (hue / 360) * 100)),
}}
/>
<div
className="hsl-slider__handle"
ref={handleRef}
style={{
'--cursor': cursor,
'--value': Math.max(0, Math.min(100, (hue / 360) * 100)),
'--hue': hue,
}}
onMouseDown={handleDown(updateHue)}
onTouchStart={handleDown(updateHue)}
title="Set hue">
<div
data-hsl-slider-handle="true"
className="hsl-slider__handle hsl-slider__handle--saturation"
onMouseDown={handleDown(updateSaturation, true)}
onTouchStart={handleDown(updateSaturation, true)}
style={{ '--angle': saturationAngle }}
title="Set saturation"
/>
<div
data-hsl-slider-handle="true"
className="hsl-slider__handle hsl-slider__handle--lightness"
onMouseDown={handleDown(updateLightness, true)}
onTouchStart={handleDown(updateLightness, true)}
style={{ '--angle': lightnessAngle }}
title="Set lightness"
/>
</div>
</div>
)
}
HslSlider.propTypes = {
hue: PropTypes.number,
handleSize: PropTypes.number,
saturation: PropTypes.number,
lightness: PropTypes.number,
BUFFER: PropTypes.number,
onChange: PropTypes.func,
}
const App = () => {
const [color, setColor] = useState(new w3color('hsl(180, 100%, 50%'))
const onChange = ({ hue, saturation, lightness }) => {
setColor(new w3color(`hsl(${hue}, ${saturation}%), ${lightness}%`))
}
return (
<div className="container" style={{ '--color': color.toHslString() }}>
<HslSlider
hue={color.hue}
saturation={color.sat * 100}
lightness={color.lightness * 100}
onChange={onChange}
/>
<div className="values">
<h1>{color.toHslString()}</h1>
<h1>{color.toHexString()}</h1>
<h1>{color.toRgbString()}</h1>
</div>
</div>
)
}
render(<App />, rootNode)
Also see: Tab Triggers