【问题标题】:Vertically center div inside Bootstrap carousel在 Bootstrap 轮播中垂直居中 div
【发布时间】:2019-04-01 15:05:57
【问题描述】:

我有以下结构:

<div class="carousel" id="my-carousel">
  <div class="carousel-item">
    <div class="text-center">
      ...
    </div>
  </div>
  <div class="carousel-item">
    <div class="text-center">
      ...
    </div>
  </div>
  <div class="carousel-item">
    <div class="text-center">
      ...
    </div>
  </div>
</div>

我还有以下用于标准化轮播的脚本:

function normalizeCarousel() {
  function normalize() {
    var maxHeight = 0;
    $(".carousel-item").each(function() {
      var currentHeight = $(this).height();
      if (currentHeight > maxHeight) {
        maxHeight = currentHeight;
      }
    });
    $("#my-carousel").css("height", maxHeight);
  };

  normalize();
  $(window).on("resize orientationchange", function() {
    normalize();
  });
}

$(document).ready(function() {
  normalizeCarousel();
});

这里是 JSFiddle:JSFiddle

我要做的是垂直对齐carousel-items 内的每个text-center div。我尝试过使用flexbox,但无法成功!

提前谢谢你!

【问题讨论】:

    标签: html css twitter-bootstrap vertical-alignment


    【解决方案1】:

    这应该可以解决问题

    .carousel-item {
      display: table;
    }
    .text-center {
      display: table-cell;
      vertical-align:middle;
    }
    

    【讨论】:

    • 它似乎不起作用,这些项目现在显示在左上角的某个地方,然后它们消失了。
    • @GeorgeR。现在呢?
    • 不,现在项目重叠了,几个项目显示在同一个轮播幻灯片中。
    • 我在问题描述中添加了一个!
    【解决方案2】:

    使用 flexbox,使用类似的东西

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
    <style>
    /* Make the image fully responsive */
    .carousel-inner img {
        width: 100%;
        height: 100%;
    }
    
    div.text-center {
        position: absolute;
        display: flex;
        justify-content: center;
        flex-direction: column;
        align-items: center;
        text-align: center;
        height: 100%;
        width: 100%;
        color: red;
    }
    </style>
    </head>
    <body>
    
    <div id="demo" class="carousel slide" data-ride="carousel">
    
    <!-- Indicators -->
    <ul class="carousel-indicators">
    <li data-target="#demo" data-slide-to="0" class="active"></li>
    <li data-target="#demo" data-slide-to="1"></li>
    <li data-target="#demo" data-slide-to="2"></li>
    </ul>
    
    <!-- The slideshow -->
    <div class="carousel-inner">
    <div class="carousel-item active">
    <div class="text-center">Text Vertically Center Aligned</div>
    <img src="la.jpg" alt="Los Angeles" width="1100" height="500">
    </div>
    <div class="carousel-item">
    <div class="text-center">Text Vertically Center Aligned</div>
    <img src="chicago.jpg" alt="Chicago" width="1100" height="500">
    </div>
    <div class="carousel-item">
    <div class="text-center">Text Vertically Center Aligned</div>
    <img src="ny.jpg" alt="New York" width="1100" height="500">
    </div>
    </div>
    
    <!-- Left and right controls -->
    <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
    </a>
    <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
    </a>
    </div>
    
    </body>
    </html>
    

    【讨论】:

    • 这不行,我相信这与flexbox覆盖了Bootstrap轮播正在使用的隐藏属性有关。
    • 请检查我的更新答案,如果您遇到任何问题,请告诉我
    猜你喜欢
    • 2018-02-05
    • 2015-02-01
    • 2013-11-07
    • 1970-01-01
    • 2022-07-19
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    相关资源
    最近更新 更多