Attachment preview media object in HTML, CSS and vanilla JS
A copy-paste Attachment preview media object in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="dcl-media-object-attachment" data-dcl-scheme="light">
<article class="dcl-media-object-attachment-attachment"><figure data-panel aria-hidden="true"><span>FIELD GUIDE</span><hr><hr><hr></figure>
<div data-stack><header><h2>Workshop handout</h2><p>Plain text · 2 pages</p></header>
<details><summary>Read excerpt</summary><p>Bring a pencil and three sheets of A4 paper. Blocks, ink and cleaning materials will be provided.</p></details></div></article>
<script>
(() => {
const root = document.currentScript.parentElement;
const instance = [...document.getElementsByClassName("dcl-media-object-attachment")].indexOf(root);
if (instance > 0) {
for (const node of root.querySelectorAll("[id], [for], [aria-labelledby], [aria-controls], [aria-describedby], a[href], input[name]")) {
for (const key of ["id", "for", "aria-labelledby", "aria-controls", "aria-describedby", "name"]) {
if (node.hasAttribute(key)) node.setAttribute(key, node.getAttribute(key).split(" ").map(value => value + "--" + instance).join(" "));
}
const href = node.getAttribute("href");
if (href && href.startsWith("#")) node.setAttribute("href", href + "--" + instance);
}
}
const scheme = matchMedia("(prefers-color-scheme: dark)");
const applyScheme = () => { root.dataset.dclScheme = scheme.matches ? "dark" : "light"; };
scheme.addEventListener("change", applyScheme);
applyScheme();
const disclosure = root.querySelector("details");
const summary = disclosure.querySelector("summary");
disclosure.addEventListener("keydown", event => {
if (event.key === "Escape" && disclosure.open) {
disclosure.open = false;
summary.focus();
}
});
})();
</script>
</section>
<style>
.dcl-media-object-attachment { box-sizing: border-box; max-inline-size: 42rem; padding: clamp(.75rem, 4vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
.dcl-media-object-attachment *, .dcl-media-object-attachment *::before, .dcl-media-object-attachment *::after { box-sizing: border-box; }
.dcl-media-object-attachment h2, .dcl-media-object-attachment h3, .dcl-media-object-attachment p, .dcl-media-object-attachment ul, .dcl-media-object-attachment dl, .dcl-media-object-attachment figure { margin: 0; }
.dcl-media-object-attachment h2 { font-size: 1.25rem; line-height: 1.25; }
.dcl-media-object-attachment h3 { font-size: 1rem; }
.dcl-media-object-attachment > * + * { margin-block-start: .75rem; }
.dcl-media-object-attachment button, .dcl-media-object-attachment summary, .dcl-media-object-attachment input, .dcl-media-object-attachment select, .dcl-media-object-attachment textarea { font: inherit; }
.dcl-media-object-attachment button, .dcl-media-object-attachment summary { padding: .45rem .7rem; min-block-size: 2.75rem; border: 1px solid; border-radius: .375rem; cursor: pointer; text-align: start; }
.dcl-media-object-attachment select, .dcl-media-object-attachment input[type="text"], .dcl-media-object-attachment textarea { max-inline-size: 100%; padding: .45rem; border: 1px solid; border-radius: .375rem; }
.dcl-media-object-attachment button:active { transform: translateY(1px); }
.dcl-media-object-attachment button[disabled], .dcl-media-object-attachment button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; transform: none; }
.dcl-media-object-attachment button[aria-pressed="true"] { border-style: double; border-width: 3px; }
.dcl-media-object-attachment details > summary { inline-size: fit-content; }
.dcl-media-object-attachment [data-panel], .dcl-media-object-attachment dialog { border: 1px solid; border-radius: .625rem; padding: .8rem; }
.dcl-media-object-attachment dialog { max-inline-size: min(calc(100% - 2rem), 28rem); max-block-size: calc(100% - 2rem); }
.dcl-media-object-attachment dialog > * + * { margin-block-start: .75rem; }
.dcl-media-object-attachment [data-row] { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; min-inline-size: 0; }
.dcl-media-object-attachment [data-stack] { display: grid; gap: .6rem; min-inline-size: 0; }
.dcl-media-object-attachment [data-list] { list-style: none; padding: 0; display: grid; gap: .55rem; }
.dcl-media-object-attachment [data-wide] { flex: 1 1 11rem; min-inline-size: 0; }
.dcl-media-object-attachment .dcl-media-object-attachment-status { min-block-size: 1.5em; font-weight: 600; }
.dcl-media-object-attachment .dcl-media-object-attachment-glyph { display: inline-grid; place-items: center; font-size: 1.2em; line-height: 1; }
.dcl-media-object-attachment [hidden] { display: none; }
.dcl-media-object-attachment { color: #11292f; background-color: #ffffff; color-scheme: light; border-color: #697f84; }
.dcl-media-object-attachment a { color: #005c62; }
.dcl-media-object-attachment button, .dcl-media-object-attachment summary, .dcl-media-object-attachment input, .dcl-media-object-attachment select, .dcl-media-object-attachment textarea { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-media-object-attachment .dcl-media-object-attachment-primary, .dcl-media-object-attachment button[aria-pressed="true"] { color: #ffffff; background-color: #00535a; }
.dcl-media-object-attachment button:hover, .dcl-media-object-attachment summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-media-object-attachment button[disabled], .dcl-media-object-attachment button[aria-disabled="true"] { color: #405a60; background-color: #ffffff; }
.dcl-media-object-attachment [data-panel], .dcl-media-object-attachment dialog { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-media-object-attachment :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-media-object-attachment[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; border-color: #839fa5; }
.dcl-media-object-attachment[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-media-object-attachment[data-dcl-scheme="dark"] button, .dcl-media-object-attachment[data-dcl-scheme="dark"] summary, .dcl-media-object-attachment[data-dcl-scheme="dark"] input, .dcl-media-object-attachment[data-dcl-scheme="dark"] select, .dcl-media-object-attachment[data-dcl-scheme="dark"] textarea { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-media-object-attachment[data-dcl-scheme="dark"] .dcl-media-object-attachment-primary, .dcl-media-object-attachment[data-dcl-scheme="dark"] button[aria-pressed="true"] { color: #11292f; background-color: #8fe1da; }
.dcl-media-object-attachment[data-dcl-scheme="dark"] button:hover, .dcl-media-object-attachment[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-media-object-attachment[data-dcl-scheme="dark"] button[disabled], .dcl-media-object-attachment[data-dcl-scheme="dark"] button[aria-disabled="true"] { color: #b0c9ce; background-color: #142f36; }
.dcl-media-object-attachment[data-dcl-scheme="dark"] [data-panel], .dcl-media-object-attachment[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-media-object-attachment[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-media-object-attachment .dcl-media-object-attachment-attachment { display: grid; gap: .75rem; } .dcl-media-object-attachment figure { min-block-size: 8rem; border-radius: .45rem .45rem .45rem .15rem; text-align: center; } .dcl-media-object-attachment figure span { display: block; font-size: .75rem; font-weight: 700; letter-spacing: .05em; } .dcl-media-object-attachment details p { margin-block-start: .45rem; } @media (min-width: 29rem) { .dcl-media-object-attachment .dcl-media-object-attachment-attachment { grid-template-columns: minmax(7rem, 1fr) minmax(0, 2fr); } }
</style>
When to use
Show enough of an attached document to decide whether to inspect it.
Avoid
Do not use the small preview for documents that must be read in detail.
Accessibility
Use a native disclosure, keep the filename visible while collapsed, and make the preview decorative when redundant. Light/dark body text: 15.19:1 and 13.07:1, respectively.
Was this useful? Send feedback.