html - 图像 CSS 和高度相对于文件的尺寸而不是容器的尺寸

标签 html css image

我有一个嵌套在流体布局中的图像,如下所示:

<div id="full_image">
  <div class="t">
    <div class="t_c">
      <img src="image.jpg" />
      <div id="close"></div>
    </div>
  </div>
</div>

使用 CSS:

#full_image{
    position: absolute;
    background: rgba(0, 0, 0, 0.7);
    left: 0px;
    right: 0px;
    top: 0px;
    text-align: center;
    bottom: 0px;
    display: block;
    z-index: 100;
}
#full_image img{
    max-width: 90%;
    max-height: 90%;
}
#full_image #close{
    width: 32px;
    height: 32px;
    position: absolute;
    top: 10px;
    right: 10px;
    background: url('../images/close2.png');
    cursor: pointer;
}
.t{
    display: table;
    width: 100%;
    height: 100%;
    position: absolute;
}
.t_c{
    display: inline-block;
    width: 100%;
    vertical-align: middle;
    height: 100%;
}

但是,#full_image img 选择器上的 max-widthmax-height 属性似乎只会根据图像文件的尺寸。因此,例如,图片为 600px x 600px,尽管 #full_image 分隔线为 300px x 300px,但 CSS 会将图片调整为 540px x 540px。

JS fiddle :http://jsfiddle.net/R8smE/2/

谢谢。

最佳答案

我想这就是你想要的 http://jsfiddle.net/R8smE/6/

现在它垂直居中

.t{
    width: 100%;
    height: 100%;
    position: relative;
}

#full_image img{
    max-height: 90%;
    max-width: 90%;
    left: 50%;
    top: 50%;
    position: absolute;
    transform: translate(-50%, -50%);    
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
}

关于html - 图像 CSS 和高度相对于文件的尺寸而不是容器的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20993565/

相关文章:

html - 如何使用 jekyll bootstrap 导航栏颜色进行更改?

html - 如何在html、css中处理PNG图像加载时间

javascript - jQuery 动画整体表现不佳。我怎样才能让它更顺畅?

html - 在 <map> <area> 中定位 <div>

html - div 中的图像在图像下方有额外的空间

jquery - 幻灯片 CSS 背景

javascript - 在 html5 中创建 Web Worker 时使用 setTimeout()

html - DOMPDF:生成一个同样对齐的表

image - Delphi:如何在单击按钮数组时制作程序?

javascript - 谷歌搜索框需要移动到屏幕中间(X :0 Y:0)