473,792 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Switch if contents of a container overflow out of it

6 New Member
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.off setHeight) doesn't seem correct.
Any help would be greatly appreciated -- thanks!
Sep 27 '07 #1
2 2152
ge5talt
6 New Member
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.getEl ementById('tabl eless')) {
var allDivs = document.getEle mentsByTagName( 'div');
for (var i=0; i < allDivs.length; i++) {
if (allDivs[i].className=='co ntentContainer' ) {
if (allDivs[i].firstChild && allDivs[i].firstChild.scr ollHeight) {
if (allDivs[i].firstChild.off setHeight > allDivs[i].offsetHeight) {
document.getEle mentById('table less').style.he ight = 'auto';
break;
}
else if (allDivs[i].firstChild.off setHeight <= allDivs[i].offsetHeight) {
if (document.getEl ementById('tabl eless').style.h eight == 'auto') {
document.getEle mentById('table less').style.he ight = '88%';
adjustHeight();
}
}
}
}
}
}
}

function slowResize () {
clearTimeout(ti medEvent);
timedEvent = setTimeout('adj ustHeight()', 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="contentC ontainer" 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="contentC ontainer" 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="contentC ontainer" 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
ge5talt
6 New Member
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
13565
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 scrollbar in firefox/netscape (not IE) , even though the text I have in the element does not exceed the bounds. The simple example below casues this behaviour. How do I fix this? <div style="overflow:auto; height:100px; width:100px;...
3
5096
by: Robert | last post by:
Is it possible to create a Panel with a vertical scrollbar? Thanks Rob
2
1635
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 not open in design view. Place quotes around a '<% %>' used as an attribute value or within a <SELECT> element. well, actually I used <% %> as an attribute value, why I shouldn't? would you suggest an alternative?
1
5935
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 flot positional paremter a lot on containers, and it really seems to do what I want. Except when I put other containers in these containers, they don't expand vertically like I want. Here's the stylesheet:
14
14287
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! ------------------------------------------------ - head - ------------------------------------------------ - - - - m - - - e - -
9
4035
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 element with the javascript contents. But when viewed as a seperate HTML file with the below contents I'm able to see the milonic menu http://www.milonic.com . <div>
5
2759
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 scrolls horizontally which is as it should be. In IE, however, the Container scrolls and I can't seem to find a way to make the picture DIV scroll instead. The page is http://cristimodels.com/catalogue/item.asp?fIndex=2 CSS:
2
3894
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. Anyone willing to offer a hint or suggestion as to why the outer container retains the width of the inner container, instead of auto-clearing both sides and instead of growing to the size of the container holding everything? <div id="container"><!--...
12
3788
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 complete - and is fairly simple. there is one area of trouble though. the #container on the site is set to height 100% but, in FF it is resetting itself to 551px! The site in question is www.vardyfoundation.com and if you load this in IE7 and look...
0
9670
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10211
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9033
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3719
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.