Tech Note: Replacing Images

Charles Johnsonfollow me on twitter
Science • Wed Dec 31, 2008 at 5:36 pm PST • Views: 157

We’re improving the infrastructure around here again. One visible change is the disappearance of the little blue word balloon icon (), that showed up next to the number of comments at the bottom right of each front page article; it was redundant, because clicking the number of comments went to the same place, so the icon got the axe.

Bye, little blue word balloon. We’ll miss you.

Another change is that the remaining icons in that row now have “rollover” states, meaning that when your mouse cursor passes over them they change color subtly.

One major change you can’t see is the way that row of icons is constructed, but it’s kind of interesting from a techie standpoint, so we’ll explain it and those of you not interested in the nuts and bolts can skip ahead to what will inevitably become an open thread.

Previously, those icons were simple PNG images. The new structure uses an unordered list, combined with the Phark image replacement technique to display the icons as background images instead of <img> tags. This method is more accessible to screen readers, and also lets us achieve that rollover effect through pure CSS, without any Javascript.

Here’s a simplified version of what the list looks like in HTML:

The CSS code removes the bullet style from the unordered list, and floats the list to the right. The list items are all floated left, so they appear horizontally instead of the default vertical arrangement. A 4-pixel margin is added to create some space between the icons.

The links themselves are set to a fixed width and height, their display type is set to ‘block’, then the text inside them is moved off screen by setting the text-indent property to negative 10000 pixels. The background-repeat and background-position properties for all the links are set.

Then we can set the background images for the links, like so:

From an accessibility standpoint, this is an improvement; screen readers don’t pay attention to CSS, so they will see (and read) the text inside the links, while standard browsers will show the background image and no text.

The rollover effects are achieved with a nifty trick that uses only one image. Here’s the complete PNG file for the email icon, to show how it’s done:

email

Notice that both image states are combined into one file. Since the width of the links was explicitly defined as 16 pixels, we’re only going to see 16 pixels of the image at a time when it’s used as the background. And we can change to the rollover state simply by changing the background-position of the image, like so:

This instantly slides the image 20 pixels to the left (because the second image in our combined file begins 20 pixels from the left), so the rollover image now shows in that little 16 pixel area.

Image rollovers and increased accessibility, without a single line of Javascript — but wait, there’s more! The other big advantage of this technique: by using background images for this repeating element (the row of icons appears at the bottom of every front page post) instead of image files, there are far fewer calls to the server to display the page.

We’re now in the planning stages for converting the comment icons (that appear at the top right of each comment) to a similar system, which could result in a big improvement in the loading time of pages with lots of comments. And we know you’re as excited about that as Stinky and I am.

Advertisement

254 comments

^ back to top ^

Name:

Pass:

Register Forgot Your Password? Account Settings Re-send Confirmation (To log in, cookies must be enabled in your browser!)

Turn off ads by subscribing!
For about 33 cents a day, our subscription option turns off all advertisements at LGF!
Read more...


► LGF Headlines

  • Loading...

► Tweeted Articles

  • Loading...

► Tweeted Pages

  • Loading...

► Top 10 Comments

  • Loading...

► Bottom Comments

  • Loading...

► Recent Comments

  • Loading...

► Tools/Info

► LGF Hits

► Resources

► Never Forget

► Statistics

► Tag Cloud

► Contact

You must have Javascript enabled to use the contact form.
Your email:

Subject:

Message:


Messages may be published in our weblog, unless you request otherwise.
Tech Note:
Using the Contact Form

More Partners

Compare Electricity Prices in your area. Texas Electricity is deregulated; you have the right to choose Texas Electric Rates from among many Texas Electric Companies.

The socks will be revealed.

TwitterFacebook
LGF Pages
Recent Pages

Ms. MacIceweasel
Half of Conservatives Are Now Against Breast Cancer Screenings
29 minutes ago
Views: 12 • Comments: 0
Tweets: 0 • Rating: 0

Randall Gross
Trial opens 2 years after feds break up Christian militia
1 hour ago
Views: 43 • Comments: 0
Tweets: 0 • Rating: 0

Randall Gross
Obama's budget headed to Congress
1 hour, 15 minutes ago
Views: 55 • Comments: 0
Tweets: 0 • Rating: 0

researchok
Dreyfus and the Birth of Intellectual Protest
4 hours, 4 minutes ago
Views: 56 • Comments: 0
Tweets: 0 • Rating: 1

researchok
The Man Who Wouldn't Die: New York's Most Fantastic Murder
4 hours, 7 minutes ago
Views: 80 • Comments: 0
Tweets: 0 • Rating: 3

researchok
Mitt's Spin
4 hours, 9 minutes ago
Views: 54 • Comments: 0
Tweets: 0 • Rating: 0

researchok
Strategy
4 hours, 9 minutes ago
Views: 49 • Comments: 0
Tweets: 0 • Rating: 0

researchok
Gender Wage Gap Skewed by Survey Flaws - Miller-McCune
4 hours, 10 minutes ago
Views: 56 • Comments: 0
Tweets: 0 • Rating: 1

researchok
Why Obama Would Be Glad if the Culture War Is a Major Election Issue
4 hours, 11 minutes ago
Views: 75 • Comments: 0
Tweets: 0 • Rating: 1

researchok
Is This the End of Wall Street as They Knew It?
4 hours, 11 minutes ago
Views: 62 • Comments: 0
Tweets: 0 • Rating: 1

 Frank says:

Anything played wrong twice in a row is the beginning of an arrangement.