Image file preview in HTML, CSS and vanilla JS
A copy-paste Image file preview in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-file-image-preview" data-dcl-scheme="light" aria-labelledby="example-file-image-preview-title">
<header><h2 id="example-file-image-preview-title">Image file preview</h2>
<p>Choosing an image shows a local preview before the form is submitted.</p></header>
<div class="parts">
<div class="field">
<label for="example-file-image-preview-file">Choose artwork image</label>
<input type="file" id="example-file-image-preview-file" accept="image/*" data-input aria-describedby="example-file-image-preview-hint">
<p class="hint" id="example-file-image-preview-hint">The preview is created locally with an object URL; no file leaves the page.</p>
</div>
<div class="preview panel" data-preview data-note hidden></div>
<p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
const root = document.currentScript.closest(".example-file-image-preview");
const instance = Array.from(document.getElementsByClassName("example-file-image-preview")).indexOf(root);
if (instance > 0) {
const singleReferences = ["id", "aria-activedescendant", "for", "list", "name"];
const tokenReferences = ["aria-labelledby", "aria-controls", "aria-describedby"];
for (const element of [root, ...root.querySelectorAll("*")]) {
for (const attr of singleReferences) {
if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
}
for (const attr of tokenReferences) {
if (element.hasAttribute(attr)) {
const rewritten = element.getAttribute(attr).trim().split(/\s+/).filter(Boolean).map((reference) => reference + "--" + instance);
element.setAttribute(attr, rewritten.join(" "));
}
}
const href = element.getAttribute("href");
if (href && href.startsWith("#")) element.setAttribute("href", href + "--" + instance);
}
}
const preference = matchMedia("(prefers-color-scheme: dark)");
const apply = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
preference.addEventListener("change", apply);
apply();
const input = root.querySelector("[data-input]");
const preview = root.querySelector("[data-preview]");
const status = root.querySelector("[data-status]");
let objectUrl = null;
input.addEventListener("change", () => {
const file = input.files && input.files[0];
preview.replaceChildren();
if (objectUrl) URL.revokeObjectURL(objectUrl);
objectUrl = null;
if (!file) {
preview.hidden = true;
status.textContent = "No image selected.";
return;
}
if (!file.type.startsWith("image/")) {
preview.hidden = true;
status.textContent = file.name + " is not recognised as an image.";
return;
}
objectUrl = URL.createObjectURL(file);
const image = document.createElement("img");
const caption = document.createElement("p");
image.src = objectUrl;
image.alt = "Preview of " + file.name;
caption.textContent = file.name;
preview.append(image, caption);
preview.hidden = false;
status.textContent = "Preview ready for " + file.name + ".";
});
})();
</script></section>
<style>
.example-file-image-preview { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-file-image-preview a { color: #005c62; }
.example-file-image-preview button, .example-file-image-preview summary, .example-file-image-preview input, .example-file-image-preview select, .example-file-image-preview textarea {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-file-image-preview button:hover, .example-file-image-preview summary:hover, .example-file-image-preview button[aria-selected="true"], .example-file-image-preview button[aria-pressed="true"] {
color: #ffffff; background-color: #00535a;
}
.example-file-image-preview dialog { color: #11292f; background-color: #ffffff; }
.example-file-image-preview [data-note] { color: #11292f; background-color: #e4edef; }
.example-file-image-preview [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-file-image-preview [data-success] { color: #14532d; background-color: #dcfce7; }
.example-file-image-preview [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-file-image-preview :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-file-image-preview[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-file-image-preview[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-file-image-preview[data-dcl-scheme="dark"] button, .example-file-image-preview[data-dcl-scheme="dark"] summary,
.example-file-image-preview[data-dcl-scheme="dark"] input, .example-file-image-preview[data-dcl-scheme="dark"] select, .example-file-image-preview[data-dcl-scheme="dark"] textarea {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-file-image-preview[data-dcl-scheme="dark"] button:hover, .example-file-image-preview[data-dcl-scheme="dark"] summary:hover,
.example-file-image-preview[data-dcl-scheme="dark"] button[aria-selected="true"], .example-file-image-preview[data-dcl-scheme="dark"] button[aria-pressed="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-file-image-preview[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-file-image-preview[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-file-image-preview[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-file-image-preview[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-file-image-preview[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-file-image-preview[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-file-image-preview, .example-file-image-preview * { box-sizing: border-box; min-width: 0; }
.example-file-image-preview { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-file-image-preview h2, .example-file-image-preview h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-file-image-preview header p, .example-file-image-preview p { margin-block: 0.5rem; }
.example-file-image-preview ul, .example-file-image-preview ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-file-image-preview dl { margin-block: 0.5rem; }
.example-file-image-preview dt { font-weight: 700; }
.example-file-image-preview dd { margin: 0 0 0.6rem; }
.example-file-image-preview fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-file-image-preview legend { padding-inline: 0.25rem; font-weight: 700; }
.example-file-image-preview button, .example-file-image-preview input, .example-file-image-preview select, .example-file-image-preview textarea {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-file-image-preview input, .example-file-image-preview select, .example-file-image-preview textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-file-image-preview input[type="checkbox"], .example-file-image-preview input[type="radio"] {
display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-file-image-preview input[type="file"] { padding: 0.45rem; }
.example-file-image-preview input[type="range"] { padding-inline: 0; }
.example-file-image-preview textarea { min-block-size: 6rem; resize: vertical; }
.example-file-image-preview button, .example-file-image-preview summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-file-image-preview button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-file-image-preview table { border-collapse: collapse; inline-size: 100%; }
.example-file-image-preview th, .example-file-image-preview td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-file-image-preview [hidden] { display: none !important; }
.example-file-image-preview [data-note], .example-file-image-preview [data-error], .example-file-image-preview [data-success], .example-file-image-preview [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-file-image-preview .parts { display: grid; gap: 1rem; }
.example-file-image-preview .field, .example-file-image-preview .fields { display: grid; gap: 0.4rem; }
.example-file-image-preview .fields { gap: 0.75rem; }
.example-file-image-preview .actions, .example-file-image-preview .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-file-image-preview .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-file-image-preview .plain-list { margin: 0; padding: 0; list-style: none; }
.example-file-image-preview .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-file-image-preview .choice input { margin-block-start: 0.25rem; }
.example-file-image-preview .choice-stack { display: grid; gap: 0.55rem; }
.example-file-image-preview .tile { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0.55rem; align-items: start; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-file-image-preview .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-file-image-preview .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-file-image-preview .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-file-image-preview .hint { font-size: 0.875rem; }
.example-file-image-preview .status { min-block-size: 1.5rem; margin-block: 0; }
.example-file-image-preview .two-column, .example-file-image-preview .split-layout { display: grid; gap: 1rem; }
.example-file-image-preview .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-file-image-preview .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-file-image-preview .visually-hidden {
position: absolute; inline-size: 1px; block-size: 1px; padding: 0; margin: -1px;
overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
@media (min-width: 34rem) {
.example-file-image-preview .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.example-file-image-preview .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
.example-file-image-preview { padding: 0.75rem; }
.example-file-image-preview .actions, .example-file-image-preview .inline-row { align-items: stretch; }
.example-file-image-preview .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-file-image-preview * { transition: none !important; } }
.example-file-image-preview .preview[hidden] { display: none !important; }
.example-file-image-preview .preview { display: grid; gap: 0.5rem; }
.example-file-image-preview .preview img { display: block; max-inline-size: 100%; block-size: auto; border-radius: 0.375rem; }
</style>
When to use
Use this for avatars, receipts, or artwork where visual confirmation prevents mistakes. Revoke the object URL when replacing it and keep the file input visible for keyboard use.
Avoid
Do not treat a successful preview as validation of dimensions, content safety, or upload success.
Accessibility
The picker is a visible native labelled input. The generated image alternative names its source file, non-image selection is announced in text, and the preview region is hidden until needed. Main text contrast is 15.19:1 light and 13.07:1 dark; controls are 12.78:1 and 10.94:1.
Was this useful? Send feedback.