• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Jackie D'Elia | UX Designer

  • Articles, Tips and Tutorials
  • About Me
  • About this site
  • Show Search
Hide Search
Home » Articles, Tips and Tutorials » Add Retina Ready Background Images using a Sass Mixin

Add Retina Ready Background Images using a Sass Mixin

Written by Jackie D'Elia  |  Published on February 17, 2016

This article has been archived.

Utility Pro is no longer under development. Some of the content may be relevant but there are better options for adding an SVG logo to a WordPress theme now.

My last post demonstrated how I added a Retina ready logo to the Utility Pro theme. If you have the professional version of Utility Pro, you can add a Sass mixin to generate the necessary code to add Retina ready background images. This can easily be adapted to any WordPress or Genesis theme that is using Sass.

Consider SVG Instead
The best approach for creating a logo that will look sharp and crisp on any device is to use an SVG (Scalable Vector Graphics) file, instead of using two versions of the same PNG (Portable Network Graphics) or JPG (aka JPEG – Joint Photographic Expert Group) file. I’ll cover that in the next post in this series.

For this post, I’ll cover how to serve Retina ready background images in Utility Pro with a Sass mixin when using JPG or PNG formats. They’ll always look sharp on HDPI (High Density Pixel and/or Retina displays). Read my previous post for an intro on that.

Utility Pro Developer Edition and Sass

This post assumes you have the Professional developer version of Utility Pro installed and are comfortable working in Sass.

Adding a Sass Mixin

A Sass mixin is an easy way to generate the CSS code. The mixin is stored in one place, so it is easy to maintain. You can call it from any other partial with @include and passing the values.

To replicate the example in my previous post using a Sass mixin, you will need to edit the _header.scss.

I replaced this code in my _header.scss partial.

/* Logo, hide text */
.header-image .site-title > a {
background: url(images/logo.png) no-repeat center;
float: left;
min-height: 60px;
width: 100%;
@include media($medium-screen-up) {
background: url(images/logo.png) no-repeat left;
}
}
view raw original_header-scss hosted with ❤ by GitHub

With this code:

/* Logo, hide text */
.header-image .site-title > a {
@include imgRetina(images/logo, png, 300px, 60px, center);
float: left;
min-height: 80px;
width: 100%;
@include media($medium-screen-up) {
@include imgRetina(images/logo, png, 300px, 60px, left);
}
}
view raw _header-scss hosted with ❤ by GitHub

The above code calls the ImgRetina() mixin. Here is the code I added to my _mixin.scss partial to create the imgRetina() mixin. I adapted it from some code I found on Snippet Central to use the code recommended by Chris Coyier at CSS-Tricks. I added some additional parameters to the mixin to make it more flexible and provide some defaults. The snippet below goes in your _mixin.scss partial.

If you have not already created a _mixin.scss partial for your Utility Pro theme, you’ll need to add one.
/* mixins
* Generates the image in either standard size or retina display version
*/
@mixin imgRetina($image, $extension, $width, $height, $position: center, $repeat: no-repeat) {
background: url($image + '.' + $extension) $repeat $position;
@media
screen and (-webkit-min-device-pixel-ratio: 2),
screen and ( min--moz-device-pixel-ratio: 2),
screen and ( -moz-min-device-pixel-ratio: 2),
screen and ( -o-min-device-pixel-ratio: 2/1),
screen and ( min-device-pixel-ratio: 2),
screen and ( min-resolution: 192dpi),
screen and ( min-resolution: 2dppx) {
background: url($image + '@2x' + '.' + $extension) $repeat $position;
background-size: $width $height;
}
}
view raw _mixin-scss hosted with ❤ by GitHub

Utility Pro developer edition does not include a mixin partial. If you add a _mixin.scss, be sure to include the _mixin.scss partial in your style.scss file. I added it just beneath the _variables.scss partial.

/* Resets
---------------------------------------------------------------------------------------------------- */
@import "partials/reset";
/* # Variables
---------------------------------------------------------------------------------------------------- */
@import "partials/variables";
/* Mixins
---------------------------------------------------------------------------------------------------- */
@import "partials/mixins";
view raw style-scss hosted with ❤ by GitHub

Breaking Down the Sass mixin

