htmlcssangularangular-materialangular-cdk-drag-drop

Style cdkdrag while dragging in angular


I am using .cdk-drag-preview to style while dragging my tab, but I should use own class and also I should not use ! important

If am trying to add higher css specify also it's not working.

How to do that

Html

`<div cdkDrag>
{{name}}
<mat-icon> settings</mat-icon>
</div>`

Css


.cdk-drag-preview{
width : fit-content ! important;
height: fit-content ! important;
background-color - white;
}

I tried to add one class above this and tried

.tab .cdk-drag-preview {}

It doesn't work


Solution

  • cdkDragPreview is an Angular CDK directive used to define a custom preview element that shows while dragging.

    Instead of using the default preview (.cdk-drag-preview), you can place an inside your draggable element to fully control the look of the drag image.

    It renders only while dragging and replaces the default ghost element.

    <div cdkDrag>
      {{name}}
      <mat-icon>settings</mat-icon>
    
      <ng-template cdkDragPreview>
        <div class="my-custom-preview">
          {{name}} <mat-icon>settings</mat-icon>
        </div>
      </ng-template>
    </div>