How to stretch the background image to fill a div

后端 未结 10 1139
慢半拍i
慢半拍i 2020-12-04 10:27

I want to set a background image to different divs, but my problems are:

  1. The size of image is fixed(60px).
  2. Varying div\'s size

How

相关标签:
10条回答
  • 2020-12-04 11:09

    For this you can use CSS3 background-size property. Write like this:

    #div2{
        background-image:url(http://s7.static.hootsuite.com/3-0-48/images/themes/classic/streams/message-gradient.png);
        -moz-background-size:100% 100%;
        -webkit-background-size:100% 100%;
        background-size:100% 100%;
        height:180px;
        width:200px;
        border: 1px solid red;
    }
    

    Check this: http://jsfiddle.net/qdzaw/1/

    0 讨论(0)
  • 2020-12-04 11:10

    by using property css:

    background-size: cover;
    
    0 讨论(0)
  • 2020-12-04 11:10

    Try something like this:

    div {
        background-image: url(../img/picture1.jpg);
        height: 30em;
        background-repeat: no-repeat;
        width: 100%;
        background-position: center;
    }
    
    0 讨论(0)
  • 2020-12-04 11:11

    To keep the aspect ratio, use background-size: 100% auto;

    div {
        background-image: url('image.jpg');
        background-size: 100% auto;
        width: 150px;
        height: 300px;
    }
    
    0 讨论(0)
  • 2020-12-04 11:12

    You can add:

    #div2{
        background-image:url(http://s7.static.hootsuite.com/3-0-48/images/themes/classic/streams/message-gradient.png);
        background-size: 100% 100%;
        height:180px;
        width:200px;
        border: 1px solid red;
    }
    

    You can read more about it here: css3 background-size

    0 讨论(0)
  • 2020-12-04 11:18

    You can use:

    background-size: cover;
    

    Or just use a big background image with:

    background: url('../images/teaser.jpg') no-repeat center #eee;
    
    0 讨论(0)
提交回复
热议问题