473,395 Members | 2,006 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,395 software developers and data experts.

Setting the height of two elements to match each other


Hi all,

I want to write a function that given two elements will make their
heights equal to the larger element. I've had a go at it and it works,
but it makes both their heights larger than either of them (but equal)

The code is as follows:

var newheight = (second.offsetHeight > first.offsetHeight) ?
second.offsetHeight :
first.offsetHeight;

first.style.height = newheight + "px";
second.style.height = newheight + "px";
So, I'm obviously doing something stupid, but can anyone enlighten me?
I tried writing to offsetHeight but the changes seemed to be ignored.

Cheers,
Andy
Sep 26 '05 #1
12 2521
Andy Jeffries wrote:

Hi all,

I want to write a function that given two elements will make their
heights equal to the larger element. I've had a go at it and it works,
but it makes both their heights larger than either of them (but equal)

The code is as follows:

var newheight = (second.offsetHeight > first.offsetHeight) ?
second.offsetHeight :
first.offsetHeight;
Try: alert(second.offsetHeight);alert(first.offsetHeigh t);alert(newheight)
Mick

first.style.height = newheight + "px";
second.style.height = newheight + "px";
So, I'm obviously doing something stupid, but can anyone enlighten me? I
tried writing to offsetHeight but the changes seemed to be ignored.

Cheers,
Andy

Sep 26 '05 #2
Mick White wrote:
I want to write a function that given two elements will make their
heights equal to the larger element. I've had a go at it and it
works, but it makes both their heights larger than either of them (but
equal)

The code is as follows:

var newheight = (second.offsetHeight > first.offsetHeight) ?
second.offsetHeight :
first.offsetHeight;

Try: alert(second.offsetHeight);alert(first.offsetHeigh t);alert(newheight)


OK, I get:

123
153
153

If I put the same alerts in after:

first.style.height = newheight + "px";
second.style.height = newheight + "px";

I get:

162
162
153

So, that's my problem. They are now equal in height but as my query
said it makes both of them larger than either was before.

Cheers,
Andy
Sep 26 '05 #3
Andy Jeffries wrote:

Hi all,

I want to write a function that given two elements will make their
heights equal to the larger element. I've had a go at it and it works,
but it makes both their heights larger than either of them (but equal)


Asking this in a CSS forum, I'm sure they'll have a way to do this (or
at least make it look like it's happening) without any scripting.

Try:

comp.infosystems.www.authoring.stylesheets
--
Rob
Sep 26 '05 #4


Andy Jeffries wrote:

So, that's my problem. They are now equal in height but as my query
said it makes both of them larger than either was before.


The CSS height is not necessarily the same as the offsetHeight, the CSS
height defines the height of the content box and offsetHeight includes
any padding or border space.

Thus if you have
height: 200px
padding: 2px
border-width: 1px
then you could have offsetHeight as
200 + 2 * 2 + 2 * 1 = 206
and if you then set
height: 206px
and don't change the padding and border then you get offsetHeight as
206 + 2 * 2 + 2 * 1 = 212
for instance.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 26 '05 #5
Martin Honnen wrote:
So, that's my problem. They are now equal in height but as my query
said it makes both of them larger than either was before.


The CSS height is not necessarily the same as the offsetHeight, the CSS
height defines the height of the content box and offsetHeight includes
any padding or border space.

Thus if you have
height: 200px
padding: 2px
border-width: 1px
then you could have offsetHeight as
200 + 2 * 2 + 2 * 1 = 206
and if you then set
height: 206px
and don't change the padding and border then you get offsetHeight as
206 + 2 * 2 + 2 * 1 = 212
for instance.


OK.

I've looked at the Javascript object using the DOM in Mozilla and
apparently there's no padding or border on the object (which sounds
right) but the assigned height still differs by 9px.

I can fudge this, but I'd really rather understand what's going on.

Cheers,
Andy
Sep 26 '05 #6
Andy Jeffries wrote:
Martin Honnen wrote:
So, that's my problem. They are now equal in height but as my query
said it makes both of them larger than either was before.

The CSS height is not necessarily the same as the offsetHeight, the
CSS height defines the height of the content box and offsetHeight
includes any padding or border space.

Thus if you have
height: 200px
padding: 2px
border-width: 1px
then you could have offsetHeight as
200 + 2 * 2 + 2 * 1 = 206
and if you then set
height: 206px
and don't change the padding and border then you get offsetHeight as
206 + 2 * 2 + 2 * 1 = 212
for instance.

