Unable to properly configure Bootstrap carousel to fit the screen - struggling to make it work

Apologies, this is now the 1754th question on this particular topic. Despite my extensive research, I still can't seem to get it to work as intended. After reviewing various answers, I created the following test case.

The challenge lies in maintaining the aspect ratio while ensuring the picture takes up the full width, even in portrait mode. Ideally, portraits should utilize the available 100vh height with some space on the sides.

Many have noted that achieving this without a background image is tricky. Unfortunately, I haven't found a solution using a background image either. If that would make things simpler, I am willing to explore that option.

CSS / HTML

.full-size-carousel.carousel { height: calc(100vh - 56px);}
.full-size-carousel.carousel-inner,.full-size-carousel.carousel-item { height: 100%;}
.full-size-carousel.carousel-item img { height: 100%; object-fit: cover; object-position: center;}

Despite successful implementation in a Bootstrap 5 sandbox, this example did not function as expected. Additionally, my actual code failed to produce the desired outcome on my local machine.

Answer №1

If someone is in need of a solution, I managed to achieve it using js/jQuery.

By adjusting the image tag below based on the aspect ratios of the image and the screen/window size, I was able to solve the issue:

<img src="myImg.jpg" class="carouselImage d-block w-100" >

// Determine width/height onload and set listener
setSize();
window.addEventListener("resize", onResizeInstant);

function onResizeInstant() {
    setSize();
}

function setSize() {
    const heightHeader = 20;
    const carouselImages = $('.carouselImage');
    var addClass;
    var removeClass;

    carouselImages.each(
        function(){     
            const imgHeightInit = $(this).height();
            const imgWidthInit  = $(this).width();
            const imgAspectRatio = imgWidthInit/imgHeightInit;

            const windowHeight = window.innerHeight - heightHeader;
            const windowWidth  = window.innerWidth;
            const windowAspectRatio = windowWidth/windowHeight;

            if(windowAspectRatio > imgAspectRatio) {
                addClass = 'h-100';
                removeClass = 'w-100';
                imgHeight = windowHeight;
                imgWidth = imgAspectRatio * imgHeight;
            } else {
                addClass = 'w-100';
                removeClass = 'h-100';
                imgWidth = windowWidth;
                imgHeight = imgWidth / imgAspectRatio;
            }
            $(this).addClass(addClass).removeClass(removeClass);
    })
}

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

Develop your website layout with Flexbox (Bootstrap 4.2), using stacked grid designs similar to the float: left method

For my Wordpress theme, I am utilizing Bootstrap 4.2 to design a basic grid layout for a list of blog posts. In my design, I have a Card that is double the height of others and I am attempting to 'float' two single-height cards beside it. Previ ...

Html content overlapping div rather than being stacked vertically

My goal is to arrange a group of divs inside another div in a vertical stack. However, I am facing an issue where the text from the last div is overlapping with the second div instead of following a proper vertical alignment where the text in the third div ...

Issues with slideToggle in IE8 caused by CSS in JQuery

I am facing an issue where my div elements are supposed to toggle visibility using jQuery's "slideToggle" function (I'm utilizing jquery-1.2.6.min.js). This functionality is working as expected! However, I am encountering a problem specifically w ...

Dynamic gallery with customizable features (HTML/CSS)

Seeking guidance on creating a seamless html/css gallery without any spacing between each gallery element. All elements are identical in size, housed as list items within a ul inside a div. Media queries have been established to adjust site and image siz ...

What is the best way to center a heading element within a section on a webpage?

I need assistance aligning the h1 element with the specific class wish to the center of its section, both horizontally and vertically. Unfortunately, I have been unable to achieve this thus far. Any guidance would be greatly appreciated. Below you will fi ...

The pattern for the input text in HTML was dysfunctional, specifically when trying to input a negative number or a

Currently delving into the world of regex, where I am exploring ways to restrict user input. After testing on regextester.com with the following pattern: The results were not as expected +10 e10 ^([-,0-9.]+) This configuration yielded successful result ...

Resizing Navigation Bar in WordPress Theme WP Clarion

Struggling to enlarge the size of the navbar below and urgently need to have it done before tonight. I've reached out to the developers for support but still need to find a solution: Check out the site here: penarthpc.com/~nuvolaor/ Unfortunately, t ...

JavaScript popup is no more visible on the webpage

Recently, I implemented a pop-up on my website that used cookies to prevent it from appearing every time a user visited a page. However, after making this change, the pop-up stopped showing altogether. Despite my best efforts in testing, researching, and s ...

Top Bootstrap dropdown menu overlaid with Leaflet zoom buttons

I'm encountering an issue depicted in the screenshot below. Is this related to bootstrap or leaflet? How can I adjust the position/style of the dropdown menu so that it appears above all other elements? https://i.sstatic.net/cQhN5.png ...

jQuery code not being triggered on secondary page

I'm currently working on a website project for one of my university courses (Link: ) and I'm encountering an issue that I could use some help with. The problem involves a "back-to-top" button that uses jQuery to smoothly scroll the page upwards. ...

What is the best way to stack text boxes vertically in CSS/HTML?

How can I arrange these textboxes so that .textbox appears at the top, followed by textbox1 below? CSS .textbox { border: 1px solid #848484; -moz-border-radius-topleft: 30px; -webkit-border-top-left-radius: 30px; border-top-left-radius: ...

Modifying the HTML5 data attribute according to the URL hash

Within my div element, I have this code: <div id="nav-aj" data-options='{"default":'t1","animation": {"duration": 500, "effects": "fade"}}'> </div> I am looking to dynamically adjust the value of the default option based on th ...

Angular, PHP, and MySQL working together to establish database connectivity

Greetings! I am facing some challenges with a small project involving mySQL and PHP for the first time. My main focus right now is on establishing connectivity. Despite following various tutorials, I have been unable to connect to the database and keep enc ...

The touchend event and target _blank are not working together harmoniously

With both image and link hover states in place, I've implemented a piece of code to ensure that opening a link on iOS only requires a single tap. Check out the code below. However, I've encountered an issue where using a link with target="_blank ...

Encountering a connectivity problem with cx_oracle in a Flask application

Currently, I am in the process of creating an application that allows users to input data from an Excel spreadsheet and then upload that information to a database based on whether they select option A or option B. The problem I am encountering is that aft ...

Navigating the ins and outs of Bootstrap 4 grids

I am trying to create a layout with two columns, each using the class col-md-6. In both columns, there are six images. However, I want to have only one image displayed in the left column if the right column is empty. How can I achieve this? The current se ...

Verify whether the value is in the negative range and implement CSS styling in React JS

I have been developing a ReactJS website where I utilize Axios to fetch prices from an API. After successfully implementing this feature, I am now looking for a way to visually indicate if the 24-hour change percentage is positive by showing it in green, a ...

My DIV elements are not responding to the widths I set for them. What could be the issue?

Recently, I've been delving into the world of Flex to experiment with some unique layouts. However, I encountered an issue where my divs were not recognizing widths as expected. I attempted to set the first div of each row to 5% width so they would ...

How can I insert an HTML/PHP code snippet into a <ul> list using JavaScript?

My upload.php file saves images into an uploads/ folder within my main directory. I am looking to dynamically add a new li tag to my index file each time an image successfully uploads. Here is a snippet from index.php: <ul> <li><im ...

Having trouble with conditional statements in tpl file on Prestashop (PHP, Smarty)?

I'm having trouble implementing the following: Showing a conditional voucher on the order confirmation page. The voucher depends on two specific conditions: the weight of the order and whether the user has an account Currently, I am working on the f ...