Upgrading CSS sprite sheets for retina displays boosts image size by two-fold

I'm currently incorporating CSS sprite sheets into a mobile-friendly page design. The layout appears as intended, and below is the CSS I have implemented (which displays correctly on Chrome Canary's iPhone 3GS emulator):

div#logo {
    background: url('/static/images/iphone/iphone_landing_sprites_small.png') -114px 0px no-repeat;
    display: inline-block;
    width: 90px;
    height: 44px;
    display:block;
    float:right;
    margin:10px;
}

div#stars {
    background: url('/static/images/iphone/iphone_landing_sprites_small.png') -114px -46px no-repeat;
    display: inline-block;
    width: 71px;
    height: 12px;
}

div#iPhone {
    background: url('/static/images/iphone/iphone_landing_sprites_small.png') 0px 0px no-repeat;
    display: inline-block;
    float:left;
    width: 112px;
    height: 238px;
    margin-top: 54px;
}

div#separator {
    background: url('/static/images/iphone/iphone_landing_sprites_small.png') -114px -60px no-repeat;
    display: inline-block;
    width: 1px;
    height: 53px;
}

Upon viewing this on a Retina display, the images appear slightly pixelated. To address this, I followed guidance from this resource on switching sprite sheets for varying display densities and incorporated the following:

@media only screen and (-webkit-min-device-pixel-ratio: 1.5),
    only screen and (min--moz-device-pixel-ratio: 1.5),
    only screen and (min-resolution: 240dppx) {

    div#logo {
        background-image: url('/static/images/iphone/iphone_landing_sprites.png');
        background-position: -226px 0px;
    }

    div#stars {
        background-image: url('/static/images/iphone/iphone_landing_sprites.png');
        background-position: -226px -91px;
    }

    div#iPhone {
        background-image: url('/static/images/iphone/iphone_landing_sprites.png');
        background-position: 0px 0px;
    }

    div#separator {
        background-image: url('/static/images/iphone/iphone_landing_sprites.png');
        background-position: -226px -117px;
    }
}

The page continues to display properly on the iPhone 3GS simulator due to the @media switch having no impact. However, observing it in an iPhone 4 simulator shows the images appearing twice their original size. Although the layout remains consistent in terms of frame size, the images get cropped to reveal only the top-left quadrant.

Am I overlooking any additional attributes that need specification to compress the images within double-density pixels while maintaining equal screen dimensions?

Answer №1

After exploring some options, I stumbled upon a resolution: rather than specifying new offsets for each element individually like this:

background-position: -226px 0px;

All we need to do is make believe that the image maintains its original dimensions. This can be achieved by setting the background-size attribute equal to the size of the smaller sprite sheet:

background-size: 204px 238px;

Surprisingly, this approach seems to do the trick!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Bullet points may not appear when printing in IE8/9 unless you select the option to print background colors and images

I created a jsfiddle on http://jsfiddle.net/hxsy6/ The problem I'm facing is that when printing the page in IE7, the bullet points are printed with the actual bullet image. However, in IE8/9, they do not print. If I go to Print Preview -> click th ...

Adjusting containers for optimal display during browser zoom and resize operations

Currently, I have designed a banner using a triangle and rectangle to overlay an image. However, when the browser is zoomed in, there seems to be a gap between these two shapes. I am looking for suggestions on how to better connect or approach this banner ...

Issue with Annotations in MKMapView on iOS

My goal is to have a button that, when pressed, will fetch the user's location and drop an annotation on it. However, I'm encountering a strange issue. When I press the button for the first time, it adds the annotation in the middle of the ocean ...

CSS Background Image Properties

I'm struggling to display a background image on my HTML page. The image is saved in the same folder as the HTML file. <html> <head> <link rel="stylesheet" type="text/css" href="style/indexstyle.css" > <meta charset="ISO-8859-1"& ...

picker elementClass()

