图像高度保持响应,没有滚动

时间:2014-02-04 22:10:33

标签: html css responsive-design width

这里的简单问题,但努力实现我想要的最佳方式...我有一个图像,我试图保持在浏览器中的中心和响应。将宽度设置为100%,图像完美(水平)响应。我遇到的麻烦就是身高。我不希望浏览器滚动并使用正确的比例,在中心,使用20px填充。

Dropbox查看器有一个很好的例子,说明我想要完成的任务,请参阅此处:https://www.dropbox.com/s/7zmau5ckx9qe2q1/P-20131215-00017_HiRes%20JPEG%2024bit%20RGB.jpg

更新以下更新代码

我在这里有一个关于我到目前为止的演示(如果你垂直浏览浏览器,滚动条会出现):http://jsfiddle.net/k7JG5/7/

HTML

<div id="top_nav">Logo Here</div>
<div id="img_wrap">
<center><img src="http://goldenleafdesigns.com/images/random-  images/soul_id_select_image1.jpg" /><center>
</div>

CSS

body {
margin: 0px;
padding: 0px;
background-color: #000;
}
#top_nav {
height: 44px;
width: 100%;
background-color: #FFF;
    text-align: center;
    line-height: 44px;
}
#img_wrap {
max-width: 100%;
max-height: 100%;
padding: 20px;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
img {
outline: 0;
max-width: 100%;
    max-height: auto;
}

所以,最好的方法是达到高度以保持响应而不滚动?

2 个答案:

答案 0 :(得分:1)

也许这适合你:

http://jsfiddle.net/4LNND/

#img_wrap {
    max-width: 100%;
    max-height: 100%;
    padding: 20px;
    margin-top: 0px;
    margin-right: auto;
    margin-bottom: 0px;
    margin-left: auto;
    position:absolute;
    bottom:0;
    left:0;
    right:0;
    top:44px;
}
#img_wrap img {
    outline: 0;
    max-width: 100%;
    max-height: 100%;
    display:block;
}

答案 1 :(得分:0)

有一些更复杂的方法,这是最简单的

<强> jsBin demo

HTML:

<div id="img_wrap"></div>

CSS:

#img_wrap{
  position:absolute;
  top:    20px;
  right:  20px;
  bottom: 20px;
  left:   20px;
  background: url('pathToImage.jpg') no-repeat center center fixed;
  background-size: contain;
}

//如果您不介意图片被截止,请代替contain使用cover