CSS能否使图片放大到窗口大小


加了max-width:100%能够使大图片缩小到窗口大小,那么如何设置能使小图片放大到窗口大小呢。还是必须得用js来做这个事?

前端 css JavaScript

JackAyu 10 years, 8 months ago

html {
width: 100%;
height: 100%;
background: url(bg.jpg) no-repeat center center fixed;
background-size: cover;
}


参考资料: http://css-tricks.com/perfect-full-page-background-image/

去尼玛暧昧 answered 10 years, 8 months ago

background-size: cover;

甜蜜的死亡 answered 10 years, 8 months ago

gunwlwl answered 10 years, 8 months ago

直接用 width: 100% 不行嗎?


 max-width: 100%;
min-width: 100%;

等於 width: 100%

即将堕落天使 answered 10 years, 8 months ago

可以作为其他元素的背景,然后设置background-size

糟糕滴全控 answered 10 years, 8 months ago

1.图片方式


 img{
    width: 100%;
    height: auto;
}

2.背景图+background-size
此处利用padding-top的百分比特性


 .img{
    padding-top: 图片高度/图片宽度 * 100%;
    background-size: 100% 100%;
}

甩奶罩的阿婆 answered 10 years, 8 months ago

Your Answer