Math Jazz — Mathias Bynens’s shizzle, y’all



Note: This site might seem inactive… That’s because it is. Don’t worry though, I’m still coding webpages and stuff! If you’re interested, I suggest you get a translator and head over to Qiwi; or you could just check the latest site we’ve been working on: Apotheek Goethals – Debrabandere. Enjoy!

PHPSS

Say what?

By combining PHP with CSS, there’s nothing that can’t be done. PHPSS is the only CSS hack you’ll ever need!

Why?

I never liked using all those CSS hacks in order to make designs look as they should on non-standards compliant browsers. So I decided to apply just one hack.

How?

Let’s get started. There’s two things we need to do before even rendering the CSS:

  1. Gzipping the CSS file;
  2. Check what sort of browser the person viewing the page is using.

So, we just put this code at the top of the CSS file.

<?php
ob_start('ob_gzhandler');
header('Content-type: text/css');
header('Cache-Control: must-revalidate');
$offset = 60 * 60;
$expstr = 'Expires: ' . gmdate('D, d M Y H:i:s', time() + $offset) . ' GMT';
header($expstr);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) $ie = 1;
?>

Note: In this example we’re only going to check whether the browser is MSIE or not, but of course other browsers can be easily detected in a similar way. As far as I know, Internet Explorer is the only browser so crappy it’s actually worth using PHPSS.

What I use it for

My stylesheet is full of these values and attributes differing, depending on the browser. IE can’t even handle border-style: dotted; properly — it displays a dashed border instead. Because the difference between these two is huge in my humble opinion, I coded my PHPSS so that IE users will see an dotted image under their links. Here’s my code for styling links.

a, a:link {
 color: #153160;
 text-decoration: none;
 border-bottom: 1px dotted #235299;
<?php if($ie) { ?>
 border: 0;
 padding-bottom: 1px;
 background: url(/style/v4/underline.png) bottom right repeat-x;
<?php } ?>
 }

a:hover {
<?php if($ie) { ?>
 padding-bottom: 0;
<?php } ?>
 border-bottom: 1px solid #f69600;
 background: #feffad;
 }

Real-world example

Let’s hack the Box Model Hack. Go see the original CSS code of div.content — it has four rules, of which only one is actually used in the design. Using PHPSS, the code looks much nicer and is smaller, because it only outputs the needed rule depending on the browser.

div.content {
<?php if($ie) { ?>
 width: 400px;
<?php } else {
 width: 300px;
<?php } ?>
 }
Filed under PHP, CSS · October 2nd, 2004

Comments (19)

