【问题标题】:Vertically align image inside a bootstrap carousel在引导轮播中垂直对齐图像
【发布时间】:2015-12-07 05:15:06
【问题描述】:

我正在尝试编写我的第一个网站,我必须构建一个轮播。问题是我有一个较小的图像(400x400),我试图将它垂直对齐到轮播中,并在右侧放置一些文本。我尝试使用旋转木马上的position: relative; 和带有图像+ 的div 上的absolute

top: 50%;
transform: translateY(-50%);

不起作用。我想是因为它旁边的轮播标题..

如果没有我上面提到的对齐方式,这就是现在的样子

http://i.imgur.com/LqoXWMo.png

这是 HTML

<!-- Start Carousel -->
    <div id="product-detail carousel-example-generic" class="carousel slide" data-ride="carousel">
      <!-- Wrapper for slides -->
      <div class="carousel-inner" role="listbox">
        <div class="item active">
          <div class="container">
            <div class="carousel-img"></div>
            <div class="carousel-caption">
              <p class="cat-title">Mercury</p>
            </div>
          </div>
        </div>
        <div class="item">
          <img src="..." alt="...">
          <div class="carousel-caption">
            ...
          </div>
        </div>
        ...
      </div>

      <!-- Controls -->
      <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left control-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right control-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>

<!-- End Carousel -->

CSS 我有用于轮播的:

/*************************
    CAROUSEL
*************************/

.carousel .carousel-inner {
    height: 550px;
    margin-bottom: 100px;
    background: #faf9f9;
}

.carousel-control.right,
.carousel-control.left {
    background-image: none;
}

.control-icon {
    color: #393939;
    text-shadow: none;
}

.carousel-inner {
    position: relative;
}

.carousel-img {
    width: 400px;
    height: 400px;
    background-image: url('../img/jewelery.jpg');
    background-size: cover;
}

.cat-title {
    text-shadow: none;
}

【问题讨论】:

    标签: html css twitter-bootstrap


    【解决方案1】:

    试试这个,看看是否有帮助:

    首先让你 .item 相对位置,并给它一个 100% 的高度,因此它与容器 .carousel-inner 占用相同的高度

    .item{
        position: relative;
        height:100%;
    }
    

    然后接下来将您的绝对位置样式添加回您的.carousel-img 元素

    .carousel-img {
        ...
        position: absolute;   
        top: 50%;
        transform: translateY(-50%);
    }
    

    请参阅此fiddle 以获取示例。

    我还注意到您的轮播 (550px) 和 carousel-img (400px) 上有静态高度,因此您可以将 margin-top:75px; 添加到您的 .carousel-img

    看到这个fiddle

    【讨论】:

    • 完美运行!谢谢!
    • 我得到了正确的图像,但现在我的标题有问题。它跨越了整个容器,但我想做的是把图像放在左边和一些文字在它的右边.. 它应该是这样的 -> i.imgur.com/AA5peOi.png 有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 2014-05-26
    • 2016-05-15
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    相关资源
    最近更新 更多