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.
<svg style="display:none;">
<defs>
<symbol id="icon-heart" viewBox="0 0 32 32" fill="currentColor">
<path d="M0 10 C0 6, 3 2, 8 2 C12 2, 15 5, 16 6 C17 5, 20 2, 24 2 C30 2, 32 6, 32 10 C32 18, 18 29, 16 30 C14 29, 0 18, 0 10 "></path>
</symbol>
<symbol id="icon-close" viewBox="0 0 32 32" fill="currentColor">
<path d="M4 8 L8 4 L16 12 L24 4 L28 8 L20 16 L28 24 L24 28 L16 20 L8 28 L4 24 L12 16 z "></path>
</symbol>
<symbol id="icon-hamburger" viewBox="0 0 32 32" fill="currentColor">
<path d="M3 8 A3 3 0 0 0 9 8 A3 3 0 0 0 3 8 M12 6 L28 6 L28 10 L12 10z M3 16 A3 3 0 0 0 9 16 A3 3 0 0 0 3 16 M12 14 L28 14 L28 18 L12 18z M3 24 A3 3 0 0 0 9 24 A3 3 0 0 0 3 24 M12 22 L28 22 L28 26 L12 26z "></path>
</symbol>
</defs>
</svg>
<h1>Progressive SVGs</h1>
<div class="buttons" data-codeblock>
<button aria-label="Like">
<span class="inline-svg" data-xlink="#icon-heart">♥</span>
</button>
<button aria-label="Close">
<span class="inline-svg" data-xlink="#icon-close">Close</span>
</button>
<button aria-label="Menu">
<span class="inline-svg" data-xlink="#icon-hamburger" >≡</span>
</button>
<p><a href="#">Do you <span class="inline-svg" data-xlink="#icon-heart" title="like">♥</span> me?</a></p>
<button aria-label="Like">
<span class="inline-svg" data-xlink="#icon-heart">♥</span> Like
</button>
</div>
## Working theory:
Most inline SVGs don't need fallbacks, they're icon candy paired with assistive text and/or they are decorative (like icon-fonts). This aims at **progressively enhancing critical fallbacks** when absolutely necessary. So, we take a span, with a little extra meta and replace it with an SVG `<use>` element when it appears enhanceable.
How it looks in IE8
![](https://s3-us-west-2.amazonaws.com/s.cdpn.io/17/Screenshot_2014-12-17_17.19.56.png)
## Approach
I try to scope the Image Replacement (IR) to either text or unicode. There are 5 examples here:
1. A Like `button` with a Unicode "BLACK HEART SUIT" (♥) fallback. This heart is generally supported. Added an `aria-label` on the `button` to avoid screenreaders stumbling on wonky Unicode. VoiceOver failed on reading SVG `title` attributes in a `button`.
1. A Close button with "Close" as a text fallback. The Unicode "MULTIPLICATION SIGN" (×) character has decent support, but I wanted an example where you might fall back to text. Same ARIA approach as above.
1. A Hamburger button with fallback Unicode "IDENTICAL TO" (≡) character. Same ARIA approach, but very necessary due to the desired label being "Menu" which is very different than the character's meaning.
1. A link with a Unicode heart (♥) fallback. Adding a `title` attribute here actually improved the accessibility for both SVG and fallback span.
1. A Like button with "Like" text. This doesn't really even require a fallback.
## Rules
- Use similar Unicode when you can get away with it.
- See [Unify](https://unicode.johnholtripley.co.uk/all/support) by John Ripley to guage how well a character is supported across devices and screenreaders.
- There's a bit of CSS you'll need to make sure old Windows uses 'Arial Unicode MS' as a fallback font with Unicode support.
- A lot of Zach Leatherman's [Bulletproof Icon Fonts](https://www.filamentgroup.com/lab/bulletproof_icon_fonts.html) applies here.
- Must be Accessible. Avoid wonky Unicode reading:
- Add `aria-label` to parent `button` elements.
- Use `title` attributes otherwise.
- If you used an external SVG (e.g., `some.svg#icon-hamburger`), you might be able to save legacy browsers the double download.
<small>SVGs from <a href="http://geomicons.com/">Geomicons</a></small>
// General Style
html, body {
background: #f0f0f0;
}
h1 {
text-align: center;
}
body {
font-family: sans-serif;
padding: 5%;
line-height: 1.6em;
}
img {
max-width: 100%;
height: auto;
}
.buttons {
text-align: center;
}
pre {
background: #fff;
padding: 1em;
max-height: 20em;
overflow: auto;
margin-bottom: 5%;
}
button {
font-size: 1em;
line-height: 1em;
}
// Inline SVG Code
// ====================================================
.inline-svg {
display: inline-block;
}
span.inline-svg {
// Unicode chars need a Unicode font on old browsers.
font-family: 'Arial Unicode MS', Arial, sans-serif;
}
svg.inline-svg {
// Tailor to suite your needs.
width: 1em;
height: 1em;
vertical-align: text-top;
}
(function(window, document){
// Feature Test
// TODO: Could be improved to detect INLINE only.
var supportsSvg = function(){
return document.implementation && document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1");
};
// Enhance!
if(supportsSvg) {
var inlineSvgs = document.querySelectorAll('span.inline-svg');
for(i=0;i<inlineSvgs.length;i++) {
var span = inlineSvgs[i];
var svgns = "http://www.w3.org/2000/svg";
var xlinkns = "http://www.w3.org/1999/xlink";
var svg = document.createElementNS(svgns, "svg");
var use = document.createElementNS(svgns, "use");
// Prepare the <use> element
use.setAttributeNS(xlinkns, 'xlink:href', span.getAttribute('data-xlink') );
// Append it
svg.appendChild(use);
// Prepare the SVG
svg.setAttribute('class', "inline-svg");
// Set a title if necessary.
if(span.getAttribute('title')) {
svg.setAttribute('title', span.getAttribute('title'));
}
// Inject the SVG
span.parentNode.insertBefore(svg, span);
// 6 Minute Abs™, for your DOM.
span.remove();
}
}
})(window, document, undefined);
Also see: Tab Triggers