Listed below are the responses for this entry.

  1. Rob Mientjes:
    This commenter’s Gravatar

    Handy. I might need this…

    No I don’t. I say fuck you to IE. I’m going to use PNG from now on and link to Firefox downloads if they can’t see the image correctly :P

    Comment posted on October 2nd, 2004 @ 11:46 am
  2. Mathias:
    This commenter’s Gravatar

    Hmmm… I stopped caring for MSIE support whilst working on some of my pre-designs for a while, too — it all just got too much. But then again, 90% of all internet users on this planet are still on the bastard! Quite depressing… But that doesn’t mean I’m not proud of this site’s Crappy Browser Support!

    Comment posted on October 2nd, 2004 @ 12:00 pm
  3. Ben de Groot:
    This commenter’s Gravatar

    I was thinking the other day about using the same method myself. Now I see you published it! This can indeed be handy. But of course using browser-sniffing has its limitations. You can have Opera or Mozilla (Firefox) lie about who they are, claiming that they are IE instead…

    And another thing: maybe you can say fuck you to Internet Exploder, but as a web professional I find it impossible to ignore it, so I will apply accessibility and graceful degradation practices, and some hacks where needed.

    Comment posted on October 2nd, 2004 @ 4:22 pm
  4. Rob Mientjes:
    This commenter’s Gravatar

    Oh Ben, that’s just on my own blog. For clients, you can’t do that. And besides, it does work in IE. Just not perfectly. As a web professional, I say that only on my own blog.

    Comment posted on October 2nd, 2004 @ 6:54 pm
  5. Mathias:
    This commenter’s Gravatar

    Ben:
    You know what they say; great minds think alike ;) And you’re right: whoever calls himself a web designer, but can’t be bothered to apply any IE support, has been labeling himself incorrectly.

    Update: I only just noticed you actually wrote Internet Exploder — that’s just hilarious, man.

    Comment posted on October 3rd, 2004 @ 9:32 am
  6. owen:
    This commenter’s Gravatar

    Perfection is impossible. I kinda just make it look as close to what I want as possible but I’m not a designer.

    Comment posted on October 3rd, 2004 @ 6:56 pm
  7. markku:
    This commenter’s Gravatar

    A simple yet brilliant solution to avoid using hacks within your stylesheets. Good work.

    Comment posted on October 4th, 2004 @ 7:47 am
  8. logtar:
    This commenter’s Gravatar

    Very cool, I would have to look into this for sure.

    Comment posted on October 4th, 2004 @ 8:03 pm
  9. Joachim Guanzon:
    This commenter’s Gravatar

    Damn!!! You are so smart!! This is the perfect thing that I need!! You are a godsend! Keep the great ideas coming!

    Comment posted on October 6th, 2004 @ 12:20 am
  10. Mathias:
    This commenter’s Gravatar

    Got an email from Phunk today…

    I see some problems with your code:

    • Browsers can spoof the UA string (so you may be sending your IE hacks to Opera when you don’t want to).
    • Some users are behind a proxy/cache. A large ISP using an aggressive cache will cache your CSS. Lets say one user behind the proxy uses IE. He views your site, the IE stylesheet gets cached, then the next person uses Firefox and still gets your IE stylesheet.
    • The way you inline all that PHP in your CSS looks messy (but you could get around that same way I do and @import the hacks).

    The Gzipping idea is good though and worth looking into.

    P.S.: My IE hacks are in a separate file, which is only 2 KB in size. It gets read by every browser, but then I don’t have any problems mentioned above.

    Dear Phunk,

    • Who would let his browser spoof the UA string unwanted? If anyone wants to perform operations like these, I don’t mind — but then the consequences aren’t my problem anymore.
    • As for the users behind a proxy/cache thing, I never thought of that. You’ve got a pretty good point here. But as long as the XHTML itself doesn’t get cached, this can be easily solved by using this simple workaround.
      1. In index.php, or whatever PHP file that renders the XHTML of your page, put this line on top: <?php if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) $ie = 1; ?>.
      2. In the same file, look for where your stylesheet is called. (This can be many things, e.g. <link rel="stylesheet" media="all" href="/style.php" type="text/css" />.) Right after “style.php”, put the following PHP code: <?php if($ie) { ?>?browser=crap<?php } ?>">. After making this modification, the line should read something among the lines of <link rel="stylesheet" media="all" href="/style.php<?php if($ie) { ?>?browser=crap<?php } ?>" type="text/css" />.
      3. In the PHP-powered CSS file, replace the line saying if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) $ie = 1; with $ie = $_GET['browser'];.
    • As for the messy look of the PHP in the CSS: personally, I think I’ve seen messier things… But hey, thanks for your workaround.

    Hope that clears out some misunderstandings regarding this new technique.

    Comment posted on October 10th, 2004 @ 11:49 am
  11. SeanW:
    This commenter’s Gravatar

    Whoops… excuse that TrackBack comment… not sure what happened there. Made a reference to this on my blog… cool stuff. =)

    Comment posted on October 21st, 2004 @ 9:57 pm
  12. ken:
    This commenter’s Gravatar

    I do similar on lsblogs, not for browser specific workarounds, as I can’t stand those, but for other reasons.

    Comment posted on November 5th, 2004 @ 2:50 am
  13. MaierMan:
    This commenter’s Gravatar

    About the Proxy (Caching of CSS files) problem:

    How about a new header for this? (Per server-config or PHP header())

    Vary: User-Agent

    See: http://w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44

    Comment posted on January 9th, 2005 @ 4:44 am
  14. Mathias:
    This commenter’s Gravatar

    You could indeed force the proxy to download a fresh version of the CSS each time, but that would suck a whole lotta bandwidth. Plus, CSS files are ment to be cached — they’re not half as !important as the content of a web page, so you can’t let users download a 10k file every time they visit a page on your site.

    Comment posted on February 6th, 2005 @ 3:53 pm
  15. mstyle:
    This commenter’s Gravatar

    I’ve been reading up on this box-model IE/FF thing somewhat and after reading somewhat the same things everywhere or related I must say that this all did one great thing, it gave us Firefox! Yay! Plus, a competitive market will surely be better when it comes to sticking to W3C guidelines. I never liked IE, at least not from the point where I got to know how M$ works their ways. It’s a good slap on their nose and even more they seem to have understand it also.

    Guess for now we need to go with what we have, keeps people creative. And more and more working hacks float to the surface, it’s a matter of picking one that works for one.

    I dislike IE as well, but to me and the viewing audience I just want to do stuff as good as possible although I understand Rob Mientjes by saying Screw you IE. It’s not how I act, but for sure how I feel.

    Comment posted on February 17th, 2005 @ 5:15 am
  16. GT:
    This commenter’s Gravatar

    Mathias — that Apple logo is truly stylish… is it illegal if I steal it? I want to put some PHP browser-hacks into my CSS, and display this as a background when the user is on a Mac (using an OS test, I think).

    I had been toying with a similar idea to tailor presentation to screen resolution; I hate it when pages simply leave 25% of the screen vacant because they’re designed for 800×600 in a 1024×768 world; I don’t think the PHP logic to test for resolution would slow things down too much…

    Comment posted on April 20th, 2005 @ 7:35 am
  17. Mathias:
    This commenter’s Gravatar

    Geoffrey, you’re allowed to steal everything from this site, as long as you give credit where it is due.

    As for the resolution checking, I’m afraid that’s not possible… By using only PHP, I mean. Screen resolution is a client-side setting, so you’ll need to use JavaScript to detect it.

    Comment posted on April 20th, 2005 @ 11:20 am
  18. Chris:
    This commenter’s Gravatar

    Is it possible to do the PHP hack with jsp? Anybody who has tried this?

    Comment posted on March 17th, 2006 @ 11:09 am
  19. Joopla:
    This commenter’s Gravatar

    http://www.hedgerwow.com/360/dhtml/css_modern_hack.html

    Comment posted on April 10th, 2006 @ 1:28 pm

