Using jQuery To Detect CSS-Based Table Display Capabilities

Posted December 1, 2009 at 9:50 AM

Tags: Javascript / DHTML, HTML / CSS

Yesterday, I started to play around with CSS-based table displays for my "Best of ColdFusion 9" contest entry. Based on some very brief testing, it appears, as expected, that Internet Explorer (IE) is the only browser that I have that doesn't support CSS-based table displays. This is something that I might ordinarily deal with via browser-sniffing; but, I believe with the future releases of jQuery, the jQuery team wants us to move away from browser-sniffing and move more towards capability-sniffing.

The difference between browser-sniffing and capability-sniffing is that capability-sniffing checks for individual pieces of functionality rather than entire sets of functionality. The benefit to this approach is that it allows for browsers to be partial upgraded (adding support for some functionality) without breaking the code. With this approach in mind, I wondered if I could programmatically detect CSS-based table display support.

While this was not extensively tested (I only have a few different browsers), it appears that you can detect support for CSS-based table displays by gathering the markup of elements that contain inline table CSS and checking it for the inline CSS; the theory here is that non-supporting browsers will strip out the unsupported table-CSS:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>
  • Detecting CSS-Based Table Display Support With jQuery
  • </title>
  • <style type="text/css">
  •  
  • div.table {
  • display: table ;
  • width: 100% ;
  • }
  •  
  • div.table-row {
  • display: table-row ;
  • }
  •  
  • div.table-cell {
  • display: table-cell ;
  • }
  •  
  • div.first-table-cell {
  • background-color: #E0E0E0 ;
  • width: 50% ;
  • }
  •  
  • div.last-table-cell {
  • background-color: #FFE0E0 ;
  • width: 50% ;
  • }
  •  
  • /* -- Fixes for non-Table support. -- */
  •  
  • div.table-fix {
  • display: block ;
  • }
  •  
  • div.table-fix div.table-row {
  • display: block ;
  • }
  •  
  • div.table-fix div.table-row:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
  • div.table-fix div.table-row { display: inline-block; }
  • /* required comment for clearfix to work in Opera \*/
  • * html div.table-fix div.table-row { height:1%; }
  • div.table-fix div.table-row { display:block; }
  •  
  • div.table-fix div.table-cell {
  • display: block ;
  • float: left ;
  • }
  •  
  • </style>
  • <script type="text/javascript" src="jquery-1.3.2.js"></script>
  • <script type="text/javascript">
  •  
  • // Check to see if this browser supports CSS-based Table
  • // layout displays.
  • var supportsCSSTableLayout = (function( $ ){
  •  
  • // Create nodes with the table-based display values.
  • var tableLayoutTest = $(
  • "<div>" +
  • "<div style=\"display:table;\"></div>" +
  • "<div style=\"display:table-row;\"></div>" +
  • "<div style=\"display:table-cell;\"></div>" +
  • "</div>"
  • );
  •  
  • // Get the HTML for the above node and gather all
  • // "table" type displays from the markup (IE browsers
  • // will strip that out of the markup if they do not
  • // support it.
  • var tableMatches = tableLayoutTest.html().match(
  • new RegExp(
  • "table(-(row|cell))?",
  • "gi"
  • )
  • );
  •  
  • // Return true if all three table-based displays were
  • // found in the markup.
  • return(
  • (tableMatches && (tableMatches.length == 3)) ?
  • true : false
  • );
  •  
  • })( jQuery );
  •  
  •  
  • // -------------------------------------------------- //
  • // -------------------------------------------------- //
  •  
  •  
  • // When the DOM is ready to be interacted with, init.
  • jQuery(function( $ ){
  •  
  • // Output whether or not CSS table display is supported.
  • $( "#has_support" ).text( supportsCSSTableLayout );
  •  
  • // If there is no CSS-based table display support,
  • // then add the fix to the table elements.
  • if (!supportsCSSTableLayout){
  •  
  • $( ".table" ).addClass( "table-fix" );
  •  
  • }
  •  
  • });
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • Detecting CSS-Based Table Display Support With jQuery
  • </h1>
  •  
  • <p>
  • Supports CSS Table Display:
  • <strong id="has_support">...</strong>
  • </p>
  •  
  • <div class="table">
  • <div class="table-row">
  •  
  • <div class="table-cell first-table-cell">
  • Cell 1
  • <br />
  • </div>
  •  
  • <div class="table-cell last-table-cell">
  • Cell 2
  •  
  • <!--- Add breaks here to grow cell. --->
  • <br /><br /><br /><br /><br /><br />
  • </div>
  •  
  • </div>
  • </div>
  •  
  • </body>
  • </html>

As you can see in the above code, I am creating three DIV elements, each with a different table-display property. I then grab the markup of those DIV elements and see if I can extract all three table-display properties. If I can, I am assuming that table-display is supported.

Here's me running the above code in FireFox (or Safari, or Chrome), which supports CSS-based table display:

 
 
 
 
 
 
Detecting CSS-Based Table Display Support With jQuery (in FireFox). 
 
 
 

And, here's me running the above code in Internet Explorer 7, which does not support CSS-based table display:

 
 
 
 
 
 
Detecting CSS-Based Table Display Support With jQuery (in IE). 
 
 
 

