Is it possible to set the height/width of an element in percent using JavaScript or jQuery?
+1
A:
Yes, it is:
<div id="myid">Some Content........</div>
document.getElementById('#myid').style.width = '50%';
Sarfraz
2010-03-17 08:11:47
+1
A:
document.getElementById('header').style.width = '50%';
If you are using Firebug or the Chrome/Safari Developer tools, execute the above in the console, and you'll see the Stack Overflow header shrink by 50%.
Daniel Vassallo
2010-03-17 08:12:37
+2
A:
The question is what do you want the div's height/width to be a percent of?
By default, if you assign a percentage value to a height/width it will be relative to it's direct parent dimensions. If the parent doesn't have a defined height, then it won't work.
So simply, remember to set the height of the parent, then a percentage height will work via the css attribute:
obj.style.width = '50%';
Nick Meldrum
2010-03-17 09:05:09