Trackbacks & Pingbacks (5)

Listed below are resources on the web that mention this article.

  1. SeanW.com: PHP + CSS = PHPSS:
    This commenter’s Gravatar

    Cool huh? Entirely possible. Go check it out!

    Trackback made on October 21st, 2004 @ 7:48 pm
  2. Fiftyfoureleven Weblog: Applied CSS Management and Optimization:
    This commenter’s Gravatar

    Applied CSS Management and Optimization
    […] To help deal with managing hacks, we can use either CSS filters or browser detection. In the end, both of these methods hide styles from target browsers. […]

    Trackback made on February 17th, 2005 @ 11:48 am
  3. Script School News: A PHP box model hack for CSS:
    This commenter’s Gravatar

    A PHP box model hack for CSS
    While battling CSS, I found some nice linkage to a PHP hack around the IE box model problem. It’s a great alternative!

    Trackback made on June 13th, 2005 @ 1:32 pm
  4. Uber Blah!: PHP to avoid browsers incompatibility:
    This commenter’s Gravatar

    PHP to avoid browsers incompatibility
    Every web designer has to face the one truth: there’s no code that works the same on all browsers. […]

    Trackback made on June 16th, 2005 @ 12:38 am
  5. Templaterie blog: Wählerisches CSS.PHP:
    This commenter’s Gravatar

    Wählerisches CSS.PHP
    Der Sniffer-Weg prüft beim Benutzer, mit welchem Browser dieser unterwegs ist. Der Snifferweg scheint eine sichere Sache zu sein, wäre da nicht die Notwendigkeit für kleine Minoritäten-Browser zu lügen, um auf Seiten gelassen zu werden.

    Trackback made on June 22nd, 2005 @ 12:35 am