Tips for resizing Bootstrap 4 carousel images for larger screens

I've been working on a small project and trying to incorporate the Bootstrap 4 carousel. I followed the example code provided on their website, shown below. Everything looks great when I view the website on mobile, as expected. However, on a laptop screen, the slider images take up half of the screen - they're too big. Even when I zoom out in the browser (using Chrome and Firefox), the images remain oversized. To try and fix this, I attempted to create a media query in the CSS file like so:

@media only screen and (min-width: 768px) {
 .myCarousel .carousel .carousel-inner .img {
height: 250px;}
}

Unfortunately, this solution did not work. Below is the HTML code for reference:

       <div class="myCarousel">
        <div id="carouselCaptions" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators">
                <li data-target="#carouselCaptions" data-slide-to="0" class="active"></li>
                <li data-target="#carouselCaptions" data-slide-to="1"></li>                  
            </ol>
            <div class="carousel-inner">
                <div class="carousel-item active" data-interval="2000">
                    <img src="{% static 'img/slider/slide1.jpg' %}" class="d-block w-100" alt="">
                    <div class="carousel-caption">
                       <h5>First Slide Caption</h5>
                    </div>
                </div>
                <div class="carousel-item" data-interval="2000">
                    <img src="{% static 'img/slider/slide2.png' %}" class="d-block w-100" alt="">
                    <div class="carousel-caption">
                       <h5>Second Slide Caption</h5>
                    </div>
                </div>
            </div>
            <a class="carousel-control-prev" href="#carouselCaptions" 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="#carouselCaptions" role="button" data-slide="next">
                <span class="carousel-control-next-icon" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>
        </div>
    </div>

Any help or suggestions would be greatly appreciated.

Answer №1

Check out the modified carousel slider with adjustable height. Feel free to customize the media query width and carousel slider height according to your needs.

.carousel .carousel-item img {
    height: 200px;
    object-fit: cover;
}

