How to show full height background image?

后端 未结 4 1598
梦毁少年i
梦毁少年i 2020-12-25 11:19

I have a photo type (not landscape) background image with 979px width by 1200px height. I would like to set it to float left and show 100% full image height fixed, without t

相关标签:
4条回答
  • 2020-12-25 11:26
     html, body {
        height:100%;
    }
    
    body { 
        background: url(images/bg.jpg) no-repeat center center fixed; 
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
        background-size: cover;
    }
    
    0 讨论(0)
  • 2020-12-25 11:29

    CSS can do that with background-size: cover;

    But to be more detailed and support more browsers...

    Use aspect ratio like this:

     aspectRatio      = $bg.width() / $bg.height();
    

    FIDDLE

    0 讨论(0)
  • 2020-12-25 11:32

    You can do it with the code you have, you just need to ensure that html and body are set to 100% height.

    Demo: http://jsfiddle.net/kevinPHPkevin/a7eGN/

    html, body {
        height:100%;
    } 
    
    body {
        background-color: white;
        background-image: url('http://www.canvaz.com/portrait/charcoal-1.jpg');
        background-size: auto 100%;
        background-repeat: no-repeat;
        background-position: left top;
    }
    
    0 讨论(0)
  • 2020-12-25 11:43

    This worked for me (though it's for reactjs & tachyons used as inline CSS)

    <div className="pa2 cf vh-100-ns" style={{backgroundImage: `url(${a6})`}}> 
    ........
    </div>
    

    This takes in css as height: 100vh

    0 讨论(0)
提交回复
热议问题