With the absence of table-less designs, creating equally proportionate “boxes” in HTML can be daunting. So, with the help of a couple of CSS tricks and jQuery, I’ll demostrate a cross-browser method of creating equally tall and wide boxes.

before:

after:

The new jQuery $.equalHeights([widths]) Plugin

$.fn.equalHeights = function(widths) {
   $(this).each(function(){
      var tallest = 0;
      var widest = 0;
      $(this).children().each(function(i){
         if ($(this).height() > tallest) { tallest = $(this).height(); }
         if (widths) if ($(this).width() > widest) { widest = $(this).width(); }
      });

   // for ie6, set height since min-height isn't supported
   if ($.browser.msie && $.browser.version == 6.0) {
      $(this).children().css({'height': tallest}); 
      if (widths) $(this).children().css({'width': widest}); 
   }

      $(this).children().css({'min-height': tallest});
      if (widths) $(this).children().css({'min-width': widest});
   });
   return this;
};

This plugin has been changed from the original to include the optional [widths] paramter instead of the previous [px]. You can now pass true if you need the plugin to apply the largest width across all children.

The JavaScript

<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">
</script>
<script type="text/javascript">
   $(document).ready(function(){
      $(".boxes").equalHeights(true);
});
</script>

Here I pass true as an argument to ensure the plugin sets the widest min-width across all boxes.

The CSS

<style type="text/css">
.box {background:#39F; margin:0.5em;}
.boxes {background:#eee;}
.iblock{
   display: -moz-inline-stack;
   display: inline-block;
   vertical-align: top;
   zoom: 1;
   *display: inline;
}
</style>

Using the .iblock class is a great trick for getting boxes to look and behave properly throughout the most popular browsers- IE6 to the latest Firefox. I discuss it further here.

The Markup

<div class="boxes">
   <div class="box iblock">Hello</div>
   <div class="box iblock"><p>Hello</p><p>World. How are you?</p></div>
   <div class="box iblock">Hello</div>
   <div class="box iblock">Hello</div>
</div>

This chunk of HTML shows the basic markup for the boxes in this demo.

The Full HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Boxes and Grids</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">
</script>
<script type="text/javascript">
   $(document).ready(function(){
      $(".boxes").equalHeights(true);
});
$.fn.equalHeights = function(widths) {
   $(this).each(function(){
      var tallest = 0;
      var widest = 0;
      $(this).children().each(function(i){
         if ($(this).height() > tallest) { tallest = $(this).height(); }
         if (widths) if ($(this).width() > widest) { widest = $(this).width(); }
      });

   // for ie6, set height since min-height isn't supported
   if ($.browser.msie && $.browser.version == 6.0) {
      $(this).children().css({'height': tallest}); 
      if (widths) $(this).children().css({'width': widest}); 
   }

      $(this).children().css({'min-height': tallest});
      if (widths) $(this).children().css({'min-width': widest});
   });

   return this;
};

</script>
<style type="text/css">
   .box {background:#39F; margin:0.5em;}
   .boxes {background:#eee;}
   .iblock{
      display: -moz-inline-stack;
      display: inline-block;
      vertical-align: top;
      zoom: 1;
      *display: inline;
   }

</style>
</head>
<body>
<div class="boxes">
   <div class="box iblock">Hello</div>
   <div class="box iblock"><p>Hello</p><p>World. How are you?</p></div>
   <div class="box iblock">Hello</div>
   <div class="box iblock">Hello</div>
</div>
</body>
</html>

CSS Inline-block is a great CSS property that enables grid-like designs without all the hacks of using tables. This property, however, is not cross-browser friendly. A quick search can provide a ton of information regarding the typical support for this property. For now, here’s a cool workaround that works with most a-grade browsers using descending definitions:

Use a class, like .iblock, to apply the following css styles to the elements you need to be inline-block. Please note, this does not validate because it uses proprietary browser definitions.

<style type="text/css">

.iblock {
display: -moz-inline-stack;
display: -moz-inline-box; /* https://developer.mozilla.org/en/CSS/display */
display:inline-block;
zoom:1;
*display:inline;
/* Alignment Fix */
vertical-align:top;
}

a {
width:50px;
margin-right:12px;
background:lightblue;
margin-bottom:12px;
}
</style>

<a href="#" class="iblock">Inline Block</a>
<a href="#" class="iblock">Inline Block</a>
<a href="#" class="iblock">Inline Block <br />With Two lines</a>
<a href="#" class="iblock">Inline Block</a>
<br />
<a href="#" class="iblock">Inline Block</a>
<a href="#" class="iblock">Inline Block</a>
<a href="#" class="iblock">Inline Block <br />With Two lines</a>
<a href="#" class="iblock">Inline Block</a>

These browser transport methods are being exploited to include or link to base64 encoded images. Some of the methods being developed are trying to replace image calls in css sprites. The concept is that an embeded base64 image would be more accessible. Others, like the guys at cappuccino.org, are trying to achieve single files that combine css/images/JavaScript inside JavaScript. The purpose is highly compressed, both minified and gzipped, JavaScript files that save bandwidth. These could then be distributed and cached nicely among CDNs.

These methods are great in theory, but concern me when it comes to graceful degradation, accessibility, and availability. How would users without JavaScript, MHTML, or Data URL protocols view these dependent websites… More to come.