在Bootstrap 4传送带中居中图像(垂直和水平)

时间:2018-12-14 09:26:56

标签: html css bootstrap-4 carousel

我正在使用Bootstrap 4传送带,并且试图将图像垂直和水平居中。以下是我的代码以及样式:

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <ul class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
    </ul>

    <div class="carousel-inner">
        <div class="carousel-item active">
            <img src="https://placehold.it/200x200/c0392b/000">                      
        </div>
    </div>

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



#myCarousel {
    height: 100%;
    width: 100%;    
}

.carousel-indicators {
    margin-bottom: 8px;
}

.carousel-indicators>li {
    width: 10px;
    height: 10px;
    border-radius: 100%;
}

.carousel-inner {
    height: 100%;
    background-color: #213B74;
}

.carousel-item {
    text-align: center;
}

.carousel-item img {
    display: block;
    margin: auto;
}

目前,我仅添加了一个轮播项目进行测试。图像水平居中。但是,我尝试将图像的显示属性设置为阻止,将其边距设置为自动,但是它仍然没有垂直居中。有人可以告诉我我在做什么错吗?

我曾尝试调查与此相关的其他问题,但仍然找不到所需的答案。如果这个问题是重复的,如果您能告诉我我应该寻找哪个答案,我将不胜感激。

谢谢

3 个答案:

答案 0 :(得分:1)

我使用以下CSS代码修复了您的问题:

#myCarousel {
    height: 100vh;
    width: 100%;    
}

.carousel-indicators {
    margin-bottom: 8px;
}

.carousel-indicators>li {
    width: 10px;
    height: 10px;
    border-radius: 100%;
}

.carousel-inner {
    height: 100%;
    background-color: #213B74;
}

.carousel-item {
   height: 100%;
}

.carousel-item img {
   position: absolute;
   left: 50%;
   top: 50%;
   transform: translate(-50%, -50%)
}

因此,首先,我给轮播设置了100vh的高度以填充屏幕的总高度(也可以给它固定的高度)。然后,我制作了轮播项目height: 100%,以使其达到轮播的整个高度,然后按上述方法应用了轮播项目img代码,以将图像水平和垂直移动到中心。

答案 1 :(得分:1)

在这里,您需要在<div>内添加一个.carousel-item,然后在该div中放置您的图片。我们正在使用此调解器div来应用flex居中效果。

如果您尝试在flex上应用d-flex之类的.carousel-item类,则轮播将无法正常工作。

在这里,我添加了<div class="parent d-flex justify-content-center">,它将图像放置在图像中精确到div的中心的位置。另外,您还必须为div赋予min-height等于.carousel-item OR 的旋转木马一些标准高度,这样,如果内部图像较小,它就不会折叠。大小。

您可以进一步了解flex here,如果您想尝试一些示例,请转到here

#myCarousel {
  height: 100%;
  width: 100%;
}

.carousel-indicators {
  margin-bottom: 8px;
}

.carousel-indicators>li {
  width: 10px;
  height: 10px;
  border-radius: 100%;
}

.carousel-inner {
  height: 100%;
  background-color: #213B74;
}

.carousel-item {
  text-align: center;
}

.parent {
  min-height: 200px;
}

.carousel-item img {
  display: block;
  margin: auto;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <ul class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myCarousel" data-slide-to="1"></li>
    <li data-target="#myCarousel" data-slide-to="2"></li>
  </ul>

  <div class="carousel-inner">
    <div class="carousel-item active">
      <div class="parent d-flex justify-content-center">
        <img src="https://placehold.it/100x200/c0392b/000">
      </div>
    </div>
    <div class="carousel-item">
      <div class="parent d-flex justify-content-center">
        <img src="https://placehold.it/200x100/c0392b/000">
      </div>
    </div>
    <div class="carousel-item">
      <div class="parent d-flex justify-content-center">
        <img src="https://placehold.it/150x150/c0392b/000">
      </div>
    </div>
  </div>

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

答案 2 :(得分:0)

您可以使用align-items-centeralign-content-center Bootstrap 4 flex类来实现。参见下面的示例:

.main-wrapper {
  width: 100%;
  height: 100vh;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

<div class="main-wrapper">
  <div class="h-100 w-100 d-flex flex-wrap align-items-center align-content-center">
    <div class="mx-auto">

      Content

    </div>
  </div>
</div>