Responsive css background images

前端 未结 19 791
北荒
北荒 2020-11-22 05:39

I have a website (g-floors.eu) and I want to make the background (in css I have defined a bg-image for the content) also responsive. Unfortunately I really don\'t have any i

相关标签:
19条回答
  • 2020-11-22 06:13

    I think, the best way to do it is this:

    body {
        font-family: Arial,Verdana,sans-serif;
        background:url("/images/image.jpg") no-repeat fixed bottom right transparent;
    }
    

    In this way there's no need to do nothing more and it's quite simple.

    At least, it works for me.

    I hope it helps.

    0 讨论(0)
  • 2020-11-22 06:15

    by this code your background image go center and fix it size whatever your div size change , good for small , big , normal sizes , best for all , i use it for my projects where my background size or div size can change

    background-repeat:no-repeat;
    -webkit-background-size:cover;
    -moz-background-size:cover;
    -o-background-size:cover;
    background-size:cover;
    background-position:center;
    
    0 讨论(0)
  • 2020-11-22 06:16

    Adaptive for square ratio with jQuery

    var Height = $(window).height();
    var Width = $(window).width();
    var HW = Width/Height;
    
    if(HW<1){
          $(".background").css("background-size","auto 100%");
        }
        else if(HW>1){
          $(".background").css("background-size","100% auto");
        }
    
    0 讨论(0)
  • 2020-11-22 06:21

    If you want the same image to scale based on the size of the browser window:

    background-image:url('../images/bg.png');
    background-repeat:no-repeat;
    background-size:contain;
    background-position:center;
    

    Do not set width, height, or margins.

    EDIT: The previous line about not setting width, height or margin refers to OP's original question about scaling with the window size. In other use cases, you may want to set width/height/margins if necessary.

    0 讨论(0)
  • 2020-11-22 06:21

    Here is the best way i got.

    #content   {
        background-image:url('smiley.gif');
        background-repeat:no-repeat;
        background-size:cover;
    }
    

    Check on the w3schools

    More Available options

    background-size: auto|length|cover|contain|initial|inherit;
    
    0 讨论(0)
  • 2020-11-22 06:23

    Try this :

    background-image: url(_images/bg.png);
    background-repeat: no-repeat;
    background-position: center center;
    background-attachment: fixed;
    background-size: cover;
    
    0 讨论(0)
提交回复
热议问题