It doesn’t smell right!
How often do you refactor your code so that it works better or is easier to understand? Hopefully this is a constant process where you are refining and adapting on a regular basis (note that this is not quite the same as just rewriting code for the sake of it). Recognising that a section of code “smells” and needs to be refactored is not a particularly straight-forward task, some of the signs I use to spot “smelly” code include:
- repeated blocks of the same code in different areas of the code base
- huge long “
if...else if” blocks
- variable names that have no meaning or description of their contents
- function names that are not descriptive of their purpose
- code that doesn’t have supporting unit tests
Of course there will be different “smells” for different programming environments, I’m a Javascript developer and my “nose” is focussed on how my javascript code performs within web applications.
Have you sniffed your code recently to check that it doesn’t smell?
Make your PNGs 32-bit
You should always ensure any alpha transparent PNGs are 32-bit to ensure that they always display the same colour across all browsers. Recently, Firefox and IE displayed a particular 24-bit alpha transparent PNG differently. Changing the PNG to 32-bit fixed this and both browsers rendered the PNG the same.
I have also experienced this with Safari 2 on MacOSX – again, switching to 32-bit PNG fixed the problem.
How do I use alpha transparent PNGs across all browsers?
Many web developers have shunned the PNG image format in the past. For some this has been because of a perceived lack of support for alpha transparency (described formally as alpha compositing) in Internet Explorer. Here are some directions on how you can use alpha transparency successfully across all browsers – and specifically IE 6 for Windows.
The examples below rely on the following CSS code (described below):
<style type="text/css">
#demo1 {
width: 250px;
height: 100px;
background: url(images/logo.png) transparent 0 0 no-repeat;
}
</style>
<!––[if lt IE 7]>
<style type="text/css">
#demo1 {
background-image: none;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader
(enabled=true,sizingMethod=scale,src=/images/logo.png);
}
</style>
<![endif]––>
The first block of CSS sets a background image on an element with the id “demo1″ (typically a div, span or img) for all browsers. This works fine for everything else, but doesn’t work with IE prior to version 7.
The second block targets just these “broken” browsers through use of a conditional comment. This second block uses the filter property to set up a proper alpha transparent version of the png. Read more about this here at the MSDN site.
In the following example the CSS above would show the logo.png with alpha transparency as the background to a div:
<div id="demo1"></div>
This next example shows that you can display a png with alpha transparency by targetting a transparent image:
<img src="transparent.gif" id="demo1" alt=""/>
There are plenty of variations of this technique. All the examples above are considered “standards friendly”. This final example shows the same CSS simplified (but not passing validation):
<style type="text/css">
#demo1 {
width: 250px;
height: 100px;
background: url(images/logo.png) transparent 0 0 no-repeat;
_background-image: none;
_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader
(enabled=true,sizingMethod=scale,src=/images/logo.png);
}
</style>
If you are putting the CSS above into an external stylesheet, then be aware that the path to the logo.png when used as the background image is relative to the CSS file, but the path to logo.png when used in the filter is relative to the enclosing HTML file. This can cause some confusion.
On a related note… you should always ensure your alpha transparent PNGs are 32-bit to ensure that they always display the same colour across all browsers (specifically Firefox and IE displayed a 24-bit alpha transparent PNG differently using the technique described here – changing it to 32-bit confirmed this was the case).
How to target specific IE browsers using just HTML?
Many people were introduced to conditional comments with the introduction of IE7 and their grass-roots call to action to stop using “CSS Hacks” to target specific IE browser versions.
Conditional comments are totally valid markup (for HTML as well as XHTML doctypes) that cause IE browsers running in Windows (the old Mac versions of IE ignored them completely) to parse the contents of the commented markup.
The following code shows how a conditional comment can be used to match “all versions of IE less than version 7″ and include an extra css file and an extra div in the content of the page:
<html>
<head>
<link href="default.css" type="text/css" rel="stylesheet" />
<!––[if lt IE 7]>
<link href="iexplore.css" type="text/css" rel="stylesheet" />
<![endif]––>
</head>
<body>
<h1>Testing</h1>
<!––[if lt IE 7]>
<div>I'm an old IE running in Windows</div>
<![endif]––>
</body>
</html>
Because of the start HTML comment code (<!--), everything until the close HTML comment code (-->) is treated as a comment for non matching IE Windows browsers.
The code that is actually rendered by the browser is different when viewed in IE Windows browsers older than IE7 and any other browser:
<html>
<head>
<link href="default.css" type="text/css" rel="stylesheet" />
<link href="iexplore.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h1>Testing</h1>
<div>I'm an old IE running in Windows</div>
</body>
</html></code>
This is the code that is rendered in other browsers:
<html>
<head>
<link href="default.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h1>Testing</h1>
</body>
</html>
You might choose to use this to set some variables on the page for javascript to use – without having to worry about user agent detection issues and spoofing browsers (remember that this will not work for the old IE Mac browsers – only Windows):
<script type="text/javascript">
var isIE=isIE5=isIE6=isIE7=false;
</script>
<!––[if IE>
<script type="text/javascript">
isIE=true;
</script>
<![endif]––>
<!––[if IE 7>
<script type="text/javascript">
isIE7=true;
</script>
<![endif]––>
<!––[if IE 6>
<script type="text/javascript">
isIE6=true;
</script>
<![endif]––>
<!––[if IE 5>
<script type="text/javascript">
isIE5=true;
</script>
<![endif]––>
Be sure to check out the difference between down-level hidden (which is what I have used all the examples in this FAQ) and down-level revealed at the MSDN conditional comments page.