Responsive css background images

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

提交回复
热议问题