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.
<button>Hover for a new image</button>
html, body {
min-height: 400px;
}
button {
background: #ddd;
border: none;
font-size: 20px;
padding: 10px 20px;
}
.tippy-tooltip.ajax-theme {
position: absolute;
width: 200px;
padding: 0;
overflow: hidden;
}
.tippy-tooltip.ajax-theme img {
display: block;
max-width: 100%;
}
/*
We need to "pin" the tooltip absolutely to the bottom or top of the popper element depending on the placement.
*/
.tippy-tooltip.ajax-theme[x-placement^='top'] {
top: auto !important;
bottom: 0;
}
.tippy-tooltip.ajax-theme[x-placement^='bottom'] {
bottom: auto !important;
top: 0;
}
const INITIAL_CONTENT = '<div style="margin:5px 0;">Loading...</div>'
function applyStyles(popper, tooltip) {
// Because the tooltip has `position: absolute`,
// it no longer affects the parent popper's layout.
// We need to explicitly give it a width.
popper.style.width = '200px'
// Setup transition styles on the tooltip itself
tooltip.style.transitionDuration = '0.2s'
tooltip.style.transitionProperty = 'visibility, opacity, height'
}
function animateHeight(instance, instanceContent) {
const { popper } = instance
const { tooltip, content } = instance.popperChildren
function onTransitionEnd(event) {
if (event.target === event.currentTarget) {
content.style.opacity = '1'
instance.setContent(instanceContent)
}
}
// Wait until the height transition has finished before
// fading the content in. Since we have `overflow: hidden`
// on the tooltip this isn't actually needed, but if you
// have an arrow element it will be.
if (!instance._transitionEndListener) {
instance._transitionEndListener = onTransitionEnd
}
tooltip.addEventListener('transitionend', onTransitionEnd)
// Store the base height of the tooltip when it has the
// initial Loading... content.
if (!instance._baseHeight) {
instance._baseHeight = tooltip.clientHeight
}
// Here is where we find out the height of the tooltip
// when it has the content. We could technically hardcode
// 200px as the value, but it's useful to know how to do
// this with dynamic content.
content.style.opacity = '0'
// Temporarily set the image as the tooltip's content
// so we can find out the final height of the tooltip.
instance.setContent(instanceContent)
const height = tooltip.clientHeight
// Apply the height to the parent popper element.
popper.style.height = height + 'px'
// Reset the tooltip's height to the base height.
tooltip.style.height = instance._baseHeight + 'px'
// Cause reflow so we can start the height transition.
void tooltip.offsetHeight
// Start the transition.
tooltip.style.height = height + 'px'
// Remove the Loading... content and wait until the
// transition finishes.
instance.setContent('')
}
tippy('button', {
content: INITIAL_CONTENT,
animation: 'fade',
animateFill: false,
theme: 'ajax',
async onShow(instance) {
if (instance.state.isFetching === true || instance.state.canFetch === false) {
return
}
instance.state.isFetching = true
instance.state.canFetch = false
applyStyles(instance.popper, instance.popperChildren.tooltip)
try {
const response = await fetch('https://unsplash.it/200/?random')
const blob = await response.blob()
const url = URL.createObjectURL(blob)
// If the tooltip hid before finishing the request, stop further action
if (!instance.state.isVisible) {
return
}
const img = new Image()
img.width = 200
img.height = 200
img.src = url
animateHeight(instance, img)
} catch (error) {
instance.setContent('An error occurred')
} finally {
instance.state.isFetching = false
}
},
onHidden(instance) {
const { tooltip } = instance.popperChildren
instance.state.canFetch = true
instance.setContent(INITIAL_CONTENT)
tooltip.style.height = null
tooltip.removeEventListener('transitionend', instance._transitionEndListener)
instance._transitionEndListener = null
}
})
Also see: Tab Triggers