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.
<div x-data="ringGenerator" x-init="generateRings" class="bg-medium min-h-screen grid lg:grid-cols-[auto_1fr] overflow-hidden">
<aside class="flex flex-col bg-dark text-neutral-400">
<h1 class="py-8 px-4 bg-teal-500 text-dark font-bold text-xl mb-4 uppercase flex items-center before:h-0.5 before:flex-1 before:bg-current before:mr-2 after:h-0.5 after:flex-1 after:bg-current after:ml-2">Shape settings</h1>
<!-- Settings -->
<ul class="flex flex-col items-start flex-1 p-4 space-y-8">
<!-- Shape style -->
<li class="flex items-center justify-between w-full space-x-8">
<label for="shape">Shape:</label>
<select id="shape" x-model="shape" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
<option value="circle">Circle</option>
<option value="rectangle">Rectangle</option>
</select>
</li>
<!-- Border Radius -->
<li x-show="shape === 'rectangle'" class="flex items-center justify-between w-full space-x-8">
<label for="shapeRadius">Border Radius:</label>
<input type="number" id="shapeRadius" min="0" x-model.number="shapeRadius" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
</li>
<!-- Shape position on X axis -->
<li class="flex items-center justify-between w-full space-x-8">
<label for="posX">Position X:</label>
<select id="posX" x-model="posX" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
<option value="left">Left</option>
<option value="center">Center</option>
<option value="right">Right</option>
</select>
</li>
<!-- Shape position on Y axis -->
<li class="flex items-center justify-between w-full space-x-8">
<label for="posY">Position Y:</label>
<select id="posY" x-model="posY" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
<option value="top">Top</option>
<option value="center">Center</option>
<option value="bottom">Bottom</option>
</select>
</li>
<!-- Shape color -->
<li class="flex items-center justify-between w-full space-x-8">
<label for="shapeColor">Shape Color:</label>
<input type="color" id="shapeColor" x-model="shapeColor" @change="generateRings" class="h-10 rounded bg-light flex-1 max-w-40">
</li>
<!-- Shape Ring Count -->
<li class="flex items-center justify-between w-full space-x-8">
<label for="shapeCount">Shape Count:</label>
<input type="number" id="shapeCount" min="2" x-model.number="shapeCount" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
</li>
<!-- Shape Ring Width -->
<li class="flex items-center justify-between w-full space-x-8">
<label for="shapeWidth">Shape Width:</label>
<input type="number" id="shapeWidth" min="1" x-model.number="shapeWidth" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
</li>
<!-- Shape Spacing between Rings -->
<li class="flex items-center justify-between w-full space-x-8">
<label for="shapeSpacing">Shape Spacing:</label>
<input type="number" id="shapeSpacing" min="0" x-model.number="shapeSpacing" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
</li>
<!-- Shape Opacity -->
<li class="flex items-center justify-between w-full space-x-8">
<label for="shapeOpacity">Shape Opacity:</label>
<input type="checkbox" id="shapeOpacity" x-model.boolean="shapeOpacity" @change="generateRings" class="p-1 rounded bg-zinc-400 text-zinc-100 scale-150">
</li>
<!-- Shape Opacity Range -->
<li x-show="shapeOpacity" class="flex items-center justify-between w-full space-x-4" x-cloak>
<label for="opacityRange">Opacity Start:</label>
<input type="range" min="0" max="0.9" step="0.1" id="opacityRange" x-model.number="opacityRange" @change="generateRings" class="p-1 rounded bg-zinc-400 text-zinc-100 flex-1 max-w-40">
<span x-text="opacityRange"></span>
</li>
<!-- Shape Dynamic Size -->
<!-- <li class="flex items-center justify-between w-full space-x-8">
<label for="dynamicSize">Dynamic Size:</label>
<input type="checkbox" id="dynamicSize" x-model.boolean="dynamicSize" @change="generateRings" class="p-1 rounded bg-zinc-400 text-zinc-100 scale-150">
</li> -->
</ul>
<!-- Buttons -->
<div class="grid gap-2 p-4 mt-8 text-center font-semibold">
<!-- Open Modal and show SVG Code -->
<button type="button" @click="showModal = true" class="border-2 border-zinc-500 text-zinc-500 rounded px-4 py-2 hover:text-dark hover:bg-zinc-500">Show SVG Code</button>
<!-- Download SVG Image File -->
<a :href="downloadImageURL" download="lw_generated_shape.svg" class="border-2 border-zinc-500 text-zinc-500 rounded px-4 py-2 hover:text-dark hover:bg-zinc-500">Download as image</a>
<!-- Copy To Clipboard -->
<button type="button" @click="copyToClipboard" class="text-dark bg-teal-500 rounded px-4 py-2 hover:bg-teal-400">
<span>Copy to Clipboard</span>
</button>
</div>
</aside>
<!-- Shape Container -->
<div class="flex-1 flex justify-center items-center p-8 size-full relative overflow-auto">
<svg xmlns="http://www.w3.org/2000/svg" id="svgContainer" fill="none" aria-hidden="true"></svg>
</div>
<!-- Modal -->
<div
x-show="showModal"
@keydown.escape.prevent.stop="showModal = false"
role="dialog"
aria-modal="true"
x-id="['modal-title']"
:aria-labelledby="$id('modal-title')"
class="fixed inset-0 z-10 overflow-y-auto"
x-cloak
>
<!-- Overlay -->
<div x-show="showModal" x-transition.opacity class="fixed inset-0 bg-black bg-opacity-50 backdrop-blur"></div>
<!-- Panel -->
<div
x-show="showModal" x-transition
@click="showModal = false"
class="relative flex min-h-screen items-center justify-center p-4"
>
<div
@click.stop
x-trap.noscroll.inert="showModal"
class="relative w-full max-w-2xl overflow-y-auto rounded-xl bg-dark border border-medium text-neutral-400 p-8 shadow-lg space-y-4"
>
<!-- Title -->
<h2 class="text-3xl font-bold" :id="$id('modal-title')">Output HTML:</h2>
<!-- Content -->
<label for="outputCode"></label>
<textarea id="outputCode" rows="10" cols="30" x-model="outputHTML" readonly class="p-2 w-full rounded border-2 border-zinc-500 bg-medium"></textarea>
<!-- Buttons -->
<div class="flex space-x-2 font-semibold">
<button type="button" @click="copyToClipboard" class="text-dark bg-teal-500 rounded px-4 py-2 hover:bg-teal-400">
Copy to Clipboard
</button>
<button type="button" @click="showModal = false" class="border-2 border-zinc-500 text-zinc-500 rounded px-4 py-2 hover:text-dark hover:bg-zinc-500">
Cancel
</button>
</div>
</div>
</div>
</div>
<!-- Toast -->
<div x-show="showToast" class="fixed right-4 bottom-4 py-2 pr-4 text-sm text-green-500 rounded shadow-lg bg-dark border border-zinc-800 z-10 flex" role="status" x-cloak x-transition>
<p class="before:h-6 before:w-1.5 before:rounded before:bg-green-500 before:mx-2 inline-flex items-center leading-none">
Copied to clipboard
</p>
</div>
</div>
[x-cloak] { display: none !important; }
import Alpine from "https://esm.sh/alpinejs";
Alpine.data('ringGenerator', () => ({
svg: document.getElementById('svgContainer'),
shape: 'circle',
shapeColor: '#14b8a6',
shapeCount: 20,
shapeWidth: 10,
shapeSpacing: 30,
posX: 'center',
posY: 'top',
rotation: 0,
shapeOpacity: false,
opacityRange: 0,
dynamicSize: false,
shapeRadius: 0,
showModal: false,
outputHTML: '',
downloadImageURL: '',
showToast: false,
generateRings() {
this.svg.innerHTML = '';
const totalSize = this.shapeCount * this.shapeWidth + (this.shapeCount - 1) * this.shapeSpacing;
this.svg.setAttribute('height', this.posY === 'center' ? totalSize : totalSize / 2);
this.svg.setAttribute('width', this.posX === 'center' ? totalSize : totalSize / 2);
this.svg.setAttribute('stroke', this.shapeColor)
this.svg.setAttribute('viewBox', `0 0 ${this.posX === 'center' ? totalSize : totalSize / 2} ${this.posY === 'center' ? totalSize : totalSize / 2}`)
this.dynamicSize ? this.svg.setAttribute('preserveAspectRatio', 'none') : this.svg.removeAttribute('preserveAspectRatio')
for (let i = 0; i < +this.shapeCount; i++) {
const cx = this.posX === 'left' ? 0 : this.posX === 'center' ? totalSize / 2 : totalSize / 2;
const cy = this.posY === 'top' ? 0 : this.posY === 'center' ? totalSize / 2 : totalSize / 2;
const halfSize = ((this.shapeWidth + this.shapeSpacing) * i) / 2;
const x = cx - halfSize;
const y = cy - halfSize;
const size = (this.shapeWidth + this.shapeSpacing) * i;
const radius = this.shapeRadius > 0 && i === this.shapeCount - 1 ? this.shapeRadius : (this.shapeRadius / this.shapeCount) * i
// calculating opacity
const startOpacityIdx = Math.ceil(this.opacityRange * this.shapeCount)
const opacity = (this.shapeOpacity && i >= startOpacityIdx)
? 1 - (i - startOpacityIdx) / (this.shapeCount - startOpacityIdx)
: 1
const shapeElement = `
<rect x="${x}"
y="${y}"
width="${size}"
height="${size}"
stroke-width="${this.shapeWidth / 2}"
rx="${this.shape === 'circle' ? '100%' : radius.toFixed(2)}" opacity="${opacity.toFixed(2)}"
/>`;
this.svg.innerHTML += shapeElement;
}
// Update the output HTML
this.outputHTML = this.svg.outerHTML;
// Generate new download url
this.downloadSVG()
},
copyToClipboard() {
const outputCode = document.getElementById('outputCode');
outputCode.select();
document.execCommand('copy');
this.toast()
},
toast() {
this.showToast = true
setTimeout(() => {this.showToast = false}, 1000)
},
downloadSVG() {
const svgData = new XMLSerializer().serializeToString(this.svg);
const blob = new Blob([svgData], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
this.downloadImageURL = url
}
}));
Alpine.start();
Also see: Tab Triggers