Tips for adjusting the size and positioning the ng bootstrap carousel in the center

My Angular project is using ng bootstrap, but I'm facing a styling issue. The content doesn't display in the center, rather it appears in the upper left corner:

example

I would like the content to be centered and wide under the blue headbar.

While trying to use CSS for this, the only close result I achieved was by manually setting the image width and height with:

style="width: 100%; height: 877px"

However, I believe there must be a better solution out there.

Any suggestions on how to achieve this?

UPDATE WITH CODE:

    <div class="carouselCont">
    <ngb-carousel *ngIf="top10">
          <ng-template *ngFor="let film of top10.items" ngbSlide >
          <div class="picsum-img-wrapper" (click)="onClick(film)">
            <img src="{{film.image}}" alt="Random first slide" style="width: 100%; height: 877px"/>
          </div>
          <div class="carousel-caption" (click)="onClick(film)">
            <h3>{{film.rank}} - {{film.title}}</h3>
            <p>{{film.year}}</p>
          </div>
        </ng-template>
    </ngb-carousel>
    </div>

Answer №1

The ngb-carousel is set to 100% width by default, but you can customize it using CSS:

ngb-carousel 
{
    max-width:250px;
    margin:auto
}

You can also dynamically adjust the max-width based on the first image loaded:

<ngb-carousel *ngIf="images" [style.max-width.px]="width">
  <ng-template ngbSlide>
        <div class="picsum-img-wrapper">
            <img #img [src]="images[1]" alt="Random second slide"
             (load)="setWidth($event.target)" />
        </div>
  </ng-template>
 ...
</ng.carousel>

Make sure to define the width and set it accordingly when the image loads:

width=0;
setWidth(target:any)
{
    this.width=target.naturalWidth;
}

(Remember to include margin:auto in your CSS)

Check out the code example on stackblitz

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When the mouse leaves, the input search will automatically change the text color to black

I'm having trouble developing a search input, as I am facing an issue and unable to find a solution. Currently, the search input is working fine; when I click on it, it expands. However, once expanded and I start typing, the text color turns white wh ...

What is the best way to incorporate HTML entities into my Angular2 project?

I am currently using Angular-CLI to develop a straightforward style guide application. Unfortunately, I have encountered some issues when including preformatted HTML markup that contains HTML entities: Uncaught Error: Template parse errors: Unknown entit ...

What is the best way to prioritize .py-md-6 over .p-3 in Bootstrap 4?

I have set the padding for the class .py-md-6 as follows: .py-md-6 { padding-top: 6rem !important; padding-bottom: 6rem !important; } Despite this, there seems to be a conflict with the .p-3 class in my HAML element: #id.p-3.py-md-6 It appears th ...

angular-oauth2-oidc - Issue with missing 'State' and 'Scope' parameters

One crucial requirement set by the identity server is to refrain from including 'state' and 'scope' in the URL. The specified request format is as follows URL?app=xxx&response_type=code&client_id=yyy&state=zzz&redirect_ ...

Toggle visibility with JQuery's onClick event

I am looking for a way to toggle the visibility of text that has display: none; in CSS when a button is clicked, as well as clear the input field on the onClick event. Currently, the text remains visible even after it has been displayed. Any assistance wou ...

Bootstrap 5.3 does not allow custom background colors to take precedence

Ever since the update to Bootstrap 5.3.1, I've been facing an issue where two custom background colors are not overriding the colors set by Bootstrap, even when using !important. Strangely enough, they were working fine before the update. Here are my ...

Dealing with a multitude of parameters in the Angular 2/4 router: What you need to know

Within my application, the URL structure can vary with multiple parameters followed by an optional pattern. Here are some examples of possible URLs: http://example.com/{param1} http://example.com/{param1}/constant/{id} http://example.com/{param1}/{param ...

The formatting directive fails to keep pace with speedy input

I am working on a feature to automatically format the input field as the user types, transforming the letters into valid initials in uppercase with dots in between. The formatting needs to happen in real-time as the user inputs characters, rather than aft ...

Having issues with floats in Bootstrap?

I'm facing an issue trying to float two elements within a Bootstrap navigation bar, but for some reason it's not working. .cls1 { float: left !important; } .cls2 { float: right !important; } <link href="https://maxcdn.bootstra ...

What is the best way to ensure string comparison safety in Angular templates when dealing with null or undefined values?

Okay, so I encountered the following scenario: <span *ngIf="!someItem?.description==''"> The situation is: If someItem is defined and the description is not an empty string. Essentially, this can be interpreted as not equal to any value X ...

Angular (version 2.4.5) is throwing an error stating that you cannot bind to the 'ngTemplateOutletContext' property because it is not recognized as a valid property of the 'ng-container' component

While I understand that there have been similar questions regarding errors during the migration from Angular 4 to 5, my issue pertains to building an Angular 2.4.5 project with webpack. Despite successful compilation without errors, attempting to run the p ...

Leverage the power of NPM Libraries in Angular using Angular-CLI

I am attempting to integrate the npm library progressbar.js into my Angular 2 application built using Angular-CLI. After installing it with npm install progressbar.js --save, I encountered an issue as the documentation suggests using var ProgressBar = req ...

An issue occurred with Ionic 4: TypeError - Unable to access property 'name' as it is undefined

None of the answers to similar questions have provided a solution for me SITUATION: I've been setting up a SQL Server connection from my Ionic app. You can check out my previous question for more details The workflow goes as follows: Ionic connects ...

`The problem of div width error``

I am trying to display two divs side by side with a width of 50% each. Below these two divs, there is another div with a full width of 100%. Check out my code below: <div class="col_1_2"></div> <div class="full-description">description& ...

'OR' separated by the <hr> tag

This particular code functions correctly on the Firefox and Chrome browsers. However, there seems to be an issue with IE and Opera, where the 'OR' text is only visible within a 2px height area. hr { height: 2px; border: 0; background-color ...

What is the best way to align a GIF and two rows of text within a container div?

Check out my JSFiddle below: In this fiddle, there is an image sized 32 by 32 on the left side and two lines of text on the right. I need help aligning the center of the gif and the text with the middle of the enclosing frame. I've tried adjusting t ...

Troubleshooting Bootstrap image alignment problem with columns on extra small screens

My website showcases screenshots of my software in a div element. Everything looks great on larger screens, but once I switch to a phone or resize the window, the images don't align properly, leaving empty space. Refer to the image below for clarifica ...

Tips on how to dynamically load the content of a URL into a modal and update the browser address simultaneously

I am attempting to achieve a specific effect using JavaScript and jQuery Link to the content I want to load in a modal The goal is to load the content in a modal while keeping the main page in the background. Additionally, when a URL is followed, I want ...

Automatically generate a file path for imports using the class name

When working on my angular 2 application in Visual Studio, I often wish there was a way to automatically populate the import path similar to how C# uses statements work with the 'ctrl .' key combination. Typing out imports repeatedly can be tedio ...

Challenge with module declaration in index.d.ts during upgrade from Angular 8 to 9 (excluding Material)

In my index.d.ts file, I have declared two modules like so: declare module 'googlemaps'; declare module 'detect-resize'; Previously, these declarations worked perfectly fine, allowing me to utilize these modules. The googlemaps module ...