Let’s go over what this mixin does. It is passed the url ‘images/logo‘ without the file extension, the file extension, the width, the height, position and repeat values. The position and repeat have defaults of center and no-repeat if a value is not passed. Since Utility Pro is mobile first, we pass the logo with a position of center (for mobile displays). The mixin is called again within the media query with a position of left (for desktop displays).

Each time the mixin is called, it generates the background code along with an @media query checking if the device is a HDPI. If it is, it serves the @2x file.

Here is how the CSS is generated:

/* Logo, hide text */
.header-image .site-title > a {
background: url(images/logo.png) no-repeat center;
float: left;
min-height: 80px;
width: 100%;
}
@media screen and (-webkit-min-device-pixel-ratio: 2), screen and (min--moz-device-pixel-ratio: 2), screen and (-moz-min-device-pixel-ratio: 2), screen and (min-device-pixel-ratio: 2), screen and (min-resolution: 192dpi), screen and (min-resolution: 2dppx) {
.header-image .site-title > a {
background: url(images/logo@2x.png) no-repeat center;
background-size: 300px 60px;
}
}
@media screen and (min-width: 1023px) {
.header-image .site-title > a {
background: url(images/logo.png) no-repeat left;
}
}
@media screen and (min-width: 1023px) and (-webkit-min-device-pixel-ratio: 2), screen and (min-width: 1023px) and (min--moz-device-pixel-ratio: 2), screen and (min-width: 1023px) and (-moz-min-device-pixel-ratio: 2), screen and (min-width: 1023px) and (min-device-pixel-ratio: 2), screen and (min-width: 1023px) and (min-resolution: 192dpi), screen and (min-width: 1023px) and (min-resolution: 2dppx) {
.header-image .site-title > a {
background: url(images/logo@2x.png) no-repeat left;
background-size: 300px 60px;
}
}
view raw final-header-css hosted with ❤ by GitHub

Use for any background image – not just logos

I used this ImgRetina() mixin to display the background photo in the footer widget on the home page. It is the image with a circular border. I uploaded two versions of the image to my image folder: waterlily.jpg and waterlily@2x.jpg. I added a border-radius of 50% to make it circular. On a HDPI or Retina display, waterlily@2x.jpg is served.

.footer-widgets-1 .bg-image-1 {
border-radius: 50%;
@include imgRetina(images/waterlily, jpg, 200px, 200px, no-repeat, center);
height: 200px;
width: 200px;
margin: 10px auto;
}
view raw footer_widgets-scss hosted with ❤ by GitHub

Here is how the CSS is generated:

.footer-widgets-1 .bg-image-1 {
border-radius: 50%;
background: url(images/waterlily.jpg) center no-repeat;
height: 200px;
width: 200px;
margin: 10px auto;
}
@media screen and (-webkit-min-device-pixel-ratio: 2), screen and (min--moz-device-pixel-ratio: 2), screen and (-moz-min-device-pixel-ratio: 2), screen and (min-device-pixel-ratio: 2), screen and (min-resolution: 192dpi), screen and (min-resolution: 2dppx) {
.footer-widgets-1 .bg-image-1 {
background: url(images/waterlily@2x.jpg) center no-repeat;
background-size: 200px 200px;
}
}
view raw final-footer-1-css hosted with ❤ by GitHub
Screen Shot of circular background image in Footer widget

That’s a Wrap

As I mentioned above, using a SVG is the preferred way to add to a logo to your WordPress site and I’ll cover that in my next post. When using PNGs for graphics and JPGs photos for background images, like the waterlily image in the footer – the imgRetina() mixin is a good option.

Resources:

Handy Sass Basics Guide

Mixin code snippet adapted from Snippet Central

http://www.howtogeek.com/howto/30941/whats-the-difference-between-jpg-png-and-gif/

https://en.wikipedia.org/wiki/Scalable_Vector_Graphics
[shared_counts location="shared-count-icons" style="fancy"]
Previous or Next Article
Previous Post: Adding a SVG Logo to Utility Pro ThemeAdding a SVG Logo to Utility Pro Theme
Adding a Retina Ready Logo to the Utility Pro Theme Next Post: Adding a Retina Ready Logo to the Utility Pro Theme

Filed under: Developer Tips, Genesis Tutorials

sidebar

sidebar-alt

Connect

  • Twitter
  • Linkedin
  • Github
  • Dribbble

Copyright © 2014 - 2023 D'Elia Media LLC, All Rights Reserved. | Privacy Policy