Natalie Perret
Natalie Perret

Reputation: 8997

Bootstrap v4: text-only carousel only center text upon sliding completion (sticked to the top otherwise)

Any idea why this text-only carousel (Bootstrap v4) keeps my carousel item text sticked to the top and then center-align it instead of directly aligning the text of the item properly?

<div id="myTextOnlyCarousel" class="carousel slide mt-5" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#myTextOnlyCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myTextOnlyCarousel" data-slide-to="1"></li>
    <li data-target="#myTextOnlyCarousel" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner bg-dark text-light rounded text-center d-flex h-100 align-items-center justify-content-center" style="min-height: 300px;">
    <div class="carousel-item active">
      <h1>This is AWESOME!</h1>
    </div>
    <div class="carousel-item">
      <h1>This is freaking AMAZING!</h1>
    </div>
    <div class="carousel-item">
      <h1>YEAHHH</h1>
    </div>
  </div>
  <a class="carousel-control-prev" href="#myTextOnlyCarousel" 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="#myTextOnlyCarousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Upvotes: 0

Views: 137

Answers (2)

Gautam Naik
Gautam Naik

Reputation: 9358

Here is my solution JSFiddle

I have wrraped your h1 with a wrapper class.

I had to give min-height because without it, the items would stick at the top.

.wrapper {
  min-height:300px;
  display: flex;
  align-items: center;
  justify-content: center;
}

and removed some flex classes that were applied to .carousel-inner div

Upvotes: 0

MontyGoldy
MontyGoldy

Reputation: 749

Ok, It is because of the active class on carousel-item class that's what doing the text slide in animation. So change it to display block and add remove flex from carousel.

.carousel-item .text{
  display: flex;
  height: 100%!important;
  min-height: 300px;
  align-items: center;
  justify-content: center;
}

.carousel-item.active,
.carousel-item-next,
.carousel-item-prev{
    display:block;
}
<div id="myTextOnlyCarousel" class="carousel slide mt-5" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#myTextOnlyCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myTextOnlyCarousel" data-slide-to="1"></li>
    <li data-target="#myTextOnlyCarousel" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner bg-dark text-light rounded text-center  h-100  " style="min-height: 300px;background: red;">
    <div class="carousel-item active">
      <div class="text">
        <h1>This is AWESOME!</h1>
      </div>
    </div>
    <div class="carousel-item">
      <div class="text">
        <h1>This is freaking AMAZING!</h1>
      </div>
    </div>
    <div class="carousel-item">
      <div class="text">
        <h1>YEAHHH</h1>
      </div>
    </div>

  </div>
  <a class="carousel-control-prev" href="#myTextOnlyCarousel" 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="#myTextOnlyCarousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

Upvotes: 1

Related Questions