Weekly Recap

Hey everyone, I already told you about the new Editor by GitHub: Atom. It’s an invite only beta at the moment, but I got two more invites to give away. It’s currently Mac only, keep that in mind. So if you want one of the invites, just write a comment. First come, first serve. And now: Happy reading.

  • CSS Shake
    Not that I think this is very useful, but it’s definitely funny. (via)

  • New UI in Firefox Aurora
    Firefox Aurora came out in Version 29 and it sports a new UI, which I really like. (Aurora is even more beta than Firefox beta, but you can get an idea what is coming down the pipe.)

  • CodeKit 2 Release
    CodeKit 2 is here. If you are not a fan of the command line and thus don’t use things like Grunt.js, this is THE app for you.

  • MAMP 3
    And another update to a very useful and already great, if not very good looking, app. Haven’t tried the new version, but it sure looks like it’s a worthy update.

  • Just Do One Thing
    Good advice. Make small steps. Achieve small goals. Feel good and keep going. It’ll add up.

  • Improving the CSS performance of fixed position elements
    Handy trick to stop fixed positioned elements from repainting on scroll.

  • Ridiculously Responsive Social Sharing Buttons
    Social Sharing Buttons are always a pain to implement responsively and responsibly. rrssb is here to help.

Weekly Recap

Hey there, it’s time for some awesome web development links. This week is very heavy on CSS and performance. Enjoy!

Avoid loading unnecessary CSS background images

While working on a responsive website the question came up whether CSS background images are being loaded by the browser if the element they are applied to is set to display: none;

My initial reaction was that I thought they will be loaded even though they aren’t rendered by the browser. After creating a test case on CodePen (see the code below), I was proven right – sadly. All CSS background images will be loaded by the browser no matter if they are rendered on the page or not.

BUT this isn’t true for Firefox, which is much smarter and always only loads the images that are actually rendered.

<div class="box box--s"></div>

<div class="box box--m"></div>

<div class="box box--l"></div>
.box {
  margin: 0 auto 10px;
  width: 100px;
  height: 100px;
}

.box--s {
  background: url(s.gif);
  
  @media (min-width: 499px) {
    display: none;
  }
}

.box--m {
  display: none;
  background: url(m.gif);
  
  @media (min-width: 500px) and (max-width: 799px) {
    display: block;
  }
}

.box--l {
  display: none;
  background: url(l.gif);

  @media (min-width: 800px) {
    display: block;
  }
}

Test it yourself with this CodePen.
Let’s have a look at what we can do to avoid loading unnecessary CSS background-images.

Instead of creating three different elements and using display: none; to hide them you can just change the background image according to media queries. This will only load the image that is actually displayed.

<div class="box box--s-m-l"></div>
.box {
  margin: 0 auto 10px;
  width: 100px;
  height: 100px;
}

.box--s-m-l {
  margin-bottom: 40px;
  background: url(s.gif);
  
  @media (min-width: 500px) {
    background: url(m.gif);
  }
  
  @media (min-width: 800px) {
    background: url(l.gif);
  }
}

CodePen Testcase.

In this special case this will work, but let’s say you have three boxes with different content and different background images and want to show either one according to viewport size, just using display: none; won’t do the trick. That’s sad, but it’s the way it is, at least for now.

What you can do is set all boxes to display: none; and only when the media query applies show the box and set the background image. This way you only load the image you are actually showing and you can have different boxes.

<div class="box box--s"></div>

<div class="box box--m"></div>

<div class="box box--l"></div>
.box {
  display: none;
  margin: 0 auto 10px;
  width: 100px;
  height: 100px;
}

.box--s {
  @media (max-width: 499px) {
    display: block;
    background: url(s.gif);
  }
}

.box--m {  
  @media (min-width: 500px) and (max-width: 799px) {
    display: block;
    background: url(m.gif);
  }
}

.box--l {
  @media (min-width: 800px) {
    display: block;
    background: url(l.gif);
  }
}

Try it with this CodePen.