How to stretch the background image to fill a div

后端 未结 10 1140
慢半拍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:22

    Use: background-size: 100% 100%; To make background image to fit the div size.

    0 讨论(0)
  • 2020-12-04 11:23
    body{
      margin:0;
      background:url('image.png') no-repeat 50% 50% fixed;
      background-size: cover;
    }
    
    0 讨论(0)
  • 2020-12-04 11:24

    Modern CSS3 (recommended for the future & probably the best solution)

    .selector{
       background-size: cover;
       /* stretches background WITHOUT deformation so it would fill the background space,
          it may crop the image if the image's dimensions are in different ratio,
          than the element dimensions. */
    }
    

    Max. stretch without crop nor deformation (may not fill the background): background-size: contain;
    Force absolute stretch (may cause deformation, but no crop): background-size: 100% 100%;

    "Old" CSS "always working" way

    Absolute positioning image as a first child of the (relative positioned) parent and stretching it to the parent size.

    HTML

    <div class="selector">
       <img src="path.extension" alt="alt text">
       <!-- some other content -->
    </div>
    

    Equivalent of CSS3 background-size: cover; :

    To achieve this dynamically, you would have to use the opposite of contain method alternative (see below) and if you need to center the cropped image, you would need a JavaScript to do that dynamically - e.g. using jQuery:

    $('.selector img').each(function(){ 
       $(this).css({ 
          "left": "50%", 
          "margin-left": "-"+( $(this).width()/2 )+"px", 
          "top": "50%", 
          "margin-top": "-"+( $(this).height()/2 )+"px" 
       }); 
    });
    

    Practical example:
    css crop like example

    Equivalent of CSS3 background-size: contain; :

    This one can be a bit tricky - the dimension of your background that would overflow the parent will have CSS set to 100% the other one to auto. Practical example: css stretching background as image

    .selector img{
       position: absolute; top:0; left: 0;
       width: 100%;
       height: auto;
       /* -- OR -- */
       /* width: auto; 
          height: 100%; */
    }
    

    Equivalent of CSS3 background-size: 100% 100%; :

    .selector img{
       position: absolute; top:0; left: 0;
       width: 100%;
       height: 100%;
    }
    

    PS: To do the equivalents of cover/contain in the "old" way completely dynamically (so you will not have to care about overflows/ratios) you would have to use javascript to detect the ratios for you and set the dimensions as described...

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

    Add

    background-size:100% 100%;
    

    to your css underneath background-image.

    You can also specify exact dimensions, i.e.:

    background-size: 30px 40px;
    

    Here: JSFiddle

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