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. You can use the CSS from another Pen by using it's URL and the proper URL extention.
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 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.
<h1>CSS Styles with Reusable SVG Icons</h1>
<svg class="defs">
<symbol id="multiColoredIcon" viewBox="-2 -2 14 14">
<desc>A stop sign</desc>
<path fill="red" stroke-width="1"
d="M0,3 L3,0 7,0 10,3 10,7 7,10 3,10 0,7Z"/>
<text text-anchor="middle" line-height="1em"
x="5" y="7" textLength="8"
fill="currentColor" stroke="white" stroke-width="0.2" font-size="4" font-weight="bold">
STOP
</text>
</symbol>
</svg>
<p>Here's some text containing an embedded SVG icon <svg class="embed"><title>Stop it!</title><use xlink:href="#multiColoredIcon"/></svg>. Isn't that nifty? </p>
<p class="blue">And this is some colored text containing the same icon <svg class="embed"><use xlink:href="#multiColoredIcon"><title>I'm warning you!</title></use></svg>. Note that the SVG graphic properties that are set to use "currentColor" inherit the color. </p>
<p>Any styles set on the embedded <code><svg></code> or <code><use></code> element will also be inherited by the referenced graphics, replacing default values but <em>not</em> explicitly set values. <strong>Hover the above paragraphs</strong> to see the effect of setting the <code>stroke</code> property on the icons. <strong>Hover directly over an icon</strong> to see the effect of setting <code>fill-opacity</code>. <strong>Click on it</strong> to change the <code>color</code> property, which should change the <code>currentColor</code> fill of the text (but this doesn't seem to work in IE). </p>
<p>In contrast, if you set styles on the elements within the original icon definition, it will affect all uses of the icon. <strong>Hover the page title</strong> to see the effect of changing the graphics styles directly. Note that I'm setting the styles on the <code><path></code> and <code><text></code> elements directly (not on the parent <code><symbol></code> element) by using <code>symbol *</code> as the CSS selector, so that the styles will over-ride the graphical attributes defined in the SVG markup.</p>
<p><strong>Be warned that browsers are <a href="https://codepen.io/AmeliaBR/pen/lshrp/">very inconsistent and buggy</a> about style changes on elements that are duplicated with <code><use></code> elements!</strong> Any sort of a transition causes problems in webkit; specifying a transition on the original graphics prevents the icons from being redrawn even when the redraw is triggered by a style change on the icon SVG (i.e., without touching the original graphic's styles). In Firefox, setting hover effects directly on the source graphical elements, causes them to be triggered when hovering over the duplicate elements, but not in other browsers. Therefore, stick with applying hover effects on the referencing SVG or <code><use></code> element, and leave the source graphic alone.</p>
<p>You can successfully transition <em>some</em> style effects by applying the CSS transition to the SVG/use element. With a generic transition on the embedded SVG's styles, the change in opacity and in <code>color</code> / <code>currentColor</code> transition as expected in Webkit browsers (Chrome 33 / Opera 20) and Firefox (28), but not the change in stroke colour (it gets applied instantaneously). In IE (which doesn't support transitions for SVG), the transition on <code>color</code> prevents <code>currentColor</code> from updating.</p>
h1{
text-align:center;
font-variant:small-caps;
text-decoration:underline;
}
h1 code {
font-variant: normal;
font-size: larger;
}
p {
max-width:80%;
min-width:8em;
margin:0.5em auto;
font-size: large;
line-height:1.5em;
}
p:first-of-type, p:nth-of-type(2){
font-size:x-large;
}
p.blue {
color:royalblue;
}
p strong {
font-weight:bold;
color:darkslateblue;
}
p code {
background-color:lavender;
}
svg.defs {
height:0; width:0; margin:0; padding:0;
}
svg.defs text {
font-family:sans-serif;
}
svg.embed {
height:3em;
width:3em;
vertical-align:-50%;
}
p:hover svg.embed{
stroke: #fd0;
}
svg.embed:hover {
fill-opacity:0.5;
}
/* This doesn't have the intended effect in IE */
svg.embed:active {
color:orangered;
}
h1:hover ~ svg.defs symbol#multiColoredIcon * {
fill:seagreen;
stroke:darkgreen;
}
/* This doesn't transition the stroke property *
use, svg.embed {
transition: 1s;
}/* */
/* This causes all sorts of bugs: *
symbol * {
transition: 1s;
}/* */
Also see: Tab Triggers