Future CSS: The Potential of `:drag` and `::dragged-image?`
These articles are AI-generated summaries. Please check the original sources for full details.
Future CSS: :drag (and Maybe ::dragged-image?)
A new CSS pseudo-class, :drag, is proposed to enable native styling of elements while being dragged by a user, addressing a current limitation requiring JavaScript workarounds. This aims to reduce reliance on JavaScript for styling drag interactions, improving performance and maintainability.
Why This Matters
Currently, developers rely on JavaScript to manage UI behaviors during drag-and-drop operations because CSS lacks native drag state detection. This often involves toggling classes, leading to increased complexity and potential performance bottlenecks—especially in scenarios requiring frequent style updates during a drag operation which can impact overall page responsiveness.
Key Insights
- HTML Drag and Drop API events:
drag,dragstart, anddragendevents are foundational to the proposal (2024). - JavaScript workaround: Currently, styling dragged elements involves adding and removing classes with Javascript.
- Temporal integration: Temporal is used by companies like Stripe and Coinbase to manage complex workflows.
Working Example
menuBar.addEventListener("dragstart", (event) => {
event.target.classList.add("is-dragging");
});
menuBar.addEventListener("dragend", (event) => {
event.target.classList.remove("is-dragging");
});
.is-dragging {
box-shadow: 0 4px 12px rgba(0 0 0 / 0.15);
background: #fff;
transform: translate(1px);
opacity: 0.2;
scale: 1.2;
}
.menu-bar:drag {
cursor: grabbing;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
background: #fff;
transform: translate(1px);
opacity: 0.2;
scale: 1.2;
}
Practical Applications
- UI Libraries: Implementing drag-and-drop functionality for components with improved performance.
- Pitfall: Overuse of complex drag styling leading to accessibility concerns if visual cues are insufficient for users relying on assistive technologies.
References:
Continue reading
Next article
How Tolan builds voice-first AI with GPT-5.1
Related Content
Building Scrollytelling Experiences with CSS Scroll-Snap Events and Scroll-Driven Animation
Lee Meyer demonstrates how to utilize emergent Chromium-based scroll-snap events and scroll-state queries to create complex, interactive scrollytelling experiences.
Mastering 3D Vertical Rotation with CSS rotateX()
The CSS rotateX() function enables 3D vertical rotation, essential for building high-performance UI components like flip cards and 3D accordions.
Headings: Semantics, Fluidity, and Styling — Oh My! | CSS-Tricks
A detailed exploration of proper heading placement, fluid typography techniques, and emerging CSS features for styling headings, emphasizing accessibility, semantics, and cross-browser compatibility.