• Stars
    star
    162
  • Rank 232,284 (Top 5 %)
  • Language
    CSS
  • Created almost 12 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Allow to use sprites in retina with Compass.

Retina Sprites for Compass

Allows you to use sprites in Compass with added retina variants. Works just like the normal sprite helpers, but has been made a lot easier with these mixins.

Take a look at the example to see the results.

The problem:

You want your site to serve retina images to retina displays, but not to non-retina ones. This mixin / solution will make it very easy for you to use sprites and let Compass generate the mappings of the two different sizes.

Since we have to develop sites that serve retina images and background-images our sprites and background-image approaches might get a bit cluttered in our CSS. Using just one call, this mixin will know which image has to be served to the browser and will also resize it to show as if it was in your normal image dimentions.

How to use:

First thing you'll need to do is to download the mixins and put them where your scss / sass files are located. Notice the underscore at the start of the filenames, this will prevent Compass from compiling these files to CSS.

  1. Include the mixin in your SASS/SCSS file using:
    @import "retina-sprites";

  2. Create two folders in your images folder of your Compass project. By default there are icons and icons-2x.

  3. Save your sprite images in the folders. The pixel-ratio 1 variant in ./icons and the retina variant in ./icons-2x. Make sure there have the same filename.

  4. Use the sprite in your SASS/SCSS using: @include use-sprite(<sprite-name>, <scale>). (Note the missing .png, this is not needed.)

It's really that easy!

Or with Bower

bower install Retina-sprites-for-Compass

What it does:

Compass will create a nice sprite image from the images you put in the folders. Make sure you only use PNG files for the best result.

Using just the name of the file Compass will know where the image is located in the huge sprite image it will generate. The mixin will also get the retina variant if the browser is running in a pixel-ratio 2 environment.

This is the generated CSS from the example:

SCSS:

.sprite2 {
    @include use-sprite("sprite2");
}

CSS:

.sprite2 {
	background-image: url('../images/icons-s44ec97e90e.png');
	background-position: 0 -25px;
	background-repeat: no-repeat;
	overflow: hidden;
	display: block;
	height: 25px;
	width: 25px;
}
@media (-webkit-min-device-pixel-ratio: 2), (-o-min-device-pixel-ratio: 3 / 2), (min--moz-device-pixel-ratio: 2), (min-device-pixel-ratio: 2), (min-resolution: 144dppx) {
	.sprite2 {
		background-image: url('../images/icons-2x-s93dce01c9d.png');
		background-position: 0 -25px;
		background-size: 45px 95px;
		height: 25px;
		width: 25px;
	}
}

You can also scale image:

SCSS:

.sprite2 {
    @include use-sprite("sprite3", 1.5);
}

Automatic stylesheet generator

You may include whole icons set from the folder automatically so when you add new icons to the icons source folder and run styles build it will generate corresponding styles automatically.
For this purpose you may use _sprite-icons-enum.scss, that contains @each function.

// style.scss

// you may set custom path for icons folder
$sprite-icons-folder: 'images/icons/my-icons/*.png';
$sprite-icons-2x-folder: 'images/icons/my-icons/*.png';

@import 'Retina-Sprites-for-Compass/src/retina-sprites';
// import _sprite-icons-enum.scss file after _retina-sprites.scss
// so it will generate stylesheet with whole set of icons

// set icon prefix (icon_ by default)
$sprite-icons-class-prefix: 'my-icon_';
@import 'Retina-Sprites-for-Compass/src/sprite-icons-enum';

Bonus: retina background-images

With the same principles of the sprites I created a mixin that sets the background-image and it's retina version.

How to use:

  1. Include the mixin in your SASS/SCSS file using:
    @import "retina-background-image";

  2. Put a pixel-ratio 1 version and a retina version anywhere in your images folder.

  3. Apply the style using: @include background-retina(<filename.png>, <filename-retina.png>);

The following code will generate:

SCSS:

.background {
	@include background-retina("background.gif", "background-2x.gif");
}

CSS:

.background {
	background-image: url('../images/background.gif');
}
@media (-webkit-min-device-pixel-ratio: 2), (-o-min-device-pixel-ratio: 3 / 2), (min--moz-device-pixel-ratio: 2), (min-device-pixel-ratio: 2), (min-resolution: 144dppx) {
	.background {
		background-image: url('../images/background-2x.gif');
		background-size: 25px 25px;
	}
}

More Repositories

1

queryloader2

Version of the QueryLoader by Gaya Kessler. Preload images with ease.
JavaScript
721
star
2

Fullscreen-Background-jQuery-plugin

Fullscreen background is a small jQuery plugin that allows you to create fullscreen background.
JavaScript
83
star
3

functional-snake

Snake clone written in JavaScript using functional programming
JavaScript
44
star
4

jQuery--Keep-the-Rhythm

Keep a nice vertical rhythm with elements that don't have one.
JavaScript
19
star
5

gaya.pizza

Public repository of Gaya Kessler's blog
SCSS
10
star
6

redux-listeners

Redux middleware which allows listening in on and handling of dispatched actions
JavaScript
10
star
7

MarkPress

Maintain a Markdown journal in this WordPress takeover App
Ruby
8
star
8

Locale-Storager

A little script that helps in managing the localeStorage of the browser.
6
star
9

svg-inliner

Creates inline HTML <svg> from an external SVG (sprite) in an <img /> tag
JavaScript
5
star
10

testing-javascript

Testing JavaScript using BrowserSync, Mocha and PhantomJS
JavaScript
4
star
11

Compass-Boilerplate

Boilerplate folders and files to start using Sass and Compass
Ruby
4
star
12

create-my-run

Generate your next run
TypeScript
4
star
13

mini-js-starter

Very minimal starter to kickstart JavaScript projects. No frameworks, plain npm tasks.
JavaScript
3
star
14

opensupport

Discover the top maintainers of the dependencies you use
JavaScript
3
star
15

rxjs-games

A collection of stupid RxJs powered games to learn more about the library
TypeScript
2
star
16

frankensoil

C++
2
star
17

Socketmon

A Websocket monitoring and proxy server
TypeScript
2
star
18

gulp-timber-example

An example how you can use Gulp + Twig + Timber data.
JavaScript
2
star
19

fermenter

TypeScript
1
star
20

react-to-redux-demo

Demo demonstrating how to go from basic react to redux
HTML
1
star
21

dekode-react-class

JavaScript
1
star
22

resume

Resume (CV) containing work experience, education and skills.
1
star
23

multipong

MULTI PONG
TypeScript
1
star
24

programming-challenges

Programming challenges we like to do
JavaScript
1
star
25

Formable-for-WordPress

Integrate Formable into WordPress
1
star
26

fetch-with-react-hooks

Example code of how you can use React to resolve data async with hooks
JavaScript
1
star
27

remote-games

TypeScript
1
star
28

mockup-data-middleware-with-connect

Example project how to serve static mockup data to your front-end using Connect and Grunt
JavaScript
1
star
29

jQuery-helper-functions

A collection of functions not included in the default jQuery package, but oh so handy!
JavaScript
1
star