03. View Transitions API, @starting-style & CSS Anchor Positioning
🔴 Advanced
1. Definition
The View Transitions API (view-transition-name) animates DOM state changes smoothly across page navigation. @starting-style animates entry transitions on newly inserted DOM elements, and CSS Anchor Positioning (anchor-name, position-anchor) pins popovers directly to target trigger elements.
2. Why It Is Used
Anchor positioning eliminates complex JavaScript bounding-rect calculations for tooltips and dropdown menus, allowing CSS to position popovers relative to trigger anchors natively.
3. How It Works
An anchor element declares anchor-name: --my-anchor. A floating popover binds via position-anchor: --my-anchor and positions itself using top: anchor(bottom).
4. Syntax
/* Anchor Positioning Syntax */
.trigger-btn {
anchor-name: --tooltip-anchor;
}
.tooltip-popover {
position: absolute;
position-anchor: --tooltip-anchor;
top: anchor(bottom);
left: anchor(center);
}
5. Modern Living Spec Features Table
| Feature | Directive / Property | Purpose |
|---|---|---|
| View Transitions | view-transition-name: hero-img; | Cross-document and SPA page navigation morphing animations |
| Entry Animations | @starting-style { opacity: 0; } | Animates newly mounted DOM elements from initial state |
| Anchor Positioning | anchor-name / anchor() | Pins popovers & tooltips directly to anchor elements natively |
6. Beginner Example
.shared-card {
view-transition-name: product-card;
}
7. Intermediate Example
/* Entry animation for dialogs */
dialog[open] {
opacity: 1;
transform: scale(1);
transition: opacity 0.3s, transform 0.3s;
@starting-style {
opacity: 0;
transform: scale(0.9);
}
}
8. Advanced Anchor Popover Example
.anchor-btn {
anchor-name: --btn-anchor;
}
.popover-box {
position: fixed;
position-anchor: --btn-anchor;
top: anchor(bottom);
left: anchor(start);
margin-top: 8px;
}
9. Line-by-Line Code Explanation
top: anchor(bottom);: Binds the popover's top edge directly to the bottom edge of the anchor button element natively in CSS.
10. Expected Terminal / Browser Output
Tooltips and popovers pinned perfectly to target buttons without requiring JavaScript scroll listener calculations.
11. Common Mistakes & Pitfalls
- Forgetting fallback positioning for non-supported browsers: CSS Anchor positioning is a living spec feature; provide fallback absolute positioning for legacy engines.
12. Important Rules & Limitations
view-transition-namevalues must be unique per rendered page view.
13. Multilingual Explanation: English
CSS Anchor Positioning pins popovers to elements natively. @starting-style animates newly inserted DOM elements seamlessly.
14. Multilingual Explanation: Hindi (Roman Script)
CSS Anchor Positioning se tooltips aur popovers bina JS ke anchor button par pin hote hain.
15. Multilingual Explanation: Marathi (Roman Script)
CSS Anchor Positioning dware tooltips ani popovers button var naitik paddhatine align karta येतात.
16. Practice Questions
- How does
@starting-stylesimplify DOM mounting entrance animations?
17. Practical Coding Challenge
Write an @starting-style rule block for a .toast-notification that fades in from opacity: 0.