Skip to content
Search
Search the documentation
Styling
GitHub 0

Styling

Style the zones that can take the dragged item, the element under the pointer and the placeholder where the item would land.

A pointer drag involves three elements you may want to style: the zones that can take the item, the dragged element that follows the pointer, and the placeholder in the list. Each has a demo: drop target styling, custom dragged element and custom shadow placeholder.

Drop targets

While an item is dragged, every zone that can accept it gets the inline styles of dropTargetStyle and the classes of dropTargetClasses. Both are removed when the drag ends. The default dropTargetStyle is a yellow outline, { outline: "rgba(255, 255, 102, 0.7) solid 2px" }.

			<ul
    {@attach dndzone(() => ({
        items,
        dropTargetStyle: {},
        dropTargetClasses: ["drop-target"],
        onconsider: update,
        onfinalize: update,
    }))}
>
    <!-- ... -->
</ul>

<style>
    :global(.drop-target) {
        outline: 2px dashed royalblue;
    }
</style>
		
  • dropTargetStyle: {} turns the default outline off.
  • The keys of dropTargetStyle are CSS property names. They are set as inline styles, so they win over your stylesheets, and at the end of the drag they are set to empty. An inline style of the same property that the zone had before the drag is lost.
  • The library adds the classes at runtime, so Svelte does not see them in your markup and removes scoped rules for them. Write the rules with :global(...), in a global stylesheet, or use utility classes such as Tailwind’s that exist globally.

A zone can accept the item when it has the same type and does not set dropFromOthersDisabled. The zone the drag started from is styled too. Keyboard drags style the zones the same way.

The dragged element

At the start of a pointer drag, the library clones the item’s element and appends the clone to the document body, or to the enclosing <dialog> or popover. The clone has position: fixed, a z-index of 9999, a grabbing cursor and the id DRAGGED_ELEMENT_ID ("dnd-action-dragged-el"). The original element is moved out of the list and hidden, and the placeholder in the list is the element your #each block renders for the shadow item.

When the clone hovers a zone, it morphs to look like the placeholder there: it takes the placeholder’s size, background, padding, font and colors, with a short transition. morphDisabled: true keeps the clone’s own shape.

There are two ways to style it:

  • With CSS, through its id. The clone is not part of any component, so use a global rule:

    			:global(#dnd-action-dragged-el) {
        box-shadow: 0 8px 20px rgb(0 0 0 / 0.25);
    }
    		

    The clone keeps the classes of the original element, so rules that match those classes match it too.

  • With the transformDraggedElement option, a function that receives the clone, the item’s data and the index the item would get if it were dropped now. It runs each time the zone renders the placeholder, after the morph, so what it sets wins. It runs again and again, so it should give the same result for the same input.

    			function transformDraggedElement(element?: HTMLElement, data?: Task, index?: number) {
        if (!element || !data || index === undefined) return;
        element.style.rotate = "3deg";
        const label = element.querySelector("[data-label]");
        if (label) label.textContent = `${data.name} (#${index + 1})`;
    }
    		

centreDraggedOnCursor: true moves the clone’s centre to the cursor when the drag starts, which helps with large items over small ones. useCursorForDetection: true uses the cursor instead of the clone’s centre to find the zone and index, without moving the clone. dropAnimationDisabled: true drops the clone in place instead of animating it to its new position.

The shadow placeholder

During a pointer drag, the zone the item hovers renders a shadow item, marked with isDndShadowItem: true. The library sets visibility: hidden on its element, so it shows as a gap of the item’s size. To draw something in the gap, render the shadow item differently and make your content visible again:

			<script lang="ts">
    import { SHADOW_ITEM_MARKER_PROPERTY_NAME } from "@lukez/svelte-dnd";
</script>

{#each items as item (item.id)}
    <li>
        {#if item[SHADOW_ITEM_MARKER_PROPERTY_NAME]}
            <div class="placeholder">Drop here</div>
        {:else}
            {item.name}
        {/if}
    </li>
{/each}

<style>
    .placeholder {
        visibility: visible;
        border: 2px dashed currentColor;
    }
</style>
		

Keyboard drags have no shadow item: the item itself moves in the list.

Edit this page Last updated: Nov 22, 2026