How to vertically center images in Bootstrap Carousel using the img-fluid class

I'm attempting to center both horizontally and vertically the images in the bootstrap carousel using the img-fluid class. I want the images to be fully visible (100%) and adaptable to different screens, so I've been utilizing the img-fluid class. The issue arises when the images are not centered vertically - for landscape images, they end up positioned at the top with a large space below. However, portrait images do not face this problem.

Landscape:

https://i.sstatic.net/6NlZZ.jpg

Portrait

https://i.sstatic.net/o8WP1.jpg

Here is the code I'm using:

     <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" style="background-color:#333;">
          <ol class="carousel-indicators">
            <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
          </ol>
          <div class="carousel-inner">
            <div class="carousel-item text-center active">
              <img class="img-fluid mw-100 mh-100" src="https://storage.googleapis.com/...">
            </div>
          </div>
          <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
          </a>
          <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
      </div>

I've attempted using align-middle and other combinations without success. Any suggestions for what I can do?

Answer №1

I managed to resolve the issue on my own by utilizing absolute positioning and eliminating the img-fluid class.

Below is the updated code snippet:

    <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" style="background-color:#333;">
      <ol class="carousel-indicators">
        <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
      </ol>
      <div class="carousel-inner">
        <div class="carousel-item active" style="max-height:600px;">
          <img class="mw-100 mh-100" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto;" src="https://storage.googleapis.com/...">
        </div>
      </div>
      <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>


      <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
     </div>

Furthermore, here is the screenshot depicting the changes made:

https://i.sstatic.net/6na0B.jpg

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

Uncovering secret divs with the power of jQuery timing upon reload

Currently, I am in the process of developing a custom Wordpress theme for my blog which includes an overlay-container. When a button is clicked, this container slides in from the top and pushes down the entire page. To achieve this functionality, I am uti ...

Grab a parameter from the URL and insert it into an element before smoothly scrolling down to that

On a button, I have a URL that looks like this: www.mywebsite.com/infopage?scrollTo=section-header&#tab3 After clicking the button, it takes me to the URL above and opens up the tab labeled tab3, just as expected. However, I would like it to direct m ...

Is CSS Friendly Control Adapters responsible for triggering a security issue?

I have recently integrated the CSS Friendly Control Adapters dll into my project and included the CSSFriendlyAdapters.browser file in the "App_Browsers" folder. Here is the content of the file: <browsers> <browser refID="Default"> <co ...

Centered Bootstrap 4 modal with responsive width

Struggling to create a BootStrap 4 Modal that is centered in the screen and adjusts its width based on content? You're not alone. Despite trying different approaches like the container fluid and CSS methods, achieving a variable width modal is proving ...

Retrieve the input fields from a webpage

I'm facing a challenge with a large number of form fields on my webpage, about 50 in total. Sending an AJAX request for each field seems like a daunting task, and could potentially clutter my code and make it less readable. Is there a way to efficient ...

What are the best methods for visually comparing two HTML documents?

In the process of developing a Windows Forms application, my goal is to compare two HTML documents. The initial document is sourced externally and contains structural errors. To address this, an algorithm is utilized to optimize the HTML text, resulting in ...

The res.download() function in Express is failing to deliver the accurate URL to the client

When trying to utilize the res.download() method for downloading specific files from a server, there is an issue where triggering the res.download does not redirect the client to the correct URL. The files intended for download are located in a directory s ...

Convert HEX to RGB color format

Is it possible to utilize jQuery to call a function that converts a HEX value to RGB and then insert the HTML? Here is the JavaScript code: function convertHexToRGB( color ) { var r, g, b; if ( color.charAt(0) == '#' ) { color = ...

The CSS scale property is not working as expected when used in a React.js application, specifically

working environment ・next.js ・react ・typescript https://www.youtube.com/watch?v=ujlpzTyJp-M A Toolchip was developed based on the referenced video. However, the --scale: 1; property is not being applied. import React, { FunctionComponent ...

Preventing Text Truncation in THREE.js Texture Created from 2D Canvas

Many people tend to use the 2D canvas texture method for creating text billboards, sprites, and overlays in THREE.js scenes instead of relying on imported image files. You can check out an example by Lee Stemkoski here. However, I have noticed that when a ...

Tips for managing the velocity of JavaScript navigation scrolling

Hey everyone, I recently discovered this incredibly helpful JavaScript sticky side navigation script and it works like a charm! However, since I don't know much about JS, I was wondering if there's a way to slow down the scrolling speed? functio ...

A guide on incorporating ":host" (or ":host()") with ":has()"

Is there a way to properly use :host (or :host()) with :has() ? For instance, like the following code snippets: :host:has([disabled]) { opacity: 0.75; } or :host(:has([disabled])) { opacity: 0.75; } Unfortunately, the opacity value doesn't seem ...

Tips for Importing HTML Table Data Line by Line into SQL Table

I have the following code here. I am looking to insert HTML table data row by row into an SQL database. However, with this current code, only the last row is being stored in the database table. I believe I need to implement a loop to read the data from e ...

Creating a scrollable element in Tailwind without the need for a scrollbar is easier than you think

Is there a way to create a horizontal scroll navbar using Tailwind CSS without a scrollbar at the bottom, similar to this example (try reducing the width of your screen to see the scroll effect)? https://getbootstrap.com/docs/5.0/examples/blog/ I attempt ...

The arrow extends just a hair below the boundaries of the div, by approximately 1 pixel

I am facing an issue with my nav bar code. In Firefox and Chrome, everything works perfectly fine - there is a small arrow displayed below the links when hovered over. However, in Internet Explorer, the arrow appears slightly below the div, causing only pa ...

Problem with expanding DIV

Currently, I am working on enhancing this page. However, the DIV element containing the overflowing text does not expand to fit the text properly unless the height property is changed from "auto" to a fixed value. Here is the CSS code snippet for referen ...

How do I get my navbar to change color using a JavaScript scroll function?

I've been struggling with changing the color of my navbar when it's scrolled. I have some JavaScript code at the bottom that should handle this, but for some reason, it's not working as expected. I've double-checked that my bootstrap CD ...

Sending information to the styles feature in Angular 2

Is there a way to transfer data from an Angular tag to the styles in the @Component? Take a look at my component: import { Component, Input } from '@angular/core'; @Component({ selector: 'icon', template: `<svg class="icon"> ...

Guide to including Bootstrap 4 prerequisites (jQuery & Popper.js) through Browserify and Gulp

Currently, I'm in the process of setting up a default template for my bootstrap 4 projects. My plan is to utilize Browserify to include Bootstrap 4 along with its dependencies (jQuery & Popper.js) in my script file. However, I'm facing some confu ...

What iOS Printing and HTML formatter should you use and what level of sophistication is best?

Currently, I have successfully created an iOS 5 application that sends a job to the print queue. The functionality works fine and utilizes the UIMarkupTextPrintFormatter to generate the document in HTML format. At this point, I am using a simple HTML str ...