Tiffany R. White Blog

Your friend Tiffany's tiny little weblog.

Background Image Hacks in CSS

2017-08-04 Tiffany WhiteWeb Development

Yesterday 1 I found myself in a situation where the background image in my freeCodeCamp Weather App on mobile was cut off at the bottom. Needed some background image hacks in css to fix it.

For context

My code for switching up the background depending on your weather conditions is as follows:

const iconMap = {
  "clear-day": "url(",
  "partly-cloudy-night": "url(",
  "clear-night": "url(",
  "partly-cloudy-day": "url(",
  "cloudy": "url(",
  "rain": "url(",
  "sleet": "url(",
  "snow": "url(",
  "wind": "url(",
  "fog": "url("

 (function() {
   $("body").css("background-image", iconMap[currentIcon]);

This bug had been bothering me for a week or so. I searched on Stack Overflow for an appropriate solution and found that I could use a div wrapper and add:

body {
   background-size: cover;
   background-repeat: no-repeat;

I added the wrapper but nothing happened.

I also already had my main content wrapped in a div:

<div class="wrapper">
  <div class="col-md-12">
      <h1 class="h1-title">Local Weather App </h1>

Object-fit, etc

You can usually object-fit on an image with the properties: fill, cover, contain, scale-down, or none:

fill fill

cover cover

contain contain

scaled down scale-down

none none

I tried object-fit: cover on the body. Didn’t work. Same with contain, scale-down, and fill.

I searched some more and found the answer on a Treehouse forum.

html {
   min-height: 100%;

I tried height: 100% on the body but that didn’t work either.

Since the html element is actually at the root of the document and body descends from the html element, applying the global style to the html element actually makes sense.

Also, because I am specifying a minimum height that happens to be a percentage, it makes it responsive because I am not specifying a fixed size in the viewport.

Now, I have a mobile app that looks like this:

iphone fcc

Still, I need to fix the loading issue. Thats for another day.

You can find the weather app at freeCodeCamp Weather App.

  1. And for several days before that actually.