473,791 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.offsetH eight > first.offsetHei ght) ?
second.offsetHe ight :
first.offsetHei ght;

first.style.hei ght = newheight + "px";
second.style.he ight = 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
12 2553
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.n ews.easynews.co m>

if (first.style.pa ddingTop) {
newheight = newheight - parseInt(first. style.paddingTo p);
}
if (first.style.pa ddingBottom) {
newheight = newheight - parseInt(first. style.paddingBo ttom);
}
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.pa ddingTop) {
newheight = newheight - parseInt(first. style.paddingTo p);
}
if (first.style.pa ddingBottom) {
newheight = newheight - parseInt(first. style.paddingBo ttom);
}


CSS is more complex than you seem to think and scripting it is more
complex too than your attempt above.
first.style.css PropertyName
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.pad dingTop
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.pad dingTop
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.pad dingTop
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.current Style and for DOM Level 2 compliant browsers like Mozilla
you need to use document.defaul tView.getComput edStyle as in

function getComputedStyl eValue (element, cssPropertyName ) {
var ownerDocument, defaultView;
if ((ownerDocument = element.ownerDo cument) &&
(defaultView = ownerDocument.d efaultView) &&
defaultView.get ComputedStyle)
{
return defaultView.get ComputedStyle(e lement, '')[cssPropertyName];
}
else if (element.curren tStyle) {
return element.current Style[cssPropertyName];
}
}

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

var results = '';

for (var i = 0; i < testElements.le ngth; 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 + ': ' + getComputedStyl eValue(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.css PropertyName
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.current Style and for DOM Level 2 compliant browsers like Mozilla
you need to use document.defaul tView.getComput edStyle 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
2856
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 probably a couple of other IE and NS versions I can't get to right now) don't want to play nice unless I hard code the image height. (Yes, I'm one of those who insists on still coding for NS4.)
12
3226
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, but it adds it below the descenders. So the actual space between lines is larger than I specified. Jacob
7
3287
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 dynamic content is made. I tried to use <td height=xxx> but it does not force the height, but only set the minimal height. I want to know if there is a way to limit the height. or even the length of the content, so if it has 1000 charater long,...
9
2566
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 joy. i'm doing something like this (: var selectControl = document.getElementById('MySelect'); var el = document.createElement('option'); el.value = "some value";
21
3993
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 nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
22
6976
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 adjust according to dynamic screen size, and I have been able to adjust the width to a percentage value which works fine. But if I try do the same with the height it shows some very strange results squashing the field to display about 3 lines.
1
1709
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 placeholder. What I would like to achieve is to always see the master page elements (the images and the menu control). In other words, the content placeholder should display a scrollbar, not the whole page.
0
2228
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
3990
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, if the current component set has a resulting small height, this leaves most of the page with the original background color (white). However, if I set the property on the "html" element instead, it does color the entire visible page, beyond the...
0
9669
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
10207
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
9993
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
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
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.