how to make bootstrap 4 carousel images small on big screens

时间:2019-04-16 23:48:14

标签: html css bootstrap-4 carousel

I am trying to use bootstrap 4 carousel on my small project. I used the example code provided in their website, which is reproduced below. When I view the website from mobile, it is perfect, as expected. However, on the laptop screen, the slider images are half of the screen - very big. Even if I zoomed out the browser (chrome and Firefox), the images remain as big as they are. I tried to make a media query in the CSS file as follows:

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

However, it did not work. Below is the HTML code for your kind 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>

Your kind help is really appreciated.

1 个答案:

答案 0 :(得分:1)

按照示例操作,我已使用高度调整功能更新了旋转木马滑块。 (可选)根据您的要求调整媒体查询宽度和轮播滑块高度。

.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>