Shared Element Transition
A thumbnail grows into a detail view, so the user sees it is the same object.
AI prompt
Create a 300x200px container with a 72x72px thumbnail button (neutral gradient, rounded 12px) at the top left. Clicking it opens a detail state in which the same element becomes a full-container panel (inset 8px) with a title, a paragraph and a Close button. Implement it with FLIP: measure the element's rectangle before changing its layout class, measure again after, then animate with element.animate from transform translate(dx,dy) scale(sx,sy) with transform-origin top left to none over 380ms cubic-bezier(.2,.7,.2,1). Fade the detail text in over 200ms after a 180ms delay. Closing reverses the animation. Move focus to the Close button on open and back to the thumbnail on close, handle Escape, and set aria-expanded on the thumbnail. Under prefers-reduced-motion skip the transform animation and use a 100ms fade.About this pattern
Definition
An element that exists in two layouts moves and resizes between them, instead of one view disappearing and another appearing. The FLIP technique (First, Last, Invert, Play) measures both layouts and animates the difference.
Purpose & UX principles
- Keeps people oriented: they see where the detail came from and where it returns to.
- Animating transform is cheap, so it stays smooth even on modest devices.
- Detail content fades in after the shape settles, so text is never stretched.
Interaction states
- Thumbnail, expanding, open, collapsing; focus moves to the close button when open and back to the thumbnail after.
Motion specification
- Shape: translate and scale from the thumbnail rectangle to the full rectangle, 380ms cubic-bezier(.2,.7,.2,1) using the Web Animations API.
- Detail text: opacity 0 to 1 over 200ms starting at 180ms.
- Reduced motion: the view switches instantly with a 100ms fade.
Responsive behaviour
- Both positions are measured at the moment of the click, so the animation adapts to any container size.
- The close control is a real button and Escape also closes.
Code
<div class="box" id="box">
<div class="el" id="el" role="button" tabindex="0" aria-expanded="false" aria-label="Open Aurora details"><div class="in" id="in" hidden><b>Aurora</b><p>A soft gradient study used for backgrounds. The panel grew from the thumbnail you pressed.</p><button class="x" id="x" type="button">Close</button></div></div>
</div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.box{position:relative;width:min(300px,100%);height:200px;background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden}
.el{position:absolute;left:12px;top:12px;width:72px;height:72px;border-radius:12px;border:0;padding:0;background:linear-gradient(135deg,#dcdcd7,#aeaea8);cursor:pointer;transform-origin:top left;text-align:left;font:inherit;color:var(--text);overflow:hidden}
.el:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.el.open{left:8px;top:8px;width:calc(100% - 16px);height:calc(100% - 16px);cursor:default;border-radius:10px}
.in{display:block;padding:16px}
.in[hidden]{display:none}
.in b{display:block;font-size:17px;font-weight:600;margin-bottom:6px}
.in p{font-size:13px;line-height:1.5;color:#3d3d3d;margin-bottom:12px}
.x{display:inline-block;padding:6px 14px;border-radius:99px;background:var(--surface);border:1px solid var(--line);font:inherit;font-size:12.5px;cursor:pointer}
.x:focus-visible{outline:2px solid var(--accent);outline-offset:2px}const el=document.getElementById('el'),inn=document.getElementById('in'),x=document.getElementById('x');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
function flip(toOpen){const f=el.getBoundingClientRect();el.classList.toggle('open',toOpen);const l=el.getBoundingClientRect();
if(reduce){el.animate([{opacity:0},{opacity:1}],{duration:100});}
else el.animate([{transform:'translate('+(f.left-l.left)+'px,'+(f.top-l.top)+'px) scale('+(f.width/l.width)+','+(f.height/l.height)+')'},{transform:'none'}],{duration:380,easing:'cubic-bezier(.2,.7,.2,1)'});
inn.hidden=!toOpen;if(toOpen&&!reduce)inn.animate([{opacity:0},{opacity:1}],{duration:200,delay:180,fill:'backwards'});
el.setAttribute('aria-expanded',String(toOpen));el.setAttribute('role',toOpen?'group':'button');el.tabIndex=toOpen?-1:0;}
el.addEventListener('click',e=>{if(el.classList.contains('open')){if(e.target.closest('.x')){flip(false);el.focus();}return;}flip(true);x.focus();});
el.addEventListener('keydown',e=>{if(e.target===el&&!el.classList.contains('open')&&(e.key==='Enter'||e.key===' ')){e.preventDefault();flip(true);x.focus();}});
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&el.classList.contains('open')){flip(false);el.focus();}});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Shared Element Transition — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.box{position:relative;width:min(300px,100%);height:200px;background:var(--surface);border:1px solid var(--line);border-radius:14px;overflow:hidden}
.el{position:absolute;left:12px;top:12px;width:72px;height:72px;border-radius:12px;border:0;padding:0;background:linear-gradient(135deg,#dcdcd7,#aeaea8);cursor:pointer;transform-origin:top left;text-align:left;font:inherit;color:var(--text);overflow:hidden}
.el:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.el.open{left:8px;top:8px;width:calc(100% - 16px);height:calc(100% - 16px);cursor:default;border-radius:10px}
.in{display:block;padding:16px}
.in[hidden]{display:none}
.in b{display:block;font-size:17px;font-weight:600;margin-bottom:6px}
.in p{font-size:13px;line-height:1.5;color:#3d3d3d;margin-bottom:12px}
.x{display:inline-block;padding:6px 14px;border-radius:99px;background:var(--surface);border:1px solid var(--line);font:inherit;font-size:12.5px;cursor:pointer}
.x:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
</style></head>
<body><div class="stage">
<div class="box" id="box">
<div class="el" id="el" role="button" tabindex="0" aria-expanded="false" aria-label="Open Aurora details"><div class="in" id="in" hidden><b>Aurora</b><p>A soft gradient study used for backgrounds. The panel grew from the thumbnail you pressed.</p><button class="x" id="x" type="button">Close</button></div></div>
</div>
</div>
<script>
const el=document.getElementById('el'),inn=document.getElementById('in'),x=document.getElementById('x');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
function flip(toOpen){const f=el.getBoundingClientRect();el.classList.toggle('open',toOpen);const l=el.getBoundingClientRect();
if(reduce){el.animate([{opacity:0},{opacity:1}],{duration:100});}
else el.animate([{transform:'translate('+(f.left-l.left)+'px,'+(f.top-l.top)+'px) scale('+(f.width/l.width)+','+(f.height/l.height)+')'},{transform:'none'}],{duration:380,easing:'cubic-bezier(.2,.7,.2,1)'});
inn.hidden=!toOpen;if(toOpen&&!reduce)inn.animate([{opacity:0},{opacity:1}],{duration:200,delay:180,fill:'backwards'});
el.setAttribute('aria-expanded',String(toOpen));el.setAttribute('role',toOpen?'group':'button');el.tabIndex=toOpen?-1:0;}
el.addEventListener('click',e=>{if(el.classList.contains('open')){if(e.target.closest('.x')){flip(false);el.focus();}return;}flip(true);x.focus();});
el.addEventListener('keydown',e=>{if(e.target===el&&!el.classList.contains('open')&&(e.key==='Enter'||e.key===' ')){e.preventDefault();flip(true);x.focus();}});
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&el.classList.contains('open')){flip(false);el.focus();}});
</script>
</body></html>