css - 强制图像为正方形并在 css 中居中

标签 css twitter-bootstrap

我正在尝试调整不同图像的大小以适应一个方形 div。 似乎我在这个主题中找到了一个完美的答案: Force bootstrap responsive image to be square @web-tiki 的答案很好用。唯一的问题是,图像没有在周围的 div 中居中。 我不知道为什么我不能在这篇文章中提出我的问题,因此我必须写在这里。知道如何在 div 中居中我的(现在已完美调整大小的)图像吗?如果图像高于宽度,它会粘在 div 的左侧。如果宽度大于高度,它会粘在 div 的顶部。

最佳答案

这样试试

.img-container {
  position:relative;
}

.img-container img {
  position:absolute;
  margin:auto;
  top:0;
  right:0;
  left:0;
  bottom:0;
  max-height:100%;
  max-width:100%;    
}

JS Fiddle

关于css - 强制图像为正方形并在 css 中居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26648276/

相关文章:

css - 在CSS中为元素周围的边框设置动画

css - 页脚没有粘在底部,尽管我已经对它进行了编码

javascript - beforeShowMonth 在 Bootstrap 日期选择器中不起作用

javascript - 更改状态时 Bootstrap switchChange 事件仍然触发

html - 我应该将以前创建的 HTML 文件放在 Rails 应用程序的什么位置?

html - 无法将文本移动到页面 Bootstrap 顶部

html - 有 3 列 4 里

css - 伪 css .hover 解释

html - IE7 : Nested inline elements are causing parent to act like block

javascript - 同一页面中存在多个时间选择器的错误