offsetHeight of children not reflected on container

S

steveh

Hi,

I am basically trying to find the location and size of an element on
the page, and draw a div on top with the identical size and location
(with some transparency), so it can be highlighted and clicked. Now I
*thought* I had managed the width/height using offsetWidth/
offsetHeight since when I initially tested it (using basic text) in
IE6 and Firefox 2, it worked fine.

However I have justed tested it with child elements (see code below),
and whilst it still works in IE6, Firefox doesn't return the value I
expect. It appears that if the height of a child element is larger
then the container, it is not reflected in the container's
offsetHeight.

Some elements may have a height and width defined in their style, so
that's not a problem. But not all will, so I really need a way to get
the browsers rendered height for an element.

The demo code I am using is below- and yes, its a rough example ;)

<html>
<head>
<script>

function Start()
{
var containerHeight =
document.getElementById('region1').offsetHeight;
var childHeight =
document.getElementById('region1').childNodes[0].offsetHeight;

var info = document.getElementById('info');

info.innerHTML = 'Container offsetHeight: ' + containerHeight + '<br/
' + 'Child offsetHeight: ' +

childHeight;
}

</script>
</head>
<body onload="Start();">
<br/><br/>
<span id="region1"><span style="font-size: 50px;">Test text</span></
span>
<br/>
<br/>
<span id="info" runat="server">
</span>
</body>
</html>

So my questions are:

- What should the behaviour be. Is IE or Firefox correct?
- Is there a way around it, to get the actual 'height'?*

*I know if there was a single child element, I could just get that
offsetHeight - but there could potentially be many, and they may
contain children too - so this isn't really an option.

Thanks

Steve
 
S

steveh

[height of the child element not reflected in the offsetHeight of
the parent element]
The demo code I am using is below- and yes, its a rough example ;)
[invalid markup]

http://diveintomark.org/archives/2003/05/05/why_we_wont_help_you

Also, post the client-side code if you use client-side properties
(as here), not the server-side code (as indicated by runat="server").

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <[email protected]>

Sorry. The page in my main project is using valid markup. I just
created the demo content quickly and dirty.

However, I have reworked it so it is now valid:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Demo</title>

<script type="text/javascript">

function Start()
{
var containerHeight =
document.getElementById('region1').offsetHeight;
var childHeight =
document.getElementById('region1').childNodes[0].offsetHeight;

var info = document.getElementById('info');

info.innerHTML = 'Container offsetHeight: ' + containerHeight
+ ' / ' + 'Child offsetHeight: ' + childHeight;
}

</script>
</head>
<body onload="Start();">
<p>
<span id="region1"><span style="font-size: 50px;">Test text</span></
span>
</p>
<p>
<span id="info"></span>
</p>
</body>
</html>

The same problem is still present, however.
 
T

Thomas 'PointedEars' Lahn

[height of the child element not reflected in the offsetHeight of
the parent element]
The demo code I am using is below- and yes, its a rough example ;)
[invalid markup]
http://diveintomark.org/archives/2003/05/05/why_we_wont_help_you

Also, post the client-side code if you use client-side properties
(as here), not the server-side code (as indicated by runat="server").
[...]

Please trim your quotes.

http://www.jibbering.com/faq/faq_notes/clj_posts.html
However, I have reworked it so it is now valid:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Code should be posted so that automatic wrapping at about 76 characters per
line does not render it syntactically invalid.

Valid does not necessarily mean XHTML, and vice-versa.
<head>
<title>Demo</title>

<script type="text/javascript">

Note that the XHTML script element has the content model PCDATA, so you have
function Start()

Only identifiers of constructors should start with a capital letter.
{
var containerHeight =
document.getElementById('region1').offsetHeight;
var childHeight =
document.getElementById('region1').childNodes[0].offsetHeight;

var info = document.getElementById('info');

info.innerHTML = 'Container offsetHeight: ' + containerHeight
+ ' / ' + 'Child offsetHeight: ' + childHeight;
}

</script>
</head>
<body onload="Start();">
<p>
<span id="region1"><span style="font-size: 50px;">Test text</span></
span>
</p>
[...]

The same problem is still present, however.

`span' elements are inline-level elements. If you format the outermost
`span' element display:block or if you use a `div' element which is a
block-level element, the height of the child element affects the value
of the offsetHeight property of the parent.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top