As you can see, the support for CSS-based table display was detected effectively. But again, I have not tested this exhaustively; however, I believe this is a similar approach to feature-detection that the future releases of jQuery will be using.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Print Page




Reader Comments

Dec 1, 2009 at 12:03 PM // reply »
2 Comments

why not use this?

if($.browser.msie && $.browser.version >= 8 ){ // do your thing }

I know they say that it isn't for browser "detection" but seems to work great...


Dec 1, 2009 at 12:09 PM // reply »
7,572 Comments

@Steven,

From what I heard John Resig explain at jQuery Conf. 2009, they are moving away from checking for things like "MSIE" and version number because then if MSIE releases a new browser or someone releases some plugin that updates functionality, then your code might not function properly.

For example, I think there's now a way to use the Chrome rendering engine inside of IE (http://code.google.com/chrome/chromeframe/). I haven't looked too much into that, but how does it change the user agent? I am not sure if it does, or if it modifies it in a way that looks like IE. In such a case, you might think you are in a crippled browser, but you are actually in a standards-compliant context. This is where checking individual functionality would be great since it is not browser-specific.


Dec 1, 2009 at 12:34 PM // reply »
21 Comments

Yah, the idea of moving away from browser sniffing pre-dates jQuery.

Back in the day it was relatively easy to check for MSIE or Netscape. Nowadays you'd have to check for MSIE, Firefox, Chrome, Opera, etc.

Not only that, but you'd have to check versions of each.

And when the next cool browser comes out, you need to go back and update all your code O_O

Checking for specific functionality allows your code to continue working in spite of what new browsers, or new browser versions, are introduced.

I think one of the earliest implementations of this was doing "if (document.images)" to see if the browser supported the ability to reference images using that syntax in order to do image-swapping :)


Dec 1, 2009 at 12:37 PM // reply »
2 Comments

you could kill the regex with this one liner and it would execute a whole lot faster:

return(tableLayoutTest.html().length > 37); // length of the divs if they are empty


Dec 1, 2009 at 12:39 PM // reply »
7,572 Comments

@Charlie,

Great explanation! Yeah, I remember doing the if (document.all) check for IE-based stuff back in the day. Oooh, and remember, if (document.layers) for the old netscape stuff! Grrr :)

@Steven,

Oh, I didn't think of that.


Dec 2, 2009 at 3:45 AM // reply »
2 Comments

nice. thx.
i have a questions, is it possible to split images with the table display css function?

Can I include an image in the tables?

Do you know what I mean?


Dec 2, 2009 at 3:49 AM // reply »
2 Comments

nice. thx.
i have a questions, is it possible to split images with the table display css function?

Can I include an image in the tables?

Do you know what I mean?

edit:
i mean to post the whole image, but it shall to hulk up a part of the image.

__________
| | image
----------

and of this image I want to want a specific part, which I want to choose.


Dec 2, 2009 at 10:37 AM // reply »
7,572 Comments

@Carina,

I am not sure I fully understand; but, you should be able to wrap the image in a fixed-height/width DIV with no overflow and give the image some negative margins.

It sounds like you want to create a clipping of the original image?


Dec 7, 2009 at 8:24 AM // reply »
2 Comments

ah okay, thx.

Yes, thats right i meant to create a clipping of the original image. Sry for cant explain what ive soght ;-)

Is it heavy to create it?


Dec 7, 2009 at 8:34 AM // reply »
7,572 Comments

@Carina,

It would be something like this:

<div style="width: 100px ; height: 100px ; overflow: hidden ;">

<img style="position: relative ; top: -50px ; left: -25px ;" />

</div>

Maybe something like that? Where the top/left position of the IMG will create the clipping in the 100x100 DIV container.


Dec 8, 2009 at 2:58 AM // reply »
2 Comments

Good Morning Ben Nadel,

yes thx that was what I ve sought!
Omg! That took some doing ;-)

I have also tried with css rect, you know the rect order?


Dec 8, 2009 at 7:45 AM // reply »
7,572 Comments

@Carina,

I've seen the CSS-based rect and clip stuff, but I have not played around with it myself. I am not sure how widely supported it is.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »
Mar 19, 2010 at 4:47 PM
Application.cfc OnRequest() Method Affects OnError() Arguments
@Jason and @Ben, I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well. Found a way to work around my problem, but what I saw was... Background: Our ... read »
Jim
Mar 19, 2010 at 4:44 PM
Shoot 'Em Up Starring Clive Owen And Paul Giamatti
I actually enjoyed this movie quite a lot. It was different, certainly, but I think they were going for more of a Quentin Tarentino-"wow, that was weird"-vibe than an actual spoof. Once I realize ... read »
Mar 19, 2010 at 4:34 PM
An Intensive Exploration Of jQuery With Ben Nadel (Video Presentation)
Hey I guess the video is down. Is there anyway you can upload to youtube or vimeo or some other service? Greatly appreciated. ... read »
Mar 19, 2010 at 4:24 PM
ColdFusion CFPOP - My First Look
@Ben Thanks for the follow up! The root of the problem had to do with being able to trace bounced emails to specific records in a DB table. Let's say you run an email campaign and you get 1,000 bou ... read »