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 id="content">
<div class="grid-div">
<div class="square1"><input placeholder="One"></div>
<div class="square2"><input placeholder="Two"></div>
<div class="square3"><input placeholder="Three"></div>
<div class="square4"><input placeholder="Four"></div>
</div>
</div>
<p>Normal html-to-image:<br>
<button onClick="renderPng()">Render PNG (broken)</button></p>
<p>Workaround by modifying the dom on the fly, doing image generation then reverting changes:<br>
<button onClick="renderPngFixed()">Render PNG (fixed)</button></p>
Demonstrates issues with and workarounds for:<ol>
<li><a href="https://github.com/bubkoo/html-to-image/issues/269" target="_blank">Input Placeholder rendering issue</a></li>
<li><a href="https://github.com/bubkoo/html-to-image/issues/258" target="_blank">Chrome bug with grid layouts</a></li>
</ol>
<p>Ideally the api would allow a callback (either per-node or after cloning the whole tree) to post-process cloned nodes and make CSS alterations to fix issues without needing to modify the source dom.</p>
#content {
width: 100px;
}
.grid-div {
display: grid;
grid-template-columns: 50px 50px;
grid-template-rows: 50px 50px;
grid-template-areas:
"one two"
"three four";
}
.square1 {
grid-area: one;
background: red;
}
.square2 {
grid-area: two;
background: green;
}
.square3 {
grid-area: three;
background: blue;
}
.square4 {
grid-area: four;
background: yellow;
}
input {
width: 40px;
}
input::placeholder {
color: purple;
}
async function renderPng() {
const elem = document.getElementById("content");
const dataUrl = await htmlToImage.toPng(elem);
const img = new Image();
img.src = dataUrl;
img.style.border = "dashed red 1px";
document.body.append(img);
}
async function renderPngFixed() {
const elem = document.getElementById("content");
const changes = [];
const undoChanges = [];
// Detect if the Chrome 99,100,101 grid bug is present
async function testHasGridBug() {
let isCorrect = true;
const tempEl = document.createElement("div");
tempEl.attachShadow({ mode: "open" });
tempEl.shadowRoot.innerHTML = `
<div style="display: grid; grid-template: 'a b' 1fr 'c d' 1fr / 1fr 1fr; width: 2px; height: 2px">
<div style="grid-area: a; background: #f00;"></div>
<div style="grid-area: c; background: #0f0;"></div>
<div style="grid-area: d; background: #00f;"></div>
<div style="grid-area: b; background: #000;"></div>
</div>
`;
try {
document.body.append(tempEl);
const pixels = await htmlToImage.toPixelData(tempEl.shadowRoot.firstElementChild);
const expecting = [255,0,0,255,0,0,0,255,0,255,0,255,0,0,255,255];
isCorrect = JSON.stringify(expecting) === JSON.stringify(Array.from(pixels));
} catch {
isCorrect = false;
} finally {
tempEl.remove();
}
return !isCorrect;
}
const hasGridBug = await testHasGridBug();
// Update changes and undoChanges arrays with callbacks to perform to make a
// particular css change. We postpone making any change until all changes are fully
// planned out.
function changeCSS(node, cssProp, value, priority = "") {
const startValue = node.style.getPropertyValue(cssProp);
const startPriority = node.style.getPropertyPriority(cssProp);
if (value === undefined) {
// Delete
changes.push(() => node.style.removeProperty(cssProp));
} else {
// Set
changes.push(() => node.style.setProperty(cssProp, value, priority));
undoChanges.push(() => node.style.removeProperty(cssProp));
}
if (startValue !== "") {
undoChanges.push(() =>
node.style.setProperty(cssProp, startValue, startPriority)
);
}
}
// Recursively modify CSS to workaround issues with rendering. Currently this
// addresses two issues (Chrome grid bug & problems with rendering placeholders)
function fixCSS(node, immediateGridChild = false) {
// If grid bug is present and node has css style: "display: grid"
// Change display to block and recurse on children, using fixed positioning for
// all immediate child elements. (Keep track of changes so we can undo after)
const needsGridFix =
hasGridBug &&
node.computedStyleMap &&
node.computedStyleMap().get("display").toString() === "grid";
if (needsGridFix) {
const rect = node.getBoundingClientRect();
changeCSS(node, "display", "block");
changeCSS(node, "position", "relative");
changeCSS(node, "height", `${rect.height}px`);
changeCSS(node, "width", `${rect.width}px`);
// Delete these
changeCSS(node, "grid", undefined);
changeCSS(node, "grid-template", undefined);
changeCSS(node, "grid-template-columns", undefined);
changeCSS(node, "grid-template-rows", undefined);
changeCSS(node, "grid-template-areas", undefined);
changeCSS(node, "grid-auto-rows", undefined);
changeCSS(node, "grid-auto-columns", undefined);
changeCSS(node, "grid-auto-flow", undefined);
}
// Use fixed positioning for immediate children of "display: grid" element
if (immediateGridChild) {
const parentRect = node.parentElement.getBoundingClientRect();
const rect = node.getBoundingClientRect();
changeCSS(node, "top", `${rect.top - parentRect.top}px`);
changeCSS(node, "left", `${rect.left - parentRect.left}px`);
changeCSS(node, "height", `${rect.height}px`);
changeCSS(node, "width", `${rect.width}px`);
changeCSS(node, "position", "absolute");
// Delete these
changeCSS(node, "grid-area", undefined);
changeCSS(node, "grid-column", undefined);
changeCSS(node, "grid-row", undefined);
changeCSS(node, "grid-row-start", undefined);
changeCSS(node, "grid-row-end", undefined);
changeCSS(node, "grid-column-start", undefined);
changeCSS(node, "grid-column-end", undefined);
}
// Fix issue with placeholder text not getting styled
// I'm not sure how to pull the current pseudo element color so this is hardcoded
if (["INPUT", "TEXTAREA"].includes(node.tagName) && node.value === "") {
changeCSS(node, "color", "purple");
}
// Recurse on children
Array.from(node.children).forEach((child) => fixCSS(child, needsGridFix));
if (node.shadowRoot) fixCSS(node.shadowRoot);
}
try {
fixCSS(elem);
changes.forEach((f) => f());
await renderPng();
} finally {
undoChanges.forEach((f) => f());
}
}
Also see: Tab Triggers