Search code examples
cssresponsive-designbackground-image

Make background image responsive


I want to make background image responsive both in width as well as in height so that i can run it in mobile.

I have used this

.your_background_class_name {
    width: 100%;
    height:auto;
    top: 0px;
    left: 0px;
    z-index: -1;
    position: absolute;

}

but it works in width only not in height. What changes should i do to make it work?


Solution

  • html { 
      background: url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSvctt0I--skoKQVfuVt-i4Et6vQ5ve7lXPkLy9sTElCWLKh1Ps) no-repeat center center fixed; 
      -webkit-background-size: cover;
      -moz-background-size: cover;
      -o-background-size: cover;
      background-size: cover;
    }
    

    Know more about background-size here.

    See demo