我希望我的旋转木马图像在中心(水平),这不是默认的。我遵循this topic的解决方案。
但是,使用此解决方案时,当轮播调整大小并小于图像时,图像将被裁剪,而不是按默认比例缩放。
我如何既使图像居中,又使其伸展到旋转木马项目?
发布于 2015-02-02 01:17:18
现在(在Boostrap 3+上)很简单:
.carousel-inner img {
margin: auto;
}
发布于 2013-10-25 15:12:59
我假设你有不同大小的图片。我自己测试过了,它就像你描述的那样工作(总是居中,图像宽度合适)
/*CSS*/
div.c-wrapper{
width: 80%; /* for example */
margin: auto;
}
.carousel-inner > .item > img,
.carousel-inner > .item > a > img{
width: 100%; /* use this, or not */
margin: auto;
}
<!--html-->
<div class="c-wrapper">
<div id="carousel-example-generic" class="carousel slide">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<img src="http://placehold.it/600x400">
<div class="carousel-caption">
hello
</div>
</div>
<div class="item">
<img src="http://placehold.it/500x400">
<div class="carousel-caption">
hello
</div>
</div>
<div class="item">
<img src="http://placehold.it/700x400">
<div class="carousel-caption">
hello
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="icon-next"></span>
</a>
</div>
</div>
这就产生了一个“跳跃”,因为可变的高度...要解决这个问题,可以尝试下面这样的代码:Select the tallest image of a list
或者使用media-query设置您自己的固定高度。
发布于 2017-08-29 17:13:23
在Boostrap4上,只需将mx-auto
添加到您的旋转木马图像类中。
<div class="carousel-item">
<img class="d-block mx-auto" src="http://placehold.it/600x400" />
</div>
根据需要与bootstrap carousel documentation中的样本相结合。
https://stackoverflow.com/questions/19582340
复制相似问题