Tiffany R. White Blog

Dark Mode
Light Mode

Background Image Hacks in CSS

August 04, 2017 • ☕️ 3 min read

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

![] cover

![contain] contain

![scale-down] scale-down


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:

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.

Share article

Discuss on TwitterEdit on GitHub

Tiffany R. White Blog

Tiffany White

Blog of Tiffany White. Thoughts on React & web development. My stuff: /uses.