@media (min-width: 768px) {
.carousel .carousel-item img {
    height: 350px;
    object-fit: cover;
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<div class="myCarousel">
    <div id="carouselCaptions" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
            <li data-target="#carouselCaptions" data-slide-to="0" class="active"></li>
            <li data-target="#carouselCaptions" data-slide-to="1"></li>
        </ol>
        <div class="carousel-inner">
            <div class="carousel-item active" data-interval="2000">
                <img src="https://via.placeholder.com/1920x650" class="d-block w-100" alt="">
                <div class="carousel-caption">
                    <h5>First Slide Caption</h5>
                </div>
            </div>
            <div class="carousel-item" data-interval="2000">
                <img src="https://via.placeholder.com/1920x750" class="d-block w-100" alt="">
                <div class="carousel-caption">
                    <h5>Second Slide Caption</h5>
                </div>
            </div>
        </div>
        <a class="carousel-control-prev" href="#carouselCaptions" 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="#carouselCaptions" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>
</div>

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 trying to find a substring within a string in JavaScript, the "else if" statement may not be triggered

I'm currently working on creating a Hangman game using HTML and JS. Most of the code is functioning properly, but I've hit a roadblock. For some reason, one part of my if else statement isn't working correctly. Here's the line of code: ...

Are current web browsers able to block the code "<a href="javascript:window.open....?

I am looking to create a pop-up window for sharing on Facebook. The best way to achieve this is by using javascript to pop up a small window with a width of 400 pixels and a height of 200 pixels. Will pop-up blockers in Chrome, IE, or Google block this f ...

Emphasize a div container within a content-editable field as though it were regular text

I currently have a unique JS Fiddle demonstrating a div with a background image inside a content editable input. My goal is to highlight the div as if it were regular text. .input { border: 1px solid black; width: 200px; } .emoji { width: 16px; ...

Notifying users with Angular bootstrapped alerts directly from a

I have a service that is set up to listen for incoming events. When an event occurs, I need to notify the user. Currently, I am using alert(). My goal is for a Bootstrap alert to pop up in every component when the service triggers. Is there a way to achie ...

How can I make a fixed background image with NextJS's Next/Image feature?

Lately, I've been exclusively using Next/Image due to Vercel's powerful image optimization service. While I've successfully replaced background-image with DIVs and z-index in most scenarios, I'm facing challenges in achieving two specif ...

Copy the content of one input field into another field at the same time

I have encountered an issue that seems simple, yet the proposed solutions have not been effective. Most suggestions involve using jQuery, but Bootstrap 5 has shifted away from jQuery. I am looking for a solution using JavaScript or HTML only. The problem ...

Unveil as you scroll - ScrollMagic

I am working on a skill chart that dynamically fills when the document loads. I am exploring the possibility of using ScrollMagic to animate the chart filling as the user scrolls down. After trying various combinations of classes and pseudo-classes in the ...

Inserting images into a MySQL database using Ajax and file uploads

I am using JQuery Mobile, PHP, and Ajax to upload an image and insert it into a database, but I am facing an issue with inserting the image's name in the database. The result in the database shows "C:fakepathLighthouse.jpg", but I only want to insert ...

Divs expanding below content in IE on a particular server

Currently, I am facing an issue with a JavaScript div that expands correctly over other content in most situations but goes under the content in one specific scenario. I am unsure about where to begin debugging this problem. To provide some context, the d ...

The basic spinning hover animation is not functioning properly

Can anyone help me with creating a hover effect on an inner circle that causes two outer rings to rotate? I've been struggling to make it work as expected. When I hover over the inner circle, the outer rings only move to the bottom right corner withou ...

What is the best way to arrange an unordered list in a horizontal layout?

I have four lists that I want to display side by side. The first row should show a Doctor and Patient side by side, the second row should show a Pharma Company and Employee side by side. However, in my code, this layout is not working as intended. .tree ...

CSS HTML parent div cannot contain image properly

Creating a website using Bootstrap: <div class="row padding_zero"> <div class="col section px-0"> <img class="img-trans padding_zero" src="./svg/clouds-00.svg" style="margin-top: -150px& ...

Include a floating element within a modal box

I am trying to display some cards within a Bootstrap modal form. Inside the modal, I want the cards to appear in two columns by using the "col-md-5" class. .mycard.col-md-5 card contents However, when I use the "col-md-5" class, it adds a property o ...

Expanding Images with React-Bootstrap Cards

I need assistance with setting up cards in React-Bootstrap. The Card component is enlarging my images beyond their original sizes. When I attempt to place the image in an img tag outside of the card, it displays at its normal size. Can someone provide guid ...

Tips for resizing an image to fit within a col-sm-12 column in Bootstrap

I have a dilemma with two columns - I want to place my image in the first column so that the second column can move down. see image here Here is my HTML code: <div class="container-fluid"> <div class="row"> ...

What is the best way to leverage environment variables in my CSS within the context of Next.js for defining the primary color scheme?

Is there a way to use an environment variable to specify the base color in my CSS? I am working with NEXTJS. All of my colors are defined as variables in my globals.css: @layer base { :root { ... --primary: #0096d0; --primary-bright: ...

What steps can I take to ensure that the content remains intact even after the page is

Hey there, I hope you're having a great start to the New Year! Recently, I've been working on creating a calculator using HTML, CSS, and JavaScript. One thing that's been puzzling me is how to make sure that the content in the input field do ...

How can I apply a unique CSS class to specific rows within an h:dataTable?

I am looking to apply unique CSS classes to certain rows within my <h:dataTable>. Is it possible to manipulate and style the individual table rows in a customized manner? ...

Making the black background stretch to the full height and allowing for scrolling when necessary

Check out this webpage where the text in the box extends beyond the page when scrolling, revealing the full content while the black background expands correctly. Visit this page to see that the text in the box is small, but I want the black background t ...

Modifying the structure of serialized data

After serializing a JS form, the data looks like this: .....&xx=xxx&otherError=&input=SMS&message=sdfgs&...... Can anyone provide guidance on how to replace the value of message with the content of a textarea before making an ajax cal ...