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.
<div class="section">
<h1>Tooltip Accessibility Demo</h1>
<blockquote>
<h2>IMPORTANT NOTE:</h2>
<p>I made this as an informal demo a while ago, and since then further user testing has shown that <kbd>Escape</kbd> is not a good key to dismiss tooltips, for <a href="https://www.youtube.com/watch?v=lb0_v7D4kbs">a variety of reasons</a>. I'd recommend using the <kbd>Control</kbd> key to dismiss instead.
</blockquote>
<p>A few examples of simple tooltips serving as the name or description of buttons and form fields. They work with mouse and keyboard, and are dismissable with <code>Escape</code> or by clicking the "x". This allows the tooltips to meet <a href="https://www.w3.org/WAI/WCAG21/Understanding/content-on-hover-or-focus.html">WCAG 2.1 Content on Hover or Focus</a>.</p>
</div>
<div class="section">
<h2>A text input with a tooltip helper</h2>
<label for="test1">A Name</label>
<div class="tooltip-wrapper">
<input id="test1" type="text" aria-describedby="tip1" class="tooltip-trigger">
<div class="tooltip" id="tip1">Any name you want</div>
</div>
</div>
<div class="section">
<h2>An icon button with name in tooltip</h2>
<p>This tooltip is a child of the button element, but could also live outside the button in conjunction with <code>aria-labelledby</code>.</p>
<button type="button" aria-labelledby="tip2" class="tooltip-wrapper tooltip-trigger">
<span class="tooltip" id="tip2">Delete</span>
<svg class="icon" viewBox="0 0 32 32" aria-hidden="true">
<path d="M4 10v20c0 1.1 0.9 2 2 2h18c1.1 0 2-0.9 2-2v-20h-22zM10 28h-2v-14h2v14zM14 28h-2v-14h2v14zM18 28h-2v-14h2v14zM22 28h-2v-14h2v14z"></path>
<path d="M26.5 4h-6.5v-2.5c0-0.825-0.675-1.5-1.5-1.5h-7c-0.825 0-1.5 0.675-1.5 1.5v2.5h-6.5c-0.825 0-1.5 0.675-1.5 1.5v2.5h26v-2.5c0-0.825-0.675-1.5-1.5-1.5zM18 4h-6v-1.975h6v1.975z"></path>
</svg>
</button>
</div>
<div class="section">
<h2>Make it better</h2>
<p>This pen does not do viewport collision detection, which would be a good idea to add for production-ready tooltips.</p>
<p>It also does not handle preventing multiple overlapping tooltips (e.g. with combined focus & hover) which would be a good idea, particularly when open tooltips may overlap, e.g. within a menubar or toolbar.</p>
</div>
/* Tooltip styles */
.tooltip-wrapper {
position: relative;
display: inline-block;
}
.tooltip {
position: absolute;
left: 50%;
bottom: 0;
transform: translate(-50%, calc(100% + 10px));
padding: 6px calc(10px + 1rem) 6px 10px;
border-radius: 4px;
background: rgba(0, 0, 0, 0.9);
color: #fff;
}
.tooltip::before {
content: "";
position: absolute;
top: -10px;
left: calc(50% - 10px);
border-bottom: 10px solid rgba(0, 0, 0, 0.9);
border-left: 10px solid transparent;
border-right: 10px solid transparent;
}
/* a designer could certainly style this better :) */
.tooltip-close {
position: absolute;
top: 0.25rem;
right: 0.25rem;
width: 1rem;
height: 1rem;
padding: 0;
border: 0;
border-radius: 50%;
box-shadow: 0 0 1px 1px rgba(200, 200, 200, 0.5);
background-color: transparent;
color: #fff;
font-size: 0.75rem;
line-height: 1rem;
}
/* Page/content styles: */
body {
background-color: #d4e4f6;
color: #0d2744;
flex-direction: column;
font-size: 18px;
padding: 20px;
}
h2 {
font-size: 1.4em;
font-weight: bold;
}
p {
line-height: 1.4;
margin-bottom: 1em;
}
blockquote {
border-left: 3px solid black;
margin-left: 0;
padding-left: 1.5em;
}
label {
display: block;
margin-bottom: 0.25em;
}
button {
padding: 0.5em 0.75em;
border: 1px solid #666;
border-radius: 3px;
background-color: #ddd;
}
input {
padding: 0.5em 0.75em;
border: 1px solid #666;
border-radius: 3px;
}
input:focus,
button:focus {
border-color: rgb(1, 104, 215);
box-shadow: 0 0 0 6px rgba(1, 104, 215, 0.5);
outline: none;
}
.section {
margin: 0 auto 2.5em;
max-width: 600px;
}
.icon {
display: inline-block;
width: 1em;
height: 1em;
stroke-width: 0;
stroke: currentColor;
fill: currentColor;
}
// Settings:
// Timeout to hide tooltip
const TIMEOUT_LENGTH = 500;
// global timeout map; quick n dirty
const timeouts = new WeakMap();
// if the user actively dismisses a tooltip, save that setting
// and do not show the tooltip again
const dismissals = new WeakMap();
// here we attach all event listeners to control the tooltip
function initTooltip(tooltipContainer: HTMLElement) {
const trigger = tooltipContainer.classList.contains('tooltip-trigger') ? tooltipContainer : tooltipContainer.querySelector('.tooltip-trigger');
const tooltip = tooltipContainer.querySelector('.tooltip');
// show tooltip on hover and focus
tooltipContainer.addEventListener('mouseenter', () => {
showTooltip(tooltip);
});
trigger.addEventListener('focus', () => {
showTooltip(tooltip);
});
// hide tooltip on mouse out and blur
// use timeout on mouse leave
tooltipContainer.addEventListener('mouseleave', () => {
timeoutTooltip(tooltip);
});
trigger.addEventListener('blur', () => {
hideTooltip(tooltip);
});
// hide the tooltip on escape key press
trigger.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
hideTooltip(tooltip);
// save dismissal
dismissals.set(tooltip, true);
}
});
// create a close button for pointer dismissal
const closeBtn = document.createElement('button');
closeBtn.innerHTML = 'X';
closeBtn.tabIndex = -1;
closeBtn.setAttribute('aria-hidden', 'true');
closeBtn.classList.add('tooltip-close');
tooltip.appendChild(closeBtn);
closeBtn.addEventListener('click', () => {
hideTooltip(tooltip);
// save dismissal
dismissals.set(tooltip, true);
});
// hide the tooltip in here so they show up without JS on
// debateably useful
tooltip.style.display = 'none';
}
function showTooltip(tooltip: HTMLElement) {
// do not show if tooltip has been dismissed
if (dismissals.has(tooltip)) {
return false;
}
tooltip.style.display = 'block';
tooltip.removeAttribute('aria-hidden');
// if a hide timeout exists for this tooltip, clear it
if (timeouts.has(tooltip)) {
window.clearTimeout(timeouts.get(tooltip));
}
}
function hideTooltip(tooltip: HTMLElement) {
tooltip.style.display = 'none';
tooltip.setAttribute('aria-hidden', 'true');
}
function timeoutTooltip(tooltip: HTMLElement) {
// hide the tooltip after a set amount of time
const timeoutId = window.setTimeout(() => {
hideTooltip(tooltip);
}, TIMEOUT_LENGTH);
// store the timeout so it can be cleared
timeouts.set(tooltip, timeoutId);
}
// initiate tooltips
const tooltips = document.querySelectorAll('.tooltip-wrapper');
tooltips.forEach((tooltip) => {
initTooltip(tooltip);
});
Also see: Tab Triggers