7

I'm using Overlay Module of Angular's CDK. And I just cannot find the way to add position: absolute to the overlay? The module will receive a top and a left position that matches the connected element position but the overlay will not receive position: absolute. I'm unable to add position absolute due to ViewEncapsulation. and I'm really struggling to find a solution.

Question: How can I use cdkOverlayOrigin and cdkConnectedOverlayOrigin

Module:

import { OverlayModule } from '@angular/cdk/overlay';



@NgModule({
  imports: [
    OverlayModule,
  ],
  declarations: [ MenuComponent ],
})
export class MenuModule {
}

Component:

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: [ './menu.component.scss' ],
})
export class MenuComponent {
}

Template:

 <div style="margin: 0 auto; width: 30%">
  <h1 cdkOverlayOrigin
      #checkListTrigger="cdkOverlayOrigin"
      (click)="checkListOpen = !checkListOpen"
      style="background: blue">Overlay Origin</h1>
  <ng-template
    cdkConnectedOverlay
    [cdkConnectedOverlayOrigin]="checkListTrigger"
    [cdkConnectedOverlayOpen]="checkListOpen">

    <ng-container>
      <p>Why you no positioned on h1 element</p>
    </ng-container>

  </ng-template>
</div>

See how it's off center enter image description here

If I add position: absolute it now works as intended? enter image description here

4
  • Is there a reason you're using OverlayModule instead of the Angular - Material mat-dialog? material.angular.io/components/dialog/overview (This being the standard for usking cdk items in Angular)
    – Z. Bagley
    Commented Dec 16, 2017 at 2:03
  • Because I'm trying to make a popover overlay/tooltip and not a dialog box persay Commented Dec 16, 2017 at 2:28
  • The dialog is supposed to be the all-browser standard for anything that requires more than a span for rendering in DOM. If all you want is text, then Material provides the tooltip. You can position the dialog to an element in the view w/ customization, and you can manipulate it many more ways (though the defaults are prettys tandard)
    – Z. Bagley
    Commented Dec 16, 2017 at 4:40
  • It isn't a tooltip either. joejordanbrown.github.io/material2-popover-demo/popover is something more that I'm looking for. Commented Dec 16, 2017 at 16:30

3 Answers 3

22

The absolute positioning of the generated overlay container is defined in the .cdk-overlay-pane class, which is part of the angular material theme. You might have forgot to include a theme?

If so, add the following line to your style.css file (assuming you use an Angular CLI project). @import "~@angular/material/prebuilt-themes/indigo-pink.css";

1
  • 1
    this answer helped me to focus on core problem: missing theme imports in styles.scss. I added @use '@angular/material' as mat; //use tilda '~@angular/..... for angular <13 and @import '@angular/material/theming'; //use tilda '~@angular/..... for angular <13 and @include mat.core();
    – niio
    Commented Jun 7, 2022 at 10:11
3

This is described in the API documentation. You can specify a panel class for the overlay pane.

Here's some code:

const overlay = overlay.create({
    panelClass: 'pos-absolute'
});
.pos-absolute {
    position: absolute;
}
1
1

Inspired by niio's comment, I figured out I had not included the core part of angular material theming (Angular 15):

@use '@angular/material' as mat;
@include mat.core();

That put the select panel back to place.

Not the answer you're looking for? Browse other questions tagged or ask your own question.