Filed under: Web, internet | Tags: height, javascript, jquery, resize, window
I am working on a recode for a government agency website at the moment, their existing site essentially uses tables (SHAME!) to handle layout. One benefit of working with tables is of course the ease with which multi-column layouts can be achieved and no need for trickery that we resort to with CSS layouts. So, in my efforts to recode their site for CSS layout I’ve had to come up with some workarounds. Typically I would have used faux columns developed years ago, that generally work really well, but this time I decided I wanted to employ jQuery to assist.
I chose to use jQuery to assist me with many things, but most importantly setting the heights of two containing divs that are floated next to one another, with the first also containing two floated divs inside which when the layout becomes too narrow the second falls below the first.
|
Features |
Because their site has the news columns with one b/g colour, and features with a second b/g colour I at first tried using teh jQuery equalHeights plug-in. This worked okay, but added annoying scrollbars. So I used their code as the basis for just doing it manually.
function adjustHeights() {
hNews = $(‘#news’).height(); // Get height
hFeatures = $(‘#features’).height(); // Get height
mHeight = hNews > hFeatures ? hNews : hForestry; // Set mHeight to the larger of the two heights
$(‘#news, #features’).height(mHeight); // Set both to equal heights
}
This worked okay. The problem I was now faced with, and a likely one, is that if someone resized their browser the columns didn’t adjust. So to capture the resize event and readjust sizes I used the following code:
$(window).resize(function() {
$(‘#news, #features’).css(“height”,”auto”); // Eliminate the heights value
adjustHeights(); // Set the heights once more
});
So far I’ve tested this in IE6, FF3.06 and Safari 3.21 and it’s working across each. Safari seems to do a better job too, as I’m dragging the browsers width it’s applying the fixes.
Hope this helps anyone else attempting the same!
5 Comments so far
Leave a comment
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>





IE8 Release Candidate 1 has a bug on window resize that it enters a permanent loop, which affects the script above. The IE team has stated it’s a known bug that will be finished with the final release. One step forward…hmm
Comment by g04uld February 22, 2009 @ 5:03 pm[...] jQuery – adjusting div heights on window resize [...]
Pingback by Internet Explorer 8 is a wrap « Longing for a break February 24, 2009 @ 8:10 pmYou want a break? Stop trying to use script for layout. (!) More to the point, stop trying to use jQuery. If the authors don’t understand it, how can you hope to maintain a working site?
Comment by David Mark April 7, 2009 @ 4:02 amThanks for your comments David. I have to disagree with you though. I was faced with either:
a) Using something like faux columns -> requiring additional html; or
b) Using css tables and then providing IE specific stylesheet to handle their lack of support for this method;
c) Using scripting with a view that once IE6 and IE7 are erased from our landscape that the script be removed and go to option b).
If option c) isn’t for you, go the alternates. I chose the use this method as I was going to be using dynamic width columns for which faux columns wasn’t really an option (as they rely upon a background image). Given the number of hits I’ve already had on this post (over 460 when I last checked), it would seem that others are also looking for additional options with regards flexible column layouts.
If you don’t understand jQuery then I suggest you invest some time, it really is a fantastic javascript library that I find saves me oodles of time.
Comment by g04uld April 7, 2009 @ 6:20 amAnother posting on this topic (excluding browser resize support): http://www.impressivewebs.com/equal-height-columns-with-javascript-full-version/
Comment by g04uld April 18, 2009 @ 9:54 am