Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Mark DeArmond
Mark DeArmond
15,233 Points

Responsive background-image

I'm trying to get this background image to be responsive just like a regular img with the max-width: 100%; property.

<body>
    <div></div>
    <div></div>
</body>

I have two divs in the html. Nothing special.

body {
  width: 80%;
  height: 100%;
}

div {
  width: 40%;
  padding-top: 100%;

  background-image: url(http://placehold.it/500x500);
  background-repeat: no-repeat;
  background-position: center center;

  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;

  margin: 2.5%;

  float: left;
}

This is my CSS.

Any help would be much appreciated.

2 Answers

jason chan
jason chan
31,008 Points
background-size: 100%;
Mark DeArmond
Mark DeArmond
15,233 Points

Hey Jason! Thanks for your response. I've tried that and the contain value. It still takes up over 100% of the height of the page. The reason I have the padding-top property is because I found on the internet that if you height / width * 100 you use that as the aspect ration percentage. In this particular case it 100% since 500 / 500 is one blah blah math. Without that padding-top property, the image will not even show up. I could put 500px by 500px as my width and height properties but I'm really trying to stick with the responsive aspect of things. Thanks again! :)

Jeff Lemay
Jeff Lemay
14,266 Points

You can change padding-top:100% to height:100vh to achieve the results you want, although support for 'Viewport-Height' is probably limited to newer browsers.

You need an element with an actual height or one that contains content in order to use height:100% on an element (will cover 100% of its parent, as long as the parent is the < body > tag).

Mark DeArmond
Mark DeArmond
15,233 Points

Well, after about 30 mins of tinkering, I've finally found a solution. It may not be the end-all be-all but it works. If anyone else has suggestions please feel free to share.

Basically, I had to create an outer dive and an inner div. Then I set the outer div's max-width and max-height to 500px;

 <body>
    <div class="outer">
      <div class="inner"></div>
    </div>
    <div class="outer">
      <div class="inner"></div>
    </div>
  </body>
.outer {
  width: 40%;
  max-width: 500px;
  max-height: 500px;
  margin: 2.5%;
  float: left;
}

.inner {
  width: 100%;
  padding-top: 100%;

  background-image: url(http://placehold.it/500x500);
  background-repeat: no-repeat;
  background-position: center center;

  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}