/    Sign up×
Community /Pin to ProfileBookmark

CSS Columns and FireFox

I’m trying to use JavaScript to create CSS columns that have equal height. I dynamically generate any number of DIV tags and would like for them to have the same height.

I have the following JavaScript:

[CODE] <script type=”text/javascript”>
matchHeight = function() {
var divs, contDivs, maxHeight, divHeight, d;
// get all <div> elements in the document
divs = document.getElementsByTagName(‘div’);
contDivs = [];
// initialize maximum height value
maxHeight = 0;

// iterate over all <div> elements in the document
for (var i = 0; i < divs.length; i++) {
// make collection with <div> elements with class attribute ‘smallcol’
if (/bsmallcolb/.test(divs[i].className)) {
d = divs[i];
contDivs[contDivs.length] = d;
// determine height for <div> element
if (d.offsetHeight) {
divHeight = d.offsetHeight;
}
else if (d.style.pixelHeight) {
divHeight = d.style.pixelHeight;
}
// calculate maximum height
maxHeight = Math.max(maxHeight, divHeight);
}
}

// assign maximum height value to all of container <div> elements
for (var i = 0; i < contDivs.length; i++) {
contDivs[i].style.height = maxHeight;
}
}

// execute function when page loads
window.onload = function() {
if (document.getElementsByTagName) {
matchHeight();
}
}

</script>[/CODE]

Here is my CSS:

[CODE].smallcol
{
float: left;
width: 32%;
background-color: #EEE7DD;
border: solid 0px #D0B996;
}[/CODE]

Everything works fine in IE, but does not work in FireFox.

Any ideas?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscApr 23.2009 — Just a quick glance at your code and I noticed a problem, not sure if this will solve anything, or if this is even the best way to go about what you want to achieve, but when applying the height property to the style of an element it needs to have units, so you need to change

<i>
</i>contDivs[i].style.height = maxHeight;


to

<i>
</i>contDivs[i].style.height = maxHeight + 'px';
Copy linkTweet thisAlerts:
@JMRKERApr 23.2009 — The post and fix work pretty good for me in FF.

I have a modified question about the script after playing around with it for a bit.

In the code below, is it possible to allow the heights to be equal for a particular class

and not be the same maximum heights for all the classes? All column displays have

the same height regardless of the content. I would like to have different max column

heights for each of the classes specified. I'm thinking I need to know the maximum

height for a particular class and somehow apply it to that class only (???)

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;2,3,4 &amp; Mixed Column Displays&lt;/title&gt;

<i> </i>&lt;script type="text/javascript"&gt;
<i> </i> matchHeight = function() {
<i> </i> var divs, contDivs, maxHeight, divHeight, d;
<i> </i> // get all &lt;div&gt; elements in the document
<i> </i> divs = document.getElementsByTagName('div');
<i> </i> contDivs = [];
<i> </i> // initialize maximum height value
<i> </i> maxHeight = 0;

<i> </i> // iterate over all &lt;div&gt; elements in the document
<i> </i> for (var i = 0; i &lt; divs.length; i++) {
<i> </i> // make collection with &lt;div&gt; elements with class attribute 'smallcol'
<i> </i> if (/bsmall2colb/.test(divs[i].className)) {
<i> </i> d = divs[i];
<i> </i> contDivs[contDivs.length] = d;
<i> </i> // determine height for &lt;div&gt; element
<i> </i> if (d.offsetHeight) { divHeight = d.offsetHeight; }
<i> </i> else if (d.style.pixelHeight) { divHeight = d.style.pixelHeight; }
<i> </i> // calculate maximum height
<i> </i> maxHeight = Math.max(maxHeight, divHeight);
<i> </i> }
<i> </i> if (/bsmall3colb/.test(divs[i].className)) {
<i> </i> d = divs[i];
<i> </i> contDivs[contDivs.length] = d;
<i> </i> // determine height for &lt;div&gt; element
<i> </i> if (d.offsetHeight) { divHeight = d.offsetHeight; }
<i> </i> else if (d.style.pixelHeight) { divHeight = d.style.pixelHeight; }
<i> </i> // calculate maximum height
<i> </i> maxHeight = Math.max(maxHeight, divHeight);
<i> </i> }
<i> </i> if (/bsmall4colb/.test(divs[i].className)) {
<i> </i> d = divs[i];
<i> </i> contDivs[contDivs.length] = d;
<i> </i> // determine height for &lt;div&gt; element
<i> </i> if (d.offsetHeight) { divHeight = d.offsetHeight; }
<i> </i> else if (d.style.pixelHeight) { divHeight = d.style.pixelHeight; }
<i> </i> // calculate maximum height
<i> </i> maxHeight = Math.max(maxHeight, divHeight);
<i> </i> }
<i> </i> }

<i> </i> // assign maximum height value to all of container &lt;div&gt; elements
<i> </i> for (var i = 0; i &lt; contDivs.length; i++) {
<i> </i> contDivs[i].style.height = maxHeight+'px';
<i> </i> }
<i> </i> }

