开发者

Background images resize in both width and height upon browser window resize

How to resize a css background image in proportion when resizing the browser window?

#main {width:950px; height:100%; background:url(image.jpg) no-repeat center bottom}

Background images resize in both width and height upon browser window resize

When browser resize images should no to go under the text, images should shrink the size to keep the same position开发者_如何转开发 and distance


This is how i would do it.

http://jsfiddle.net/DpneL/1/

HTML:

<div id="imgCont">
    <img src="http://lorempixum.com/400/200/" alt="" />
</div>

CSS:

#imgCont {
    margin: 0px auto;
    min-width: 300px;
    max-width: 900px;    
    width: expression(document.body.clientWidth < 302? "300px" : document.body.clientWidth > 902? "900px" : "auto"); /* ie6 */
}

img {
    width: 100%;
}


Proportionally it is not going to be right as you are setting a specific width with a percentage height. So try setting a width and height in according percentages.

If you are using height 100% then it's always going to go under your text as well, so that would explain that as it'll go to 100% of the browser.

You may need to experiment with setting width and height as values such as 80% etc. experiment!


@jitendra; Yes you can use background-size property for this but instead of cover give value in 100% like this

CSS:

#image{
    -moz-background-size: 100% 50%;
     background:url("image.jpg") no-repeat center bottom;
}

may be that's work for you

read these article for more: http://webdesign.about.com/od/styleproperties/p/blspbgsize.htm , http://robertnyman.com/css3/background-size/background-size.html


You can use the css background-size. Have a look here: w3c background-size

You are probably after contain or cover.

Alternatively, here is an example with an img tag: http://jsfiddle.net/jwg2s/ Try resizing your browser.

HTML

<div> 
    Heading text Heading text<br/>
    Heading text Heading text<br/>
    <img src="http://secondnaturearomatics.com/images/pear1.jpg" border="1" alt=""/>
</div>

CSS

div {
    text-align: center;
    background: silver;
    width: 90%;
    font-size: 25px;
}
img {
    width: 50%;
}


I'd suggest using this jQuery plugin http://johnpatrickgiven.com/jquery/background-resize/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