简体   繁体   English

设置 ng-bootstrap carousel 的宽度

[英]setting the width of ng-bootstrap carousel

I'm using ng-bootstrap to create a carousel for my angular project.我正在使用 ng-bootstrap 为我的 angular 项目创建一个轮播。 I have succeeded in following the example shown at https://ng-bootstrap.github.io/#/components/carousel/examples and now have a carousel in the middle of the screen.我已经成功地遵循了https://ng-bootstrap.github.io/#/components/carousel/examples 中显示的示例,现在屏幕中间有一个轮播。

I now want the carousel to take up the entire width of the screen, instead of just sitting in the middle.我现在希望旋转木马占据屏幕的整个宽度,而不是仅仅坐在中间。 How do I do this?我该怎么做呢?

I have tried adding a class to the carousel and changing the width as such:我尝试向轮播添加一个类并更改宽度:

component HTML组件 HTML

<ngb-carousel *ngIf="images" class="myclass">
  <ng-template ngbSlide>
    <img [src]="images[0]" alt="Random first slide">
    <div class="carousel-caption">
      <h3>First slide label</h3>
      <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
    </div>
  </ng-template>
  <ng-template ngbSlide>
    <img [src]="images[1]" alt="Random second slide">
    <div class="carousel-caption">
      <h3>Second slide label</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </ng-template>
  <ng-template ngbSlide>
    <img [src]="images[2]" alt="Random third slide">
    <div class="carousel-caption">
      <h3>Third slide label</h3>
      <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
    </div>
  </ng-template>
</ngb-carousel>

component css组件CSS

.myclass{
   width: 100%;
}

在您的 html 标签中直接添加示例:

<img [src]="images[0]" alt="Random first slide" class="mh-100" style="width: 100%; height: 50vh">

You have to set you myclass to your images.您必须将myclass设置为您的图像。 You are using bootstrap so you can use w-100 instead:您正在使用引导程序,因此您可以改用w-100

Try it online! 在线试试吧!

<ngb-carousel *ngIf="images">
  <ng-template ngbSlide>
    <img [src]="images[0]" alt="Random first slide" class="w-100">
    <div class="carousel-caption">
      <h3>First slide label</h3>
      <p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
    </div>
  </ng-template>
  <ng-template ngbSlide>
    <img [src]="images[1]" alt="Random second slide" class="w-100">
    <div class="carousel-caption">
      <h3>Second slide label</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
  </ng-template>
  <ng-template ngbSlide>
    <img [src]="images[2]" alt="Random third slide" >
    <div class="carousel-caption">
      <h3>Third slide label</h3>
      <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
    </div>
  </ng-template>
</ngb-carousel>

You may also want to use object-fit:cover;您可能还想使用object-fit:cover;

You could do that too.你也可以这样做。 Instead of using w-100 using !Important而不是使用 w-100 使用 !Important

<img [src]="images[0]" alt="Random first slide" width="100%">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM