【发布时间】:2012-12-14 00:24:34
【问题描述】:
我的目标是在具有固定纵横比的 div 中显示图像(或其他内容),它具有最大宽度,但如有必要会按比例缩小。
下面的 jsFiddle 展示了我到目前为止所得到的。它确实适用于 IE8。在 Firefox 和 Chrome 中,内部 div 没有完全填满外部 div,底部有一个小间隙。 Safari 显示错误的纵横比。
<!doctype html>
<html>
<head>
<title>Fixed Aspect Ratio</title>
<style>
.keepaspect
{
position: relative;
max-width: 750px;
margin: auto;
/* Box Shadow */
-moz-box-shadow: 0px 0px 10px #000;
-webkit-box-shadow: 0px 0px 10px #000;
box-shadow: 0px 0px 10px #000;
/* For IE 8 */
/* For IE 5.5 - 7 */
behavior: url(http://localhost/PIE.php);
}
.inner
{
width: 100%;
padding: auto;
display: block;
}
</style>
</head>
<body>
<div class="keepaspect inner">
<img src=http://i42.tinypic.com/21e18cx.jpg width='100%' height='100%'>
</div>
</body>
</html>
我该如何设置它,使它对跨浏览器友好,并且在某种程度上,它总是填充外部 div?
它还应该与嵌入的 jwplayer 一起使用,正如您在这个示例中看到的那样,它还没有工作。但是 jwplayer 嵌入的标记用于测试/演示目的: http://jsfiddle.net/Kn2Ju/1/
我不确定,这是否需要两种不同的设置。
这是一个完整的示例,但它基于 img 标签,我不能使用它。内容不一定是img。 http://jsfiddle.net/gMUkE/2/
<!doctype html>
<html>
<head>
<title>Fixed Aspect Ratio</title>
<style>
#container
{
position: relative;
min-width: 300px;
max-width: 750px;
margin: auto;
}
#container img
{
width: 100%;
margin: auto;
position: relative;
display: block;
/* Box Shadow */
-moz-box-shadow: 0px 0px 10px #000;
-webkit-box-shadow: 0px 0px 10px #000;
box-shadow: 0px 0px 10px #000;
/* For IE 8 */
/* For IE 5.5 - 7 */
behavior: url(http://localhost/PIE.htc);
}
.content
{
width: 100%;
height: 100%; /*optional in case the poster image has exact aspect ratio*/
position: absolute;
z-index: 1;
}
</style>
</head>
<body>
<div id=container>
<img src=http://i42.tinypic.com/21e18cx.jpg>
</div>
</body>
</html>
【问题讨论】:
-
在 Firefox 中,您会看到“文本”
display: inline图像,因此您会看到底部的小间隙。将display: block添加到图像中,差距就消失了。见:jsfiddle.net/ywyQQ/3 -
摆脱
height: 100%!如果你想保持纵横比,这是毫无意义的,因为它指示 Safari 将图像的高度设置为页面的高度。 -
不知何故我错过了,删除
height:100%修复了 Safari 的行为(错误测试)。所以我在这里的最新小提琴实际上适用于 FF、IE、Safari、Chrome:jsfiddle.net/dfpGx 至少对于图像。现在我尝试让它与 jwplayer 一起工作。两个测试都在 jsFiddle 中。
标签: html css cross-browser responsive-design