Click to See Complete Forum and Search --> : Can I redefine a style at runtime?


GuyWithDogs
02-15-2005, 03:11 PM
Is it possible to redefine a style at runtime?

For example, assume I have a definition

img {
height: 100%;
}

When a page loads, I'd like to redefine the images to be 75% of the available body height.

I created a routine that I execute in the onload() event to change each image to a calculated height - I'm wondering whether there's a better, or more generic, way to do this.

Fang
02-16-2005, 03:13 AM
Changing the rules directly is possible, but full of incompatibilities (http://www.quirksmode.org/dom/w3c_css.html).

Better is creating an array of the element then changing each property value:
onload=function() {
var aImg=document.getElementsByTagName('img');
for(var i=0; i<aImg.length; i++) {
aImg[i].style.width=someValue;
}
}

GuyWithDogs
02-16-2005, 10:56 AM
Thanks for the info. There's a whole whack of things I'd never heard of in there.

I had set up a loop to do the adjustment, as you suggested. I'll probably leave it at that - why break a working program? I'm sure the next change I make will introduce its own set of problems...