Positioning image horizontally center and make height 100% of viewport

前端 未结 4 699
长发绾君心
长发绾君心 2021-01-03 02:53

I have a an image taking up the entire height of the viewport. The image height should span the entire viewport height (100%) so that it will fit to the screen it is viewed

4条回答
  •  孤城傲影
    2021-01-03 03:12

    Put the img in a div and set text-align: center. Make the div fixed, not the img. To stretch smaller images, use height: 100% instead of max-height.

    div.bg {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        text-align: center;
    }
    div.bg img {
        height: 100%;
    }​
    

    Working demo: http://jsfiddle.net/Mt7ce/

提交回复
热议问题