📜 ⬆️ ⬇️

Making css sprites more responsive on retina-displays and not only [less]

image

Why do we need sprites at all?


I will write only briefly why this is necessary, since the advantages and disadvantages of css sprites have already been described on the habr.

Problem statement or so life does not seem to raspberry


Using css sprites with many elements raises the problem of creating css properties with background-position ; They need to write a lot, sometimes a lot. Of course, many sprite generation services help us - they also issue a css / less / sass file with coordinates along with the sprite. But almost always everything is rigidly tied up in pixels:

Using sprites prepared for 72ppi, on phones, tablets and new retina-laptops causes blurring of images, and looks ugly ...

Disclaimer on cross-browser compatibility, cross-platform and cross-something else
We will use css3 in this method only for displays with ppi, above the standard 72. Similar displays appeared relatively recently, and no IE 7.0-IE8.0 and the like old versions of Firefox or Opera will stand there. With the mobile segment, too, everything is in order, support for media queries is implemented everywhere .

Let's start forming a less-snippet


Immediately, I note that no one forces you to use less on a working project, everything can be compiled into regular css. A bunch of similar mac programs, WinLess will be useful for windows.
Now I will consider a special case of the sprite - a sprite with icons, generated by the wonderful resource icomoon.io

What would we do with a simple css, if you had to configure a sprite for retina? Most likely, we would have refused to use the sprite with high resolution. Perhaps you would connect it only for devices with high ppi.
But here we are faced with the problem of scaling this sprite to fit our size. And this, except for transform: scale(x); just not to solve, and you have to redraw the sprite, rewrite the whole background-position . And everything would be fine, but sometimes the number of icons exceeds the limit, and in general I would like a universal solution .
')

I have them


So let's get started:

Comments for use in practice


  1. You do not need to generate three different sprites at once through the generator program. You can generate one (for very-retina screens), get a precious css-file, and then through a graphics editor make a smaller version of the sprite for conventional devices (not retina).
  2. I don't use " px " in my projects. Instead, I set the size to a value in the units of " rem ". This gives the freedom to resize the icons to the required, and in case we open the site from the tablet, these same rem become a bit more, because I set a slightly larger font size for tablets, for example:
     @media (min-width: 768px) and (max-width: 1366px) { html {font-size: 120%;} } 


When to use this technique




PS: About spelling, syntax and lexical errors, please write in the LAN.
PPS: This material does not claim to be added to the chamber of measures and weights, and the examples used are not perfect. Guru-less / sass in this post will not find anything special, but for beginners it will be useful.

Source: https://habr.com/ru/post/196030/


All Articles