8

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?

insanity
  • 1,148
  • 6
  • 16
  • 29
  • 2
    You have to use `background-size` property. Check this link http://css-tricks.com/perfect-full-page-background-image/ – James Mar 21 '14 at 10:32

3 Answers3

26
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

newTag
  • 2,149
  • 1
  • 22
  • 31
0

The following code should do the trick:

class_name {
    background: url(image);
    background-size: 100% auto contain;
}
skuntsel
  • 11,624
  • 11
  • 44
  • 67
Mykolaus
  • 9
  • 1
  • 1
  • this seems to not to be allowed as background-size expects a single value: contain, cover, inherit or initial – Mattijs Dec 06 '15 at 06:31
-1

This worked for me:

#header {
background-image: url('http://www.fillmurray.com/g/1920/1080');
background-repeat:no-repeat;
background-size: 100%;
background-position:top left;
position: relative;
}

#header .container {
height: 893px;
position: relative;
}

#header .div1 {
position: absolute;
top: 20px;
}

#header .div2 {
position: absolute;
bottom: 20px;
right: 20px;
}
<div id="header">
<div class="container">
    <div class="div1">
        Top left content
    </div>
    <div class="div2">
        Bottom right content
    </div>
</div>
trinaldi
  • 2,872
  • 2
  • 32
  • 37
AlphaX
  • 615
  • 1
  • 11
  • 25