Internet Explorer and PNG's

It's the bain of every Web Developers life. The one thing that would make all our lives just that little bit nicer. Proper support for PNG's in all common web browsers.

I'd kind of gotten over the fact that Internet Explorer doesn't, and probably never will, support alpha transparent PNG files. I guess it's because Microsoft don't see the need for them on their site.

What has been bugging me is the fact that PNG images always seemed to display slightly too dark in IE. Very irritating if you have an image that needs to match a colour specified in your CSS file.

So I decided last night to try and find out why this happens, and it turns out the Internet Explorer doesn't handle the gAMA chunk correctly, and the solution to this is to use pngcrush to remove the gAMA chunk and the cHRM chunk and put in a sRGB chunk.

It's all a bit technical but, rest assured, the command below will correct your PNG files so they display at the correct brightness in Internet Explorer.

# pngcrush -srgb 1 -rem gama -rem chrm [infile].png [outfile].png

At some point in the near future i'll write a bash script or something to automate fixing directories of files as it's a bit of a pain to do one file at a time.

Style Sheet Switcher

We all know that the idea behind XHTML and CSS is to seperate content from style, and when this is done properly it allows us to change the design of an entire site simply by changing the CSS stylesheet. So why not have multiple stylesheets for your website and allow the user to change them?

What follows is a step by step guide to exactly how I achieved the stylesheet switcher on ClockRocket.

Preparations

For this to work properly all elements relating to the 'design' of the page must be contained in the CSS. I won't go into any details about that here, but there are plenty of articles on the web about image replacement techniques. On such article can be found at Stopdesign; 'Using Background-Image to Replace Text'.

The Switcher Code

The first thing we will do is write some code that picks up a GET variable (which we will call 'style') and set a cookie for our dynamic stylesheet to pick up. We will put this code into an empty file and call it 'setstyle.php'.

<?php
    if ($_GET['style']) {
        setcookie("style", "$style", time() + 315360000, "/");
    }
?>

The 'style' variable will equal the name of the CSS file we want to change to, without the .css extension, eg. if your stylesheet is called 'default.css', then 'style' will equal 'default'.

We need to reference this file in our index page now. Your index page must have a .php extension for this to work. Add the following line to the top of your index page.

<?php include("<$MTBlogSitePath$>setstyle.php");?>

The Stylesheet

We are going to create the master stylesheet now. This will be the file referenced by the HTML and will be written in PHP. Basically we are taking advantage of the fact that PHP can return data for any filetype.

Here is the code. Put this in a new file and call it 'site-styles.php'.

<?php
    if (!$_COOKIE['style']) {
        /* here we check to see if the cookie exists
        and if it doesn't, set a default stylesheet */
        $style = "default";
    }
    /* output the correct HTTP header for the stylesheet */
    header("Content-Type: text/css");
    /* output the stylesheet itself */
    include("$style.css");
?>

This file needs to be referenced in your code in the place where you would normally reference the normal stylesheet. So change the appropriate line in your HTML to refer to 'site-styles.php'.

<link rel="stylesheet" href="styles-site.php" type="text/css" />

Allowing the user to change the Stylesheet

As of now, you should be able to change stylesheets by adding '?style=[style]' to your index URL in the browser window. But that's not very convenient or intuative for the user! So the last step is to create a way of letting the user change the stylesheet.

We can do this a couple of ways. I chose to use a form with a combo box and submit button to list the stylesheets, but you can just as well use a list of links, each linking to the URL required for each stylesheet. Example code for each technique follows.

The Form Method

<form method="get" action="index.php">
    Style:<br /><select name="style">
        <option value="default"<?php if ($style == "default") { echo " selected=\"selected\""; } ?>>Default Stylesheet</option>
        <option value="another"<?php if ($style == "another") { echo " selected=\"selected\""; } ?>>Another Style</option>
        <option value="different"<?php if ($style == "different") { echo " selected=\"selected\""; } ?>>Different Style</option>
    </select> <input type="submit" class="button" value="Go" />
</form>

The Links Method

<a href="index.php?style=default">Default Stylesheet</a><br />
<a href="index.php?style=another">Another Stylesheet</a><br />
<a href="index.php?style=different">Different Stylesheet</a><br />

And that should be that! Comments are appreciated.

Work in Progress

ClockRocket is a bit work in progress at the moment. So while I am currently in the middle of writing my site skinning tutorial I found that my site needs a bit of work in that department anyway!

Hell I could probably do with bringing the site code more up to date, as it's currently based on MovableType 2 templates which aren't very semantically correct.

Watch this space!

A Fresh Start

You may have noticed that all of a sudden all the content has dissapeared from this site. 'Why?' you ask? Well, I figured that I'm a bit fed up of writing about my day to day personal life and I'm sure it can't be that interesting to read either!

With that in mind, I intend to start producing useful and worthwhile content -- starting with an article on how I achieved the stylesheet switcher, particularly how I approached the problem of styling the pop up content windows.

Suggestions and comments are all appreciated (except the spammers, they can kiss my...).

Page:  1 … 16 17 18

About

Pixelhum is the web playground of Dan Barber (danbee), a disgruntled Web Designer and general geek.

Header image by Chris Percival. Licenced under Creative Commons.

Recent Listening

User


Could not include class file com.php