Search code examples
htmlcssbootstrap-4carousel

Can I move Bootstrap Carousel to the right instead of center?


I'm using Bootstrap Carousel, but not sure if it is possible to move the Carousel to the far right instead of putting it in the center. I've looked for an example of it but I could not find any other than moving the indicator to the right. I'd like to move the whole Carousel to the right, so I can have white space on the left. Can I do this with Bootstrap Carousel? Or should I look for some other way?

.mainCarousel {
    background-color: whitesmoke;
    margin-top: 0;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="mainCarousel">
    <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
            <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
            <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
            <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
        </ol>
        <div class="carousel-inner">
            <div class="carousel-item active">
                <img class="d-block w-100" src="images/1.jpg" alt="First slide">
            </div>
            <div class="carousel-item">
                <img class="d-block w-100" src="images/2.jpg" alt="Second slide">
            </div>
            <div class="carousel-item">
                <img class="d-block w-100" src="images/1.jpg" alt="Third slide">
            </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>
</div>


Solution

  • If you only just want to have non-writable whitespace on the left of the carousel, use the accepted answer's approach. In fact, you don't even need to set the width to 75%. You can just add left paddings to the carousel.

    If you want to have space on the left of the carousel so that you can put some texts there, and you want it to be responsive, use the grid system to construct the layout, and place the carousel on the right column.

    The following is just an example. I have no idea how long you want the carousel to be, and/or when you want the left stacks up on top of the right column:

    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-4">
                Writable area
            </div>
            <div class="col-lg-8">
                <div class="mainCarousel" />
            </div>
        </div>
    </div>
    

    At large breakpoint and up:

    enter image description here

    Below large breakpoint:

    enter image description here


    demo: https://jsfiddle.net/davidliang2008/sj541uoL/7/