<i> </i> // execute function when page loads
<i> </i> window.onload = function() {
<i> </i> if (document.getElementsByTagName) { matchHeight(); }
<i> </i> }

<i> </i>&lt;/script&gt;

&lt;style type="text/css"&gt;
.small2col {
float: left;
width: 48&amp;#37;;
background-color: #EEE7DD;
border: solid 1px #D0B996;
font-size: 12px;
}
.small3col {
float: left;
width: 32%;
background-color: #EEE7DD;
border: solid 1px #D0B996;
font-size: 12px;
}
.small4col {
float: left;
width: 24%;
background-color: #EEE7DD;
border: solid 1px #D0B996;
font-size: 12px;
}
.smallcol {
float: left;
width: 24%;
background-color: #EEE7DD;
border: solid 1px #D0B996;
font-size: 16px;
}
.largecol {
float: left;
width: 72%;
background-color: #EEE7DD;
border: solid 1px #D0B996;
font-size: 24px;
}
.clearcols { clear:both; }
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;Start of equal column height displays
&lt;div&gt;
&lt;div class="small4col"&gt;Column 1&lt;/div&gt;
&lt;div class="small4col"&gt;Column 2&lt;p&gt;&amp;nbsp;&lt;p&gt;&amp;nbsp;&lt;br&gt;End of Column 2&lt;/div&gt;
&lt;div class="small4col"&gt;Column 3&lt;/div&gt;
&lt;div class="small4col"&gt;Column 4&lt;/div&gt;
&lt;div class="clearcols"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;3 column section&lt;/h3&gt;
&lt;div&gt;
&lt;div class="small3col"&gt;Column 1&lt;/div&gt;
&lt;div class="small3col"&gt;Column 2&lt;/div&gt;
&lt;div class="small3col"&gt;Column 3&lt;br&gt;
This would represent a section of the three column display that is extra long
and with multiple lines to display within the column. <br/>
&lt;p&gt;Want to see what effect
&lt;p&gt;it has on the other columns
&lt;p&gt;of the script.
&lt;/div&gt;
&lt;div class="clearcols"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;Mixed section (without and with equal height)&lt;/h3&gt;
&lt;div&gt;
&lt;div class="smallcol"&gt;Column 1&lt;/div&gt;
&lt;div class="largecol"&gt;Column 2&lt;/div&gt;
&lt;div class="clearcols"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div&gt;
&lt;div class="small2col"&gt;Column 1&lt;p&gt;&amp;nbsp;&lt;p&gt;&amp;nbsp;&lt;br&gt;End of Column 1&lt;/div&gt;
&lt;div class="small2col"&gt;Column 2&lt;p&gt;&amp;nbsp;&lt;p&gt;&amp;nbsp;&lt;br&gt;End of Column 2&lt;/div&gt;
&lt;div class="clearcols"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h1&gt;End of four, three &amp;amp; mixed two column displays&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;

Still a neat bit of JS and CSS ?
Copy linkTweet thisAlerts:
@SuperTalauthorApr 23.2009 — Thank you, got it working but then decided to go with using jQuery instead and came up with this:

[CODE] <script src="Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>

<script type="text/javascript">

//Function to loop through each item with passed in Class
function equalHeight(group) {
tallest = 0;
group.each(function() {
thisHeight = $(this).height();
if (thisHeight > tallest) {
tallest = thisHeight + 5;
}
});
group.height(tallest);
}

//Code to call equalHeight passing in Wildcard col because different width
//columns get generated (colSmall, colMedium, colLarge)
$(document).ready(function() {
equalHeight($("[class^=col]"));
});
</script>[/CODE]


JMRKER: You can hard code a max height in your style sheet but I don't believe this would be effective when this script runs since they will all have the same height.
Copy linkTweet thisAlerts:
@JMRKERApr 23.2009 — 
JMRKER: You can hard code a max height in your style sheet but I don't believe this would be effective when this script runs since they will all have the same height.[/QUOTE]


No, I don't need to hard code the maximum height.

In the version I posted, maximum height was calculated and became set to the max height

of ANY of the columns. I was looking for a way to determine the maximum height for a

particular column class (small4col, small3col, small2col, etc).
×

Success!

Help @SuperTal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...