473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Switch if contents of a container overflow out of it

Hi all,

I am looking for a switch that will style the height of a parent element to a specific % if the height of the contents is less than the height of the container (it doesnt spill out), or height: auto if the contents does try to spill out, and then maybe somehow back again?

In pseudocode..


Expand|Select|Wrap|Line Numbers
  1. if (contents.height > container.height) {
  2. currentPercentHeight = container.style.height; /* store in memory */
  3. container.style.height = 'auto';
  4.  
  5. } else if (container.height >= contents.height) {
  6. container.style.height = currentPercentHeight;
Here is my first go at it. This function fires on load and on resize with a delay so it doesnt take heaps of resources if on Windows the window is resized continuously while dragging.


Expand|Select|Wrap|Line Numbers
  1. /* stores current heights */
  2. var currentHeights = new Array();
  3.  
  4. function HeightSwitch () {
  5. /* If this page has id="tableless", I want to run this function */
  6.     if (document.getElementById('tableless')) {
  7. /* get containers */
  8.         var allDivs = document.getElementsByTagName('div');
  9.         for (var i=0; i < allDivs.length; i++) {
  10.             if (allDivs[i].parentNode) {
  11.                 if (allDivs[i].offsetHeight && allDivs[i].firstChild.offsetHeight) {
  12.                 if (allDivs[i].firstChild.offsetHeight > allDivs[i].offsetHeight) {
  13.                         currentHeights[i] = allDivs[i].parentNode.style.height;
  14.                     allDivs[i].parentNode.style.height = 'auto';
  15.                     } else if (allDivs[i].offsetHeight >= allDivs[i].firstChild.offsetHeight) {
  16.                          allDivs[i].parentNode.style.height = currentHeights[i];
  17.                                          }
  18.                 }
  19.             }
  20.  
  21.         }
  22.     }
My current guess at the height of the content (allDivs[i].firstChild.offsetHeight) doesn't seem correct.
Any help would be greatly appreciated -- thanks!
Sep 27 '07 #1
2 2123
As an update, I've gotten a function (adjustHeight) that works pretty well within IE7 and doesn't need to work in IE6 since contents cant spill out of divs under that rendering.

It's a little simpler. Below is my code. Maybe someone knows why Firefox isn't interpreting it as I expect? I don't think any DOM properties I use are unsupported (I've used them in other ways successfully) so I'm not sure what's up.


[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<script type="text/javascript">

var timedEvent = null;

function adjustHeight() {

if (document.getElementById('tableless')) {
var allDivs = document.getElementsByTagName('div');
for (var i=0; i < allDivs.length; i++) {
if (allDivs[i].className=='contentContainer') {
if (allDivs[i].firstChild && allDivs[i].firstChild.scrollHeight) {
if (allDivs[i].firstChild.offsetHeight > allDivs[i].offsetHeight) {
document.getElementById('tableless').style.height = 'auto';
break;
}
else if (allDivs[i].firstChild.offsetHeight <= allDivs[i].offsetHeight) {
if (document.getElementById('tableless').style.height == 'auto') {
document.getElementById('tableless').style.height = '88%';
adjustHeight();
}
}
}
}
}
}
}

function slowResize () {
clearTimeout(timedEvent);
timedEvent = setTimeout('adjustHeight()', 80);
}

window.onload = adjustHeight;
window.onresize = slowResize;

</script>

<style type="text/css">

html, body {
height: 96%;
}

body {
background-color: gray;
}

div.inner {
width: 92%;
height: 88%;
padding: 0px;
margin: 4%;
background-color: white;
}

</style>

<title>height adjust</title>
</head>
<body>

<div class="inner" id="tableless">
<div class="contentContainer" style="height: 40%; background-color: orange;">
<div>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras gravida ipsum sed sem. Praesent orci velit, rhoncus ut, ullamcorper a, blandit quis, nibh. Mauris et felis aliquam pede bibendum consectetuer. Maecenas enim risus, auctor tempor, tempus vel, laoreet volutpat, ipsum. Morbi varius arcu in pede. Fusce pretium, quam eget consequat gravida, augue odio sollicitudin augue, eget lacinia purus augue at lorem. Mauris ullamcorper enim a magna. Donec metus. Pellentesque ullamcorper. Nullam eget dui. Etiam sed quam a est adipiscing tincidunt. Ut libero.
</div>
</div>
<div class="contentContainer" style="height: 25%; background-color: aqua;">
<div>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras gravida ipsum sed sem. Praesent orci velit, rhoncus ut, ullamcorper a, blandit quis, nibh. Mauris et felis aliquam pede bibendum consectetuer. Maecenas enim risus, auctor tempor, tempus vel, laoreet volutpat, ipsum. Morbi varius arcu in pede. Fusce pretium, quam eget consequat gravida, augue odio sollicitudin augue, eget lacinia purus augue at lorem. Mauris ullamcorper enim a magna. Donec metus. Pellentesque ullamcorper. Nullam eget dui. Etiam sed quam a est adipiscing tincidunt. Ut libero.
</div>
</div>
<div class="contentContainer" style="height: 25%; background-color: pink;">
<div>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras gravida ipsum sed sem. Praesent orci velit, rhoncus ut, ullamcorper a, blandit quis, nibh. Mauris et felis aliquam pede bibendum consectetuer. Maecenas enim risus, auctor tempor, tempus vel, laoreet volutpat, ipsum. Morbi varius arcu in pede. Fusce pretium, quam eget consequat gravida, augue odio sollicitudin augue, eget lacinia purus augue at lorem. Mauris ullamcorper enim a magna. Donec metus. Pellentesque ullamcorper. Nullam eget dui. Etiam sed quam a est adipiscing tincidunt. Ut libero.
</div>
</div>
</div>

</body>
</html>[/HTML]


Here is my function, outside of HTML:

Expand|Select|Wrap|Line Numbers
  1. function adjustHeight() {
  2.  
  3.     if (document.getElementById('tableless')) {
  4.         var allDivs = document.getElementsByTagName('div');
  5.         for (var i=0; i < allDivs.length; i++) {
  6.         if (allDivs[i].className=='contentContainer') {
  7.             if (allDivs[i].firstChild && allDivs[i].firstChild.scrollHeight) {
  8.                 if (allDivs[i].firstChild.offsetHeight > allDivs[i].offsetHeight) {
  9.                     document.getElementById('tableless').style.height = 'auto';
  10.                     break;
  11.                     }
  12.                 else if (allDivs[i].firstChild.offsetHeight <= allDivs[i].offsetHeight) {
  13.                     if (document.getElementById('tableless').style.height == 'auto') {
  14.                         document.getElementById('tableless').style.height = '88%';
  15.                         adjustHeight();
  16.                         }
  17.                     }
  18.                 } 
  19.             }
  20.         }
  21.     }
  22. }
Sep 27 '07 #2
Maybe someone knows why Firefox isn't interpreting it as I expect? I don't think any DOM properties I use are unsupported (I've used them in other ways successfully) so I'm not sure what's up.
I think I may have answered my own question just now:

Gecko-based browsers insert text nodes into a document to represent whitespace in the source markup. Therefore a node obtained for example via firstChild or previousSibling may refer to a whitespace text node, rather than the actual element the author intended to get.

See Whitespace in the DOM and W3C DOM 3 FAQ: Why are some Text nodes empty? for more information.
Sep 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rob T. | last post by:
I am using a div tag to make an element that contains some text. I have specified overflow: auto; in my stylesheet. If I use font-size/line-height to specify the font information I get a vertical...
3
by: Robert | last post by:
Is it possible to create a Panel with a vertical scrollbar? Thanks Rob
2
by: Saber | last post by:
I've a repeater and here is a piece of HTML when I use, I can't switch back to Design view: <a href=default.aspx?id=<%#container.dataitem("id")%>Some Link</a> I get this error message: Could...
1
by: Miked | last post by:
Hello: I'm relatively new to CSS, and I'm doing a site where I don't want to use any tables. I've gotten pretty far, and the site has the layout I want. My only problem is that I'm using the...
14
by: MrPutty | last post by:
Hello everyone! It seems I've finaly found a place with savy folks ;-) The following problem has me puzzeled - hopefully someone can help me out! ...
9
by: mevryck | last post by:
Greetings I have a huge Javascript with inclusion of external scripts and all. I got this by doing a XSLT . Now I have the contents in a Javascript variable, but I'm not able to update the...
5
by: arathra | last post by:
Hi, hope someone can help with this. It's driving me mad. I have a container inside which is another DIV which is filled with pictures and then another DIV with some text. In FF the picture DIV...
2
by: Jim Carlock | last post by:
I ran into a problem a think. Either that or I overlooked something. I seem to run into this problem quite often. I fail to pinpoint the problem and it appears as a bug with mozilla browsers....
12
nathj
by: nathj | last post by:
Hi, I have been working on a redesign of a site. this is a site that was developed by someone else and the CSS has been built to fit with the CMS that they supplied. This redesign is nearly...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.