Search code examples
angularowl-carousel

owl-carousel positions next and previous below images


I'm using Angular 17 with ngx-owl-carousel-0 : 17.0.0 package.

I have my customOptions set as:

customOptions: OwlOptions = {
  loop: true,
  mouseDrag: false,
  touchDrag: false,
  pullDrag: false,
  dots: false,
  navSpeed: 700,
  margin:10,
  navText: ["<", ">"],
  responsive: {
    0: {
      items: 1
    },
    400: {
      items: 2
    },
    760: {
      items: 3
    },
    1000: {
      items: 4
    }
  },
  nav: true
}

and for the moment in my page I have the default template of:

<owl-carousel-o [options]="customOptions">
  <ng-template carouselSlide>
    <div class="slide">
      <img src="https://via.placeholder.com/600/92c952" alt="img 1" />
    </div>
  </ng-template>
  <ng-template carouselSlide>
    <div class="slide">
      <img src="https://via.placeholder.com/600/771796" alt="img 2" />
    </div>
  </ng-template>
  <ng-template carouselSlide>
    <div class="slide">
      <img src="https://via.placeholder.com/600/24f355" alt="img 3" />
    </div>
  </ng-template>
  <ng-template carouselSlide>
    <div class="slide">
      <img src="https://via.placeholder.com/600/d32776" alt="img 4" />
    </div>
  </ng-template>
  <ng-template carouselSlide>
    <div class="slide">
      <img src="https://via.placeholder.com/600/f66b97" alt="img 5" />
    </div>
  </ng-template>
  <ng-template carouselSlide>
    <div class="slide">
      <img src="https://via.placeholder.com/600/56a8c2" alt="img 6" />
    </div>
  </ng-template>
</owl-carousel-o>

and this is my output. You can see the navigation buttons at the bottom of the carousel, though not the end of the world, I cannot seem to get them to the left and right of the carousel itself.

enter image description here

in my Angular.json file I have:

"styles": [
  "src/styles.css",
  "node_modules/bootstrap/dist/css/bootstrap.min.css",
  "node_modules/ngx-owl-carousel-o/lib/styles/prebuilt-themes/owl.carousel.min.css",
  "node_modules/ngx-owl-carousel-o/lib/styles/prebuilt-themes/owl.theme.default.min.css"
],
"scripts": [
  "node_modules/jquery/dist/jquery.min.js"
]

Solution

  • We can use the below CSS to achieve what you want, I sue position: absolute to overlay the nav over the slides to get the desired effect!

      ::ng-deep .owl-carousel  {
        position: relative !important;
      }
      ::ng-deep .owl-nav {
        position: absolute !important;
        top: 0 !important;
        left: 0 !important;
        bottom: 0 !important;
        right: 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
      }
    

    If you are not a fan of ::ng-deep you can move the styles to the global styles and wrap them in a class, so that they get applied conditionally based on the class!

    FULL CODE:

    import { Component } from '@angular/core';
    import { bootstrapApplication } from '@angular/platform-browser';
    import { CarouselModule, OwlOptions } from 'ngx-owl-carousel-o';
    import 'zone.js';
    
    @Component({
      selector: 'app-root',
      styles: [
        `
          ::ng-deep .owl-carousel  {
            position: relative !important;
          }
          ::ng-deep .owl-nav {
            position: absolute !important;
            top: 0 !important;
            left: 0 !important;
            bottom: 0 !important;
            right: 0 !important;
            display: flex !important;
            align-items: center !important;
            justify-content: space-between !important;
          }
        `,
      ],
      standalone: true,
      imports: [CarouselModule],
      template: `
        <owl-carousel-o [options]="customOptions">
          <ng-template carouselSlide>
            <div class="slide">
              <img src="https://via.placeholder.com/600/92c952" alt="img 1" />
            </div>
          </ng-template>
          <ng-template carouselSlide>
            <div class="slide">
              <img src="https://via.placeholder.com/600/771796" alt="img 2" />
            </div>
          </ng-template>
          <ng-template carouselSlide>
            <div class="slide">
              <img src="https://via.placeholder.com/600/24f355" alt="img 3" />
            </div>
          </ng-template>
          <ng-template carouselSlide>
            <div class="slide">
              <img src="https://via.placeholder.com/600/d32776" alt="img 4" />
            </div>
          </ng-template>
          <ng-template carouselSlide>
            <div class="slide">
              <img src="https://via.placeholder.com/600/f66b97" alt="img 5" />
            </div>
          </ng-template>
          <ng-template carouselSlide>
            <div class="slide">
              <img src="https://via.placeholder.com/600/56a8c2" alt="img 6" />
            </div>
          </ng-template>
        </owl-carousel-o>
      `,
    })
    export class App {
      customOptions: OwlOptions = {
        loop: true,
        mouseDrag: false,
        touchDrag: false,
        pullDrag: false,
        dots: false,
        navSpeed: 700,
        margin: 10,
        navText: ['<', '>'],
        responsive: {
          0: {
            items: 1,
          },
          400: {
            items: 2,
          },
          760: {
            items: 3,
          },
          1000: {
            items: 4,
          },
        },
        nav: true,
      };
    }
    
    bootstrapApplication(App);
    

    Stackblitz Demo