3 CSS Cross Browser Code Snippets

Here are 3 CSS hacks I use on a weekly basis. They have been greatly covered before but I think it’s important to agglomerate them into one post as they are, to me, the major cross browser fixes I need regularly. It’s a good idea to keep these in your Coda Clips.

Cross-browser min/max height/width

This 3-line hack makes it easy to implement min-height, max-height, min-width and max-width across all browsers (dirty look at IE6).

.class {
min-height: 200px;
height: auto !important;
height: 200px;
}

It’s pretty straight forward. Modern browsers will understand the min-height property and the !important designation. So when the block contains content that is shorter than 100px, min-height will be observed while content taller than 100px will be adjusted properly.

IE6 only understands one line in this whole class, the last one. But that’s ok because it interprets height just like modern browsers interpret min-height, adjusting the height for taller content but not making the box any shorter than specified.. So we’re good. Because height: auto has the important designation, newer browsers will simply ignore the last line.

Of course you can apply this hack with max or min and width or height.

Cross-browser float clearing

If there’s one thing I hate when coding XHTML pages it’s having to drop in <div class=”clear”></div> any time I want to clear a float. It’s ugly, cumbersome and to me, goes against the principle that your XHTML code should not have have presentational markup.

Well lucky for me (and you), there are several ways you can clear floats with CSS. Position Is Everything talks about this long method which I personally find to be a bit confusing and apparently it doesn’t work with IE7. Instead, you can use this very simple alternative which takes up only 2 lines of your stylesheet.

.clear {
overflow: hidden;
width:100%;
}

This works because by setting overflow establishes new block formatting contexts. For IE, by giving hasLayout to the element, it will clear. This why we give it width but height and zoom will work as well. Just make sure that if you use zoom that you are setting in a different stylesheet with conditional comments to keep your CSS valid.

Cross-browser PNG transparency (almost)

Again I can’t help but give that dirty look to IE6 because it is the only browser that this concerns. Fortunately though versions of IE7 and up and all the good browsers now support it, but that’s no news. There is hope for the annoying older brother. It has a special filter built in that will allow transparency but it’s use is quite limited. As long as you don’t need to use background-position or repeat it does work pretty good, in fact I use it on this site for pretty much every image except the wood background. For example, the white background on this post is a 1px high transparent PNG which is repeated on modern browsers but “streched” with IE6. So in a way, it simulates a repeated image. Here’s the code:

.background {
background:transparent url(img/left-col-bg.png) repeat-y scroll left top;
_background: none;
_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src=’img/left-col-bg.png’);
}

Using the undersore hack, we can target IE6 and previous versions only in lines 2 and 3. Notice the sizingMethod=scale which is what streches the image like I talked about. For elements such as menu buttons (like the ones on this blog) use sizingMethod=crop.

CSS3 cross browser compatibility: filling in the gaps with jQuery

With CSS3 still in its implementation phase, as a Web Developer I’m just itching to use it as I’m sure many are. Many of the new properties and selectors are extremely handy and will make the language much more complete and streamlined. Although it’s semi-compatible with some of the more modern browsers out there, the more seasoned browsers still in use don’t and probably will never support the new version of CSS. Of course, by the time it’s fully implemented, the old guys will be out and we won’t need to worry about that anymore… NOT! We’ll still be in the same conundrum when CSS4 comes out and people are still using IE7, FF2 and FF3. Will it ever end?

In the mean time there is a short term solution that can help you take advantage the new selectors in CSS3. By using jQuery, we can harness the power of CSS3 selectors as it understands them fully and it’s compatible with browsers dating back to IE6.

Say for example you want to add alternate row color to a table. You can do that pretty easily with CSS3 by using the :nth-child(odd) selector but even Firefox 3 doesn’t support this one; the only browsers currently supporting this very useful selector are Opera 9.51 and Safari 3.1.2 (at the time of writing). The other ways to do this would be to hard code a class directly in the markup (yuck!) or if the table is PHP generated you could easily add it with a loop. More simply, you could use jQuery!

Here’s how it’s done. In your head tag, you must include the latest version of the Javascript library. Once jQuery is linked, add this bit of Javascript to your head.

$(document).ready(function() {
$(”table tbody tr:nth-child(odd)”).addClass(”odd-row”);
});

With this simple code, every odd tr tag of a table will have the odd-row class added to it. It’s that simple. We can also take advantage of attribute filters. Say you also want to add a class to links to PDF files:

$(document).ready(function() {
$(”table tbody tr:nth-child(odd)”).addClass(”odd-row”);
$(”a[href*='pdf']“).addClass(”pdf-link”);
});

The possibilities are endless (as is usually the case with jQuery). You can see a list of the supported selectors in the jQuery Documentation pages. You can also see these examples in action.

How Parallels Desktop can help Mac Web Developers

In this first of what I hope to be a series of screencasts, I go over a great tool for Mac OS called Parallels Desktop. As a Web Developer working on Mac OS, I think I speak for all of us when I say that it can be quite difficult to test your Web sites on Windows browsers such as IE7 and IE6. You either have to install Windows on a separate partition using Boot Camp or maybe you have a Windows PC laying around. In both those cases, you’re having to change computers (or reboot) to look at your work on the Windows platform. With Parallels, you can easily install and run Windows on your Intel Mac quite seamlessly without the hassel of going to a different computer or even rebooting .

In this short video tutorial, I will show you how Parallels works and how it can help your working conditions as a Mac Web Developer and make your debugging a lot simpler.

[flashvideo filename="http://sebastiencouture.com/wp-content/uploads/2008/08/screencast-1-parallels.flv" /]