html - 你能用显示器: table-cell and have a fixed width/height?吗

标签 html css css-tables

我有以下内容:

CSS:

.photo {
  overflow: hidden;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  width: 100px;
  height: 100px;
}

HAML

.photo
  %img{:src => my_url}

参见 jsFiddle here .我用来演示的图像是 150px x 80px。

我需要在 .photo div 中显示图像并剪掉多余部分。图像需要垂直和水平居中。但是,display:table-cell 导致 .photo div 忽略我的宽度和高度设置。我该如何解决这个问题?

最佳答案

table-cell 不是垂直对齐的唯一解决方案。

.photo {
    overflow: hidden;
    background: #c0c0c0;
    display:inline-block; /* or float:left; */
    text-align: center;
    width: 100px;
    height: 100px;
    line-height:97px;
}
.photo img {
    vertical-align:middle;
    max-width:100%;
    max-height:100%;
}

关于html - 你能用显示器: table-cell and have a fixed width/height?吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6977313/

相关文章:

css - SourceSansPro-Firefox 中的粗体字体

css - 使用 Css 将 Tr 边框底部放在 Td 边框上,留在 TABLE 中

php - 我可以从服务器端选择的数据直接将数据注入(inject) session 存储吗?

html - css 文件未被识别

javascript - NotFoundError 未找到节点 image_div.parentNode.removeChild(img)

css - 显示 : Table-cell not setting height constantly xbrowser (ie8 table bug)

html - 在表格中间对齐图标

javascript - 使用 JavaScript 插入 HTML Img 标签时遇到问题

javascript - 基于屏幕尺寸的动态面板高度

php - 如何在 url 中附加用户名而不是 cakephp 3 中的 user_id