extended image in a bootstrap carousel

Having an issue with Bootstrap 4 carousel images getting stretched. Not sure what the problem is.

.carousel {
  margin-bottom: 4rem;
  position: relative;
}

.carousel-caption {
  bottom: 3rem;
  z-index: 10;
}

.carousel-inner img {
  position: absolute;
  top: 0;
  left: 0;
  height: 32rem;
  object-fit: containt !important;
  display: block;
}

.carousel-item {
  height: 32rem;
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
    <div class="carousel-item active col-xs-12 col-sm-12">
      <img src="https://i.sstatic.net/cPtnU.jpg" class="d-block w-100" alt="...">
      <div class="carousel-caption d-none d-md-block">
        <h1>Lorem</h1>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident, minima.</p>
      </div>
    </div>
    <div class="carousel-item col-xs-12 col-sm-12">
      <img src="https://i.sstatic.net/cPtnU.jpg" class="d-block w-100" alt="...">
      <div class="carousel-caption d-none d-md-block">
        <h1>Lorem</h1>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident, minima</p>
      </div>
    </div>
    <div class="carousel-item col-xs-12 col-sm-12">
      <img src="https://i.sstatic.net/cPtnU.jpg" class="d-block w-100" alt="...">
      <div class="carousel-caption d-none d-md-block">
        <h1>Lorem</h1>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Provident, minima</p>
      </div>
    </div>
  </div>
</div>

Here's how it currently looks:

https://i.sstatic.net/lfnny.png

This is the original image: https://i.sstatic.net/cPtnU.jpg

If anyone can help me figure this out, I would greatly appreciate it.

Answer №1

Why not experiment with object-fit: cover !important; instead of sticking to object-fit: contain !important; in order to achieve the desired outcome?

If your goal is to position the background so that it covers both horizontally and vertically without distorting the image, consider using background-size: cover; or object-fit: cover;.

Answer №2

Adjusting the img height to auto can be done by:

.carousel-inner img {
  position: absolute;
  top: 0;
  left: 0;
  height: auto
  object-fit: containt !important;
  display: block;
}

An alternative is to set width to auto while retaining the height value:

.carousel-inner img {
  ....
  height: 32rem;
  ....
  width : auto; /* or 100% */
}

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

Animating CSS Skill Bars

My goal is to create a skill bar that smoothly slides in the background color of the progress while keeping the label for the skill in a fixed position. .skill-container * { box-sizing: border-box; } .skill-container { width: 100%; border-radiu ...

JavaScript does not interact with HTML content that has been loaded through jQuery AJAX requests

I'm currently working on developing a form filled with ajax requests, but I've encountered an issue where my javascript isn't interacting with the HTML content that was loaded through an ajax call to my server. ---template---- <form id=" ...

Having trouble changing a CSS property (margin-right)?

I have created a simple HTML list that consists of various items: <div id="menu"> <ul> <li>apples</li> <li>&#149</li> <li>bananas</li> <li>oranges</li> <li>grape ...

Website navigation toolbar with access level control

I'm currently working on a website with various webpage links in the navigation menu, and I want to restrict access for non-admin users. Would it be better to set up different access levels or just use passwords on each page? What approach would be mo ...

Anchored text is obscured by a fixed Bootstrap 4 navigation bar at the top

I'm currently developing a website that utilizes bootstrap 4. I have successfully fixed the <nav> element to the top of the page using the fixed-top class. Despite applying padding-top to the <body> tag as recommended, I am encountering an ...

Issue with applying two gradient colors when hovering over navbar links in Bootstrap 4 using Sass

As a newcomer to the world of Sass, I've searched high and low for a solution to my specific issue, but haven't been able to find one. Can anyone help me out? I managed to create a Bootstrap 4 navbar with a gradient background and gradient navba ...

Encountering issue when deploying multiple HTML files using Parcel

Whenever I deploy a website with multiple entry points and many HTML files, and the host uses the build command: parcel build index.html aboutme.html, the deployed website gives me a 404 error. However, if I manually enter /aboutme.html or /index.html in t ...

Apply CSS styling (or class) to each element of a React array of objects within a Component

One issue I'm facing involves adding specific properties to every object in an array based on another value within that same object. One such property is the background color. To illustrate, consider an array of objects: let myObj = { name: "myO ...

``Troubleshooting Problem with Tailwind's Flex Box Responsive Grid and Card Elements

Starting point: Preview https://i.sstatic.net/zfzBU.jpg Code : <div class="container my-12 mx-auto"> <div className="flex flex-wrap "> {error ? <p>{error.message}</p> : null} {!isLoading ...

Manage the flow of traffic between a stationary webpage and the Express framework

My Apache web server is configured to proxy node applications in a virtualhost environment. One of my express applications serves a static web page: app.use(express.static('.')) app.set('views', path.join(__dirname, 'views') ...

Aligning container divs at the center in various screen resolutions

I recently constructed a portfolio website using Bootstrap and Isotope JS. In my layout, I have a container div which works perfectly when viewed on a desktop browser - displaying 3 divs in one line. However, the issue arises when the resolution changes an ...

Make multiple images in one row resize proportionally instead of wrapping them to the next line

My phone's screen is small and I want the three images to remain in the same row. In case they don't fit, they should shrink proportionately to maintain alignment (refer to image at the bottom). Consider the code snippet below: <div id="exam ...

JS switch for numerous container elements

How can I create a toggle slider in HTML for multiple elements displayed using foreach? Each element should open a div block with specific information and have a close button. Unfortunately, I haven't been able to find any suitable code to achieve thi ...

Modifying the appearance of radio buttons using jQuery

I'm new to jQuery and I'm finding it challenging. Currently, I have a set of three radio buttons with the third button already prechecked. My aim is to change the CSS class of the checked button, making it similar to an unchecked button with the ...

Using a HTML value as an argument in a React component

Can someone help me figure out how to pass an HTML value as a parameter for my function? Here is the code snippet I have: <input type ="text" placeholder="Smart Contract Bytecode" name="name" id ="scbytecode" className="nice-textbox"/> <button i ...

Present information from a list in a table format

My webpage contains a single table where I want to showcase an image in row1 along with its details. As each image is displayed, its corresponding details are added to a list. There are a total of 27 images spread across 9 rows in the table. Once all the ...

CSS Conditional Enrollment feature malfunctioning

Struggling to register a conditional CSS for a SharePoint 2013 masterpage. No matter what style I apply, the layout remains unchanged. After researching, it seems like this is how I should register the CSS: <SharePoint:CssRegistration ID="CssRegistra ...

Ensuring the height of the HTML element for menu items matches the navigation bar

I've been struggling to customize a navigation bar styled with Bootstrap 4. My goal is to create a hover effect on the menu items similar to the image or code snippet provided, but without that annoying thin blue flashing offset below the item. Despit ...

The Label in Material UI TextField is appearing on top of the border,

Incorporating Material UI TextField and Material UI Tab, I have encountered an issue. There are two tabs, each containing a text field. Upon clicking on the TextField, the label's border is supposed to open, but this behavior only occurs if the curren ...

Delayed suggestions using Typeahead and Bloodhound technology

I have a situation where certain fields require a three-letter shortcode for locations. These shortcodes are not widely known and are mainly familiar to a select group of long-time users. For example: LND - London SWN - Swindon The dropdown menu displa ...