我正试图在任何屏幕尺寸下以不裁剪的方式显示尽可能大的图片。这意味着在横屏
模式下使用height: 100%; width: auto
,在竖屏
模式下使用width: 100%; height: auto
。
我提供的图片足够大,可以填满视网膜iPad,因此几乎每个屏幕尺寸都会缩小图片。 这在除了Internet Explorer和Firefox之外的所有浏览器和方向中都可以正常完成,在横屏模式下仍然会过度放大,超出几乎所有屏幕的范围。请注意,这仅适用于横屏模式。
相关的代码片段如下:
<style type="text/css">
#container {position:absolute; top:0; left: 0; right: 0; bottom:0; display: block;}
#content {
text-align: center;
margin: 0px;
font-size:0;
position: absolute;
top:0; left: 0; right: 0; bottom: 0
}
#content:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em;
}
.sponsor {
display: inline-block;
vertical-align: middle;
}
#content img {
max-width: 100%;
width: 100%;
height:auto;
}
@media all and (orientation: landscape) {
#main {
top:0;
display: block;
width: 100%;
height: 100%;
margin:0px auto;
text-align:center;
}
#content img {
height:100%;
width:auto;
max-width:auto !important;
max-height:100%;
display:block;
margin:0 auto;
}
}
</style>
<div id="content">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div title="Click to flip" class="sponsor">
<a href="#" class="img-link">
<img src="<?php echo $image[0]; ?>" alt="" class="feat-1" style="display: block;" />
</a>
<a href="#">
<img src="<?php echo kd_mfi_get_featured_image_url('featured-image-2', 'post', 'full'); ?>" alt="" class="feat-2" style="display: none;" />
</a>
</div><?php endif; ?>
</div><!-- End div #content -->
我不太关心IE9及以下的版本,但最好能够兼容所有浏览器。不过只要能够兼容IE9及以上版本和Firefox,我就会很满意。
哦,顺便说一下 - Firefox的检查器告诉我width:100%
规则是被遵循的,但显然并不是这样。
提前致谢!
<div class="sponsor">
设置宽度解决了问题。Chrome和Safari从祖先那里继承了宽度,但IE和Firefox没有,这真的让我很困惑。非常感谢! - suryanagadiv.class > a > img
,我有一些 CSS 代码基本上是这样写的.class img: { max-width: 100%;}
。我通过这样写来解决我的问题:.class a: { max-width: 100%;}
。 - raiderrobert