Responsive css background images

前端 未结 19 755
北荒
北荒 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:24

    This is an easy one =)

    body {
        background-image: url(http://domains.com/photo.jpeg);
        background-position: center center;
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-size: cover;
    }
    

    Take a look at the jsFiddle demo

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

    You can use this. I have tested and its working 100% correct:

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

    You can test your website with responsiveness at this Screen Size Simulator: http://www.infobyip.com/testwebsiteresolution.php

    Clear Your cache each time you make changes and i would prefer to use Firefox to test it.

    If you want to use an Image form other site/URL and not like: background-image:url('../images/bg.png'); //This structure is to use the image from your own hosted server. Then use like this: background-image: url(http://173.254.28.15/~brettedm/wp-content/uploads/Brett-Edmonds-Photography-14.jpg) ;

    Enjoy :)

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

    Here is sass mixin for responsive background image that I use. It works for any block element. Of course the same can work in plain CSS you will just have to calculate padding manually.

    @mixin responsive-bg-image($image-width, $image-height) {
      background-size: 100%;
      height: 0;
      padding-bottom: percentage($image-height / $image-width);
      display: block;
    }
    
    
    .my-element {
      background: url("images/my-image.png") no-repeat;
    
      // substitute for your image dimensions
      @include responsive-bg-image(204, 81);
    }
    

    Example http://jsfiddle.net/XbEdW/1/

    0 讨论(0)
  • 2020-11-22 06:28
        <style> 
             * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    
    #res_img {
      background: url("https://s15.postimg.org/ve2qzi01n/image_slider_1.jpg");
      width: 100%;
      height: 500px;
      background-repeat: no-repeat;
      background-size: 100% 100%;
      background-position: center;
      border: 1px solid red;
    }
    
    @media screen and (min-width:300px) and (max-width:500px) {
      #res_img {
        width: 100%;
        height: 200px;
      }
    }
    
        </style>
    
    <div id="res_img">
    </div>
    
    0 讨论(0)
  • 2020-11-22 06:29

    Try using background-size but using TWO ARGUMENTS One for the width and the other one for the height

    background-image:url('../images/bg.png'); 
    background-repeat:no-repeat;
    background-size: 100% 100%; // Here the first argument will be the width 
    // and the second will be the height.
    background-position:center;
    
    0 讨论(0)
  • 2020-11-22 06:31
    #container {
        background-image: url("../images/layout/bg.png");
        background-repeat: no-repeat;
        background-size: 100% 100%;
        height: 100vh;
        margin: 3px auto 0;
        position: relative;
    }
    
    0 讨论(0)
提交回复
热议问题