data-mock prototype
Placeholder media via data attributes. Add data-mock to img, video, iframe, or canvas for instant placeholder boxes with X-pattern, presets, and photo service integration.
Overview
The data-mock attribute turns native media elements into placeholder boxes. CSS renders the visual (gray box, X-pattern) and JS generates a valid src to keep the element well-formed. Moving to production means replacing src and removing data-mock — no HTML restructuring.
How It Works
- CSS applies a gray background, dashed border, and diagonal X-pattern via gradients
- JS generates an inline SVG data URI as
src(for<img>),poster(for<video>),srcdoc(for<iframe>), or draws on<canvas> - The label text comes from the
altattribute (images),title(iframes), oraria-label(canvas) - Named presets set aspect ratio and default dimensions via CSS — no
width/heightattributes needed
Attributes
| Attribute | Type | Description |
|---|---|---|
data-mock |
string | Preset name (hero, card, avatar, etc.) or empty for generic placeholder. Special values: photo (picsum), placehold (placehold.co). |
data-seed |
string | Reproducible seed for the photo preset (picsum.photos). Same seed always returns the same image. |
data-mock-init |
boolean | Set automatically to prevent double-binding. Do not set manually. |
Presets
Named presets set aspect-ratio and a default inline-size via CSS. Explicit width/height attributes override the JS dimensions used for SVG generation.
| Preset | Aspect Ratio | Default Width | Use Case |
|---|---|---|---|
hero | 3:1 | 100% | Hero banners |
card | 16:9 | 400px | Blog/article cards |
avatar | 1:1 | 48px | User avatars (rounded) |
product | 1:1 | 400px | Product images |
thumbnail | 1:1 | 150px | Grid thumbnails |
logo | 4:1 | 200px | Brand logos |
og | 1200:630 | 100% | Open Graph images |
banner | 728:90 | 100% | Ad banners |
Photo Service
Use data-mock="photo" for realistic placeholder photos from picsum.photos. The image dimensions come from width and height attributes.
Add data-seed for reproducible results — the same seed always returns the same photo.
Placehold.co
Use data-mock="placehold" for labeled placeholder images from placehold.co. The alt text is used as the label.
Video, Iframe, and Canvas
data-mock works on all media elements.
Video
Gets a poster attribute with an SVG placeholder showing a play icon and label. Defaults to 16:9 aspect ratio.
Iframe
Gets a srcdoc with a minimal HTML page showing a centered label. Defaults to 16:9 with min-height 300px.
Canvas
Draws the X-pattern and label directly on the canvas context. Defaults to 4:3 aspect ratio.
Inside Figure
Works inside <figure> and <picture> with no structural changes needed.
Production Transition
Moving to production requires no HTML restructuring. Replace src and remove data-mock.
Styling
The placeholder colors use design tokens (--color-gray-200, --color-gray-400). Override them with element-scoped CSS.
Wireframe Integration
When inside a [data-wireframe] container, mock media automatically adapts to wireframe tokens (--wireframe-fill, --wireframe-stroke, --wireframe-border). No extra configuration needed.
Dynamic Elements
Elements added to the DOM after page load are automatically enhanced via a MutationObserver.
Accessibility
- Always include
alttext on images — it doubles as the placeholder label - Use
titleon iframes andaria-labelon canvas for labeling - The generated SVG includes
role="img"andaria-label - Placeholder images get
loading="lazy"for photo/placehold variants