I encountered an issue while using the datepicker from Material UI. The datepicker has a method called dateClass: dateClass() { return (date: Date): MatCalendarCellCssClasses => { const unvailableArray = this.shareDate.unavailableDates; ...

Adjust background dimensions using jQuery

Having trouble adjusting background-size using jQuery? It seems to work with a single parameter, but not with two. I'm trying to set both width and height via jQuery variables. Here's an example of my JavaScript code: var w = 100; var h = 100; ...

Issue with auto-sliding feature on Bootstrap carousel

Wishing you a wonderful day! I'm encountering an issue where only the first slide appears for a few seconds before disappearing completely. Strangely, if I use the side arrows or buttons before the slide disappears, it functions normally. HTML <he ...

What is the process for building a grid system similar to Bootstrap's 12-column grid using the new CSS Grid Layout?

Is there a way to implement a 12-column grid system similar to Bootstrap using CSS Grid? I am struggling to understand this new technology and would greatly appreciate it if someone could provide a demo. My goal is to create a simple grid structure resem ...

Creating Consistent Height and Alignment for Bootstrap 4 Cards

Aligning Bootstrap 4 cards using d-flex and align-self-stretch for the cards works great. But I'm struggling to make the part with the red border float to the bottom. Any suggestions using only Bootstrap 4 utilities? https://i.sstatic.net/2VUME.png ...

Position the cells in the middle of the bottom row within the table

Indeed, this layout is based on tables. It's a legacy design, so removing the table structure may not be feasible. Nevertheless, I am curious to learn about table alignment, hence posing this somewhat unconventional question. Imagine a 3x3 table with ...

Select elements that do not have a specific parent class using LESS

I have created an HTML structure as outlined below: <ul class="list list--sm"> <li class="list__item list__item--draggable"> List item 1 <ul> <li class="list__item list__item ...

Update the value of an NSNumber every 5 seconds in Cocos2d

I'm attempting to increment a NSNumber every 5 seconds by 1 within Cocos2d, but for some reason, it doesn't seem to be working as expected. I believe there may be an evident mistake in my code. Currently, it only adds +1 once instead of doing so ...

Effective methods to prevent the `translate` transform from triggering an element to be perceived as position relative?

I am encountering an issue with an absolutely positioned div that is nested within a statically-positioned parent container. I noticed that when I apply the transform: translateY(-50%) property to the container, the child element behaves as if it were bein ...

Eliminating data from selection options on a HTML menu list

Currently, I am facing an issue while attempting to incorporate a menu bar into my HTML page. After adding the icons, they are displaying with both underlines and dots at the back. I have tried using text-decoration=none; to remove the underline, but unf ...

Adjust text size automatically to fit into a container with a fixed size

Looking to dynamically adjust the font size of user-entered text to fit within a fixed-size div. The goal is to have the text fill the box as much as possible. For example, if the div is 400px x 300px and the user enters "ABC", the font will be very large ...

Attempting to send out an iBeacon signal through React Native

Seeking to develop a basic app with React Native that can broadcast an iBeacon, I stumbled upon a potential third-party module named react-native-ibeacon-simulator. However, even a simple test doesn't seem to work. Despite its lack of recent updates, ...

floating point for a list item compared to other elements

nav > ul > li { float: left; } #one { float: left; } <nav> <ul> <li> he has a cow </li> <li > he has a dog </li> <li> he has a mouse </li> </ul> & ...

Open the Facebook app on iOS if it is installed, otherwise, launch the browser

I am currently working on implementing user authorization via Facebook. At the moment, I have successfully created a custom login button using the following code snippet: [FBSession openActiveSessionWithReadPermissions:permissions ...

The Issue with Non-Functional Links in Nivo Slider

Currently facing an issue with the Nivo Slider as it no longer links to any of the photos in the slider. Initially, I had 14 pictures linked to a post with a full embedded gallery inside. The problem arose when using a "fixed" size for the gallery, which d ...

What options exist for dynamically altering HTML/DOM within an iOS app using programming techniques?

I am currently working on processing various RSS feeds that contain summaries and URLs to target pages. I want to find a uniform transformation method for this task. Initially, XSLT seemed like the perfect solution as it can achieve what I need in a stand ...