适应,缩放和居中图像以响应浏览器窗口(CSS) [英] Fit, scale and center image responsively to browser window (CSS)

查看:101
本文介绍了适应,缩放和居中图像以响应浏览器窗口(CSS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论肖像还是风景,我如何都具有图像:

How do I have an image, irrespective of portrait or landscape:


  • 水平和垂直响应居中(保持居中为浏览器窗口)调整大小)

  • 占据浏览器窗口的固定部分,进行响应缩放

  • 在调整浏览器窗口大小时保留宽高比

推荐答案

以下是仅使用基本CSS即可满足上述所有要求的解决方案(无需CSS3):

Here's a solution that satisfies all of the above with only basic CSS (no CSS3 required):

http://jsfiddle.net/dvidby0/sytj1uws/1 /

HTML

<div class="container">
<!-- this is a landscape image-->
<img src="http://upload.wikimedia.org/wikipedia/commons/1/15/Cat_August_2010-4.jpg"/>
<!--or use this portrait image; still works
<img src="http://upload.wikimedia.org/wikipedia/commons/9/9d/Britishblue.jpg"/>
-->

CSS strong>

.container {
width: 80%;
height: 80%;
position: absolute;    
margin:auto;
top:0;
bottom:0;
left:0;
right:0;
background-color: #aaa;
}

img {
max-width: 100%;
max-height: 100%;  
position: absolute;
margin:auto;
top:0;
bottom:0;
left:0;
right:0;
}

这篇关于适应,缩放和居中图像以响应浏览器窗口(CSS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