繁体   English   中英

Bootstrap轮播内垂直居中div

[英]Vertically center div inside Bootstrap carousel

我有以下结构:

<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

我想做的是将每个text-center div垂直对齐在carousel-items 我尝试使用flexbox但无法使其正常工作!

先感谢您!

这应该可以解决问题

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM