• About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us
AimactGrow
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
AimactGrow
No Result
View All Result

Future CSS: :drag (and Possibly ::dragged-image?)

Admin by Admin
January 7, 2026
Home Coding
Share on FacebookShare on Twitter


Now, I do know what you’re considering. Yet one more CSS pseudo-class… However I believe this suggestion is somewhat cool.

Earlier this 12 months, it was proposed so as to add a brand new pseudo-class, :drag, that may allow builders to use kinds when a component is being actively dragged by the person. Presently, CSS lacks a mechanism to detect drag interactions, making it tough to handle UI behaviors that rely upon this motion with out counting on JavaScript.

No JavaScript! I like the concept of getting a pseudo-class devoted to this operate somewhat than going via the classList.toggle() route.

However, how would this work, although?

To know, you first should know that the HTML Drag and Drop API. A few of the occasions it fires embrace: 

  • drag (fires each few milliseconds when the aspect is dragged), 
  • dragstart (occasion fires on the preliminary drag), and 
  • dragend (occasion fires when the dragging the aspect stops).

Let’s take a fast have a look at how these drag-and-drop occasions work in JavaScript with the intention to perceive how they’d translate in CSS. Think about now we have seven button parts in a 

We are able to make all the .menu-bar draggable by slapping an attribute on it:

For our CSS, we merely give the is-dragging class some styling, which might be utilized solely when the aspect is dragged or moved:

In CSS, we are able to arrange an .is-dragging class that we’ll set on the aspect with JavaScript when it’s within the strategy of being dragged. These are the kinds we apply to the aspect when that’s occurring:

.is-dragging {
  box-shadow: 0 4px 12px rgba(0 0 0 / 0.15);
  background: #fff;
  rework: translate(1px);
  opacity: 0.2;
  scale: 1.2;
}

And right here’s the JavaScript to toggle between the beginning of the mouse drag and its finish. It listens for a dragstar occasion and provides the .is-dragging class to the .menu-bar. And after we drop the .menu-bar, the dragging enjoyable stops and the .is-dragging class is eliminated:

menuBar.addEventListener("dragstart", (occasion) => {
  occasion.goal.classList.add("is-dragging");
});

menuBar.addEventListener("dragend", (occasion) => {
  occasion.goal.classList.take away("is-dragging");
});

Our output would look one thing like this. Drag the dropdown aspect to see:

Not unhealthy! When the menu bar is dragged, it retains a picture of itself in its authentic place that's styled with the .is-dragging class. And whereas we had been completely in a position to knock this out with JavaScript, how cool would it not be to have that proposed :drag pseudo-class to summary all that script-y stuff:

.menu-bar:drag {
  cursor: grabbing;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  background: #fff;
  rework: translate(1px);
  opacity: 0.2;
  scale: 1.2;
}

+1 for efficiency! +1 for one much less dependency! +1 for maintainability!

How in regards to the preview picture?

Do you know we are able to model the precise aspect itself because it’s being dragged across the display screen? That’s known as the preview picture and we are able to change it with a

that we are able to add customized styling to.

A horizontal menu of buttons in a single row being dragged by a mouse cursor. We can see the menu in its original position and its current dragged position.
The browser shows a “preview” of the aspect as it's dragged.

It’s just a bit extra JavaScript utilizing the dataTransfer.setDragImage() operate:

const dragPreview = doc.createElement("div");

dragPreview.textContent = "📦 Dragging...";
dragPreview.model.cssText = `
  background: #fff6d6;
  border: 2px dashed orange;
  border-radius: 0.5rem;
  coloration: #333;
  padding: 0.5rem 1rem;
`;

doc.physique.appendChild(dragPreview);

// This replaces the default drag preview
occasion.dataTransfer.setDragImage(dragPreview, 0, 0);

// Take away after the occasion fires
setTimeout(() => dragPreview.take away(), 0);

Right here’s the place I’ll exit on a limb and recommend one other CSS pseudo particularly for that ::drag-image. Think about with the ability to sidestep all that JavaScript and straight-up write the kinds in CSS:

::drag-image {
  content material: "📦 Dragging...";
  padding: 0.5rem 1rem;
  background: #fff6d6;
  coloration: #333;
  border: 2px dashed orange;
  border-radius: 0.5rem;
}

I suppose it might be a pseudo-class as an alternative, nevertheless it appears like a pseudo-element makes extra sense since we’re speaking a couple of particular object somewhat than a state.

I opened a difficulty for that — give it a thumbs-up in case you’d discover it helpful to have a ::drag-image pseudo-element like that. The CSSWG is already slated to debate the :drag proposal. If that will get baked into the specs, then I’d push for the pseudo-element, too.

Ideas?

Yea or nay for drag-related pseudos? Would you attain for one thing like that, or do you're feeling steps on JavaScript’s toes an excessive amount of?

Tags: CSSdragdraggedimagefuture
Admin

Admin

Next Post
Easy methods to get an influence rod in Arc Raiders

Easy methods to get an influence rod in Arc Raiders

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

Is AI Higher than Bacon?

Is AI Higher than Bacon?

December 15, 2025
Early Knowledge Reveals Fewer Domains In US

Early Knowledge Reveals Fewer Domains In US

February 24, 2026

Trending.

AI-Assisted Menace Actor Compromises 600+ FortiGate Gadgets in 55 Nations

AI-Assisted Menace Actor Compromises 600+ FortiGate Gadgets in 55 Nations

February 23, 2026
10 tricks to begin getting ready! • Yoast

10 tricks to begin getting ready! • Yoast

July 21, 2025
Exporting a Material Simulation from Blender to an Interactive Three.js Scene

Exporting a Material Simulation from Blender to an Interactive Three.js Scene

August 20, 2025
Design Has By no means Been Extra Vital: Inside Shopify’s Acquisition of Molly

Design Has By no means Been Extra Vital: Inside Shopify’s Acquisition of Molly

September 8, 2025
Rogue Planet’ in Growth for Launch on iOS, Android, Change, and Steam in 2025 – TouchArcade

Rogue Planet’ in Growth for Launch on iOS, Android, Change, and Steam in 2025 – TouchArcade

June 19, 2025

AimactGrow

Welcome to AimactGrow, your ultimate source for all things technology! Our mission is to provide insightful, up-to-date content on the latest advancements in technology, coding, gaming, digital marketing, SEO, cybersecurity, and artificial intelligence (AI).

Categories

  • AI
  • Coding
  • Cybersecurity
  • Digital marketing
  • Gaming
  • SEO
  • Technology

Recent News

The Greatest Offers As we speak: Trails within the Sky 1st Chapter, Dragon Quest VII Reimagined, Nioh 3, and Extra

The Greatest Offers As we speak: Trails within the Sky 1st Chapter, Dragon Quest VII Reimagined, Nioh 3, and Extra

March 15, 2026
At the moment’s NYT Connections Hints, Solutions for Sept. 29, #841

Right now’s NYT Connections Hints, Solutions for March 16 #1009

March 15, 2026
  • About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved

No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved