我使用Bootstrap 3做一个目录。当在平板电脑上显示时,产品图像看起来很难看,因为它们的尺寸很小(500x500),在浏览器中宽度为767像素。我想把图像放在屏幕的中央,但由于某种原因我不能。谁将帮助解决这个问题?
当前回答
只添加类中心块到一个图像,这适用于Bootstrap 4:
<img src="..." alt="..." class="center-block" />
注意:即使使用img-responsive, center-block也可以工作
其他回答
<div class="col-md-12 text-center">
<img class="img-responsive tocenter" />
</div>
.
<style>
.tocenter {
margin:0 auto;
display: inline;
}
</style>
我建议采用更“抽象”的分类。添加一个新类"img-center",它可以和.img-responsive类结合使用:
// Center responsive images
.img-responsive.img-center {
margin: 0 auto;
}
您仍然可以使用img-responsive,而不影响使用此样式类的其他图像。
你可以在这个标签前面加上section id/ div id/class来定义一个img被嵌套的顺序。这个自定义的img-responsive将只在该区域工作。
假设你有一个HTML区域定义为:
<section id="work">
<div class="container">
<div class="row">
<img class="img-responsive" src="some_image.jpg">
</div>
</div>
</section>
然后,你的CSS可以是:
section#work .img-responsive{
margin: 0 auto;
}
注意:这个答案与整体改变img-responsive的潜在影响有关。当然,中心块是最简单的解决方案。
如果你使用Bootstrap 3,只需使用.text-center类。
<div class="text-center">
<img src="..." alt="..."/>
</div>
注意:这对img-responsive不起作用
应用于所有Booostrap对象的更准确的方法是只使用标准类不设置上下边距(因为图像可以从父类继承这些),所以我总是使用:
.text-center .img-responsive {
margin-left: auto;
margin-right: auto;
}
我也做了一个Gist,所以如果任何变化将适用于任何bug,更新版本将永远在这里: https://gist.github.com/jdrda/09a38bf152dd6a8aff4151c58679cc66
推荐文章
- 使伸缩项目正确浮动
- 如何取消最大高度?
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何选择在最后一个子元素之前的元素?
- 如何触发自动填充在谷歌Chrome?
- CSS变换,锯齿边缘在铬
- 创建圈div比使用图像更容易的方法?
- 强迫孩子服从父母的弯曲边界在CSS
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在CSS中@apply是什么?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?