OK.

I've looked at the Javascript object using the DOM in Mozilla and
apparently there's no padding or border on the object (which sounds
right) but the assigned height still differs by 9px.

I can fudge this, but I'd really rather understand what's going on.


And the best bit is, that it doesn't work for one set (expands them by
9px) but my original code does work perfectly for another.

The code I put in is (to see if it worked before doing the same for
border*):

if (first.style.paddingTop) {
newheight = newheight - parseInt(first.style.paddingTop);
}
if (first.style.paddingBottom) {
newheight = newheight - parseInt(first.style.paddingBottom);
}

Cheers,
Andy
Sep 26 '05 #7

Andy Jeffries wrote:
I've looked at the Javascript object using the DOM in Mozilla and
apparently there's no padding or border on the object (which sounds
right) but the assigned height still differs by 9px.


Make a minimal test case with valid HTML and CSS and post a URL. If
there is any script to be applied please make a button that calls the
script so that we can look at the static document first in DOM inspector
for instance and then see the result of your script when the button is
clicked.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 26 '05 #8
Martin Honnen wrote:

Andy Jeffries wrote:
I've looked at the Javascript object using the DOM in Mozilla and
apparently there's no padding or border on the object (which sounds
right) but the assigned height still differs by 9px.

Make a minimal test case with valid HTML and CSS and post a URL. If
there is any script to be applied please make a button that calls the
script so that we can look at the static document first in DOM inspector
for instance and then see the result of your script when the button is
clicked.


http://www.andyjeffries.co.uk/temp/testcase/

Cheers,
Andy
Sep 26 '05 #9


Andy Jeffries wrote:

Andy Jeffries wrote:
I've looked at the Javascript object using the DOM in Mozilla and
apparently there's no padding or border on the object (which sounds
right) but the assigned height still differs by 9px.
http://www.andyjeffries.co.uk/temp/testcase/


So you are looking at the first inner <div> element in <div
class="first"> respectively <div class="second">?

DOM inspector says that line 221 in main.css gives
padding-top: 4px
padding-bottom: 4px
border-bottom-width: 1px
So that gives you the 9px difference, offsetHeigth is e.g. 128, computed
CSS height is 119px, if you now set the CSS height to 128 the padding
and border is added to the offsetHeight and you get the new offsetHeight
128 + 9 = 137.

That is exactly what I already told you, the CSS padding and/or border
is added to the CSS height/width when offsetWidth/offsetHeigth is computed.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 26 '05 #10
Martin Honnen wrote:


Andy Jeffries wrote:

Andy Jeffries wrote:

I've looked at the Javascript object using the DOM in Mozilla and
apparently there's no padding or border on the object (which sounds
right) but the assigned height still differs by 9px.

http://www.andyjeffries.co.uk/temp/testcase/

So you are looking at the first inner <div> element in <div
class="first"> respectively <div class="second">?

DOM inspector says that line 221 in main.css gives
padding-top: 4px
padding-bottom: 4px
border-bottom-width: 1px
So that gives you the 9px difference, offsetHeigth is e.g. 128, computed
CSS height is 119px, if you now set the CSS height to 128 the padding
and border is added to the offsetHeight and you get the new offsetHeight
128 + 9 = 137.


Doh <embarressed> thanks for that.
That is exactly what I already told you, the CSS padding and/or border
is added to the CSS height/width when offsetWidth/offsetHeigth is computed.


But I posted back in a message* that I'd tried that and it didn't work
(at least in Javascript). I can fudge it, but I'd rather ensure the
code is neat.

Any idea what the problem is with the code below?

Cheers,
Andy

* <Cv********************@fe07.news.easynews.com>

if (first.style.paddingTop) {
newheight = newheight - parseInt(first.style.paddingTop);
}
if (first.style.paddingBottom) {
newheight = newheight - parseInt(first.style.paddingBottom);
}
Sep 27 '05 #11


Andy Jeffries wrote:

That is exactly what I already told you, the CSS padding and/or border
is added to the CSS height/width when offsetWidth/offsetHeigth is
computed.

But I posted back in a message* that I'd tried that and it didn't work
(at least in Javascript). I can fudge it, but I'd rather ensure the
code is neat.

Any idea what the problem is with the code below?

if (first.style.paddingTop) {
newheight = newheight - parseInt(first.style.paddingTop);
}
if (first.style.paddingBottom) {
newheight = newheight - parseInt(first.style.paddingBottom);
}


CSS is more complex than you seem to think and scripting it is more
complex too than your attempt above.
first.style.cssPropertyName
in DOM scripting where first is an element object is not in any way the
computed style value, it is the inline style of the element that you can
set with the inline style attribute or with script and the style
property. So your expression
first.style.paddingTop
reads the inline style and thus gives you a value other than the empty
string only if you have an inline style attribute e.g.
<div style="padding-top: 2px;">
in the HTML markup or your script has already set
first.style.paddingTop
to a value.

If you have CSS rules in a stylesheet (either an embedded stylesheet
(e.g. <style type="text/css">) or an external stylesheet linked in (e.g.
<link rel="stylesheet" type="text/css" href="file.css">) then reading
first.style.paddingTop
does not in any way help to reveal whether any stylesheet rules set a
padding on the element.

If you want a computed CSS value on an element then IE supports
element.currentStyle and for DOM Level 2 compliant browsers like Mozilla
you need to use document.defaultView.getComputedStyle as in

function getComputedStyleValue (element, cssPropertyName) {
var ownerDocument, defaultView;
if ((ownerDocument = element.ownerDocument) &&
(defaultView = ownerDocument.defaultView) &&
defaultView.getComputedStyle)
{
return defaultView.getComputedStyle(element, '')[cssPropertyName];
}
else if (element.currentStyle) {
return element.currentStyle[cssPropertyName];
}
}

var testElements = [document.documentElement, document.body];
var testProperties = ['marginLeft', 'paddingLeft'];

var results = '';

for (var i = 0; i < testElements.length; i++) {
var element = testElements[i];
results += 'element ' + element.tagName + ':\r\n';
for (var j = 0; j < testProperties.length; j++) {
var property = testProperties[j];
results += ' ' + property + ': ' + getComputedStyleValue(element,
property) + '\r\n';
}
results += '\r\n';
}

alert(results);


--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 27 '05 #12
Martin Honnen wrote:
CSS is more complex than you seem to think and scripting it is more
complex too than your attempt above.
I understand CSS fine, I'm absolutely new to DOM scripting (having
absolutely avoided and slated it in the past, I've recently approached
it with an open mind and DHTML Utopia really turned me into a believer).
first.style.cssPropertyName
in DOM scripting where first is an element object is not in any way the
computed style value, it is the inline style of the element that you can
set with the inline style attribute or with script and the style
property.
Ahhh, OK. I thought it would be the computed final style, in the same
way offsetHeight is the final computed height.
If you want a computed CSS value on an element then IE supports
element.currentStyle and for DOM Level 2 compliant browsers like Mozilla
you need to use document.defaultView.getComputedStyle as in


Fantastic, thanks for the code snippet. I'll have a crack with this.

Cheers,
Andy
Sep 27 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MyndPhlyp | last post by:
I've been busting my head trying to figure this out for quite some time. With IE6 and NS7, no problems. I can simply code the HTML <img height="100%"> and be done with it. But NS4 and NS6 (and...
12
by: Jacob Weber | last post by:
Hello. Is it possible to specify the exact space between two lines, measured from the baseline of the top line to the ascenders of the second line? I tried adding the space with padding-bottom,...
7
by: nntp | last post by:
I am making two tables side by side. I need talble's each elements to match table two's. So the <td> in both must have the same heights. However. one talble's content's length is unknown under...
9
by: netclectic | last post by:
I'm dynamically adding options to a select list in javascript and i need to be able to set the height of the option, but setting style.height has not effect, I also tried style.pixelHeight but no...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
22
by: Les Juby | last post by:
I am trying to adjust the window/table size of a website (www.worklaw.co.za) which has made use of DIV tags with its settings embedded in an CSS file. The client wants its width and height to...
1
by: Roland Dick | last post by:
Hello, I have a problem here with an ASP.NET website. The pages are supposed to use a single master page which just contains some images and a menu control on the top, below that is the content...
0
by: erdavila | last post by:
With a code like this: <table> <tr> <td> <div style="height: 7px; overflow: hidden;"> Cell A </div> </td> <td>
19
by: david.karr | last post by:
If in my CSS I set the "background-color" property on the "body" element, it only covers the background of the elements defined in the body, up to the current width and height of the page. However,...
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
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.