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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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.
#app
*
box-sizing border-box
body
margin 0
padding 2rem
background #222
color #fff
display flex
align-items center
justify-content center
font-family -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif
min-height 100vh
#app
min-height 100vh
display flex
align-items center
justify-content center
flex-direction column
width 50vmin
min-width 200px
transform-style preserve-3d
perspective 1000px
perspective-origin 50% 25%
.motion-element
height 40px
width 40px
position absolute
top 0%
left 0%
offset-path path(var(--path))
animation travel 2s infinite var(--animation-direction, normal) linear
transform-style var(--transform-style, 'none')
transform translate3d(0, 0, 20px)
&__side
background rgba(128,191,255,0.1)
border 2px #80bfff solid
height 100%
position absolute
width 100%
&:nth-of-type(1)
transform translate3d(0, 0, 20px)
&:nth-of-type(2)
transform translate3d(0, 0, -20px)
&:nth-of-type(3)
transform rotateX(90deg) translate3d(0, 0, -20px)
&:nth-of-type(4)
transform rotateX(90deg) translate3d(0, 0, 20px)
&:nth-of-type(5)
transform rotateY(90deg) translate3d(0, 0, 20px)
&:nth-of-type(6)
transform rotateY(-90deg) translate3d(0, 0, 20px)
.container
height 50vmin
width 50vmin
min-width 200px
min-height 200px
border 4px solid hsla(0, 0%, 100%, 0.5)
overflow hidden
position relative
resize both
margin-bottom 2rem
transform-origin bottom center
transform-style preserve-3d
transform rotateX(calc(var(--rotation, 0) * 1deg))
button
padding 8px 16px
details
width 100%
summary
margin-bottom 1rem
padding 1rem 0
path
fill none
stroke 'hsl(%s, 100%, 50%)' % var(--hue, 260)
stroke-width 4px
transition stroke .25s ease
svg
height 100%
width 100%
@keyframes travel
from
offset-distance 0%
to
offset-distance 100%
label
display block
margin-bottom 0.5rem
font-weight bold
input
display block
[type=text]
[type=number]
margin 0
padding 8px 16px
width 100%
a
color 'hsl(%s, 100%, 50%)' % var(--hue)
p
line-height 1.5
text-align left
width 100%
form
display grid
grid-gap 20px
.form-field
margin-bottom 1.25rem
.form-field--grid
display grid
grid-template-columns auto 1fr
grid-template-rows auto auto
grid-gap 20px 10px
const {
React: { Fragment, useEffect, useReducer, useRef },
ReactDOM: { render },
} = window
const PATH =
'M10.362 18.996s-6.046 21.453 1.47 25.329c10.158 5.238 18.033-21.308 29.039-18.23 13.125 3.672 18.325 36.55 18.325 36.55l12.031-47.544'
const INITIAL_STATE = {
alternate: false,
path: PATH,
svg: true,
height: 79.375,
width: 79.375,
threeD: false,
}
const formReducer = (state, action) => {
switch (action.type) {
case 'UPDATE':
return { ...state, [action.name]: action.value }
case 'DROP':
return { ...state, ...action.data }
default:
return state
}
}
const App = () => {
const containerRef = useRef(null)
const elementRef = useRef(null)
const pathRef = useRef(null)
const svgRef = useRef(null)
const motionPathRef = useRef(null)
const [state, dispatch] = useReducer(formReducer, INITIAL_STATE)
const { alternate, path, svg, threeD, width, height } = state
const onFileDrop = e => {
e.preventDefault()
const file = e.dataTransfer.files[0]
if (
file.type === 'image/svg+xml' ||
file.name.slice(file.name.length - 4) === '.svg'
) {
// process the file.
const reader = new FileReader()
reader.onloadend = response => {
try {
// file.target.result is the SVG markup we want to use.
const wrapper = document.createElement('div')
wrapper.innerHTML = response.target.result
const svg = wrapper.querySelector('svg')
const path = wrapper.querySelector('path')
const viewBox = svg.getAttribute('viewBox').split(' ') // 0 0 x2 y2
const pathString = path.getAttribute('d')
dispatch({
type: 'DROP',
data: {
path: pathString,
width: viewBox[2],
height: viewBox[3],
},
})
} catch (e) {
throw Error('Something went wrong', e)
}
}
reader.readAsText(file)
}
}
const prevent = e => e.preventDefault()
const updateField = e =>
dispatch({
type: 'UPDATE',
name: e.target.name,
value: e.target.type === 'checkbox' ? e.target.checked : e.target.value,
})
useEffect(() => {
if (containerRef.current) {
const containerRefObserver = new ResizeObserver(entries => {
if (motionPathRef.current) {
const newPath = motionPathRef.current.generatePath(containerRef.current.offsetWidth, containerRef.current.offsetHeight)
containerRef.current.style.setProperty('--path', `"${newPath}"`)
pathRef.current.setAttribute('d', newPath)
}
})
containerRefObserver.observe(containerRef.current)
}
}, [])
useEffect(() => {
if (containerRef.current && elementRef.current) {
// Set up the initial responsive motion path
motionPathRef.current = new Meanderer({
path,
height,
width,
})
const newPath = motionPathRef.current.generatePath(containerRef.current.offsetWidth, containerRef.current.offsetHeight)
containerRef.current.style.setProperty('--path', `"${newPath}"`)
pathRef.current.setAttribute('d', newPath)
}
}, [path, width, height])
useEffect(() => {
document.body.addEventListener('dragover', prevent)
document.body.addEventListener('drop', onFileDrop)
return () => {
document.body.removeEventListener('dragover', prevent)
document.body.removeEventListener('drop', onFileDrop)
}
}, [])
const hue = Math.random() * 360
return (
<Fragment>
<div
ref={containerRef}
className="container"
style={{
'--rotation': threeD ? 75 : 0,
overflow: threeD ? 'visible' : 'hidden',
}}>
<svg
{...(!svg && { hidden: true })}
ref={svgRef}
style={{
'--hue': hue,
}}>
<path ref={pathRef}></path>
</svg>
<div
ref={elementRef}
style={{
'--animation-direction': alternate ? 'alternate' : 'normal',
'--transform-style': threeD ? 'preserve-3d' : 'none',
}}
className="motion-element">
<div className="motion-element__side"></div>
<div className="motion-element__side"></div>
<div className="motion-element__side"></div>
<div className="motion-element__side"></div>
<div className="motion-element__side"></div>
<div className="motion-element__side"></div>
</div>
</div>
<p
style={{
'--hue': hue,
}}>
Drag and drop an optimized SVG file onto the page that contains a path.
Clean up your SVG first with{' '}
<a
href="https://jakearchibald.github.io/svgomg/"
target="_blank"
rel="noreferrer noopener">
SVGOMG
</a>
. Alternatively, manually enter path info into the configuration form
below.
</p>
<p>
Resize the viewport and see your motion path scale!{' '}
<span aria-label="TADA!" role="img">
🎉
</span>
</p>
<details>
<summary>Path configuration</summary>
<form onDrop={onFileDrop}>
<section className="form-field">
<label htmlFor="path">Path</label>
<input
id="path"
type="text"
name="path"
value={path}
onChange={updateField}
/>
</section>
<section className="form-field">
<label htmlFor="width">Initial Width (viewBox x2)</label>
<input
id="with"
type="number"
name="width"
value={width}
onChange={updateField}
/>
</section>
<section className="form-field">
<label htmlFor="height">Initial Height (viewBox y2)</label>
<input
id="height"
type="number"
name="height"
value={height}
onChange={updateField}
/>
</section>
<section className="form-field form-field--grid">
<label htmlFor="svg">Show SVG path?</label>
<input
id="svg"
type="checkbox"
name="svg"
checked={svg}
onChange={updateField}
/>
<label htmlFor="alternate">Alternate direction?</label>
<input
id="alternate"
type="checkbox"
name="alternate"
checked={alternate}
onChange={updateField}
/>
<label htmlFor="threeD">See path in 3D?</label>
<input
id="threeD"
type="checkbox"
name="threeD"
checked={threeD}
onChange={updateField}
/>
</section>
<section className="form-field form-field--grid">
<button
onClick={e => {
e.preventDefault()
containerRef.current.removeAttribute('style')
}}>
Reset container size
</button>
</section>
</form>
</details>
</Fragment>
)
}
render(<App />, document.getElementById('app'))
Also see: Tab Triggers