I had such a problem with IE (as usual). I had a column with dropdown lists and all these lists had different width. My layout required that all of these elements had the same width.
First of all I tried to create a table with one column and place all my elements into cells. After that I set style="width: 100%" for all content of the table. It worked ideally in all browsers. Except one... Internet Explorer compressed my table to several pixels. As I understood he realized that width of my table depends on elements and width of elements depends on table. This recursion made him crazy.
I thought that if it knew actual width of table, he would scale right. So I removed "width" from tags attributes and added JavaScript in the end of the page (after load and automatic scaling) and added "width=100%" through this script (when table width is known for a browser). The same result...
My solution was to add two lines to the script before setting elements width. I got the first cell as element by ID and then changed it's width from "auto" to it's actual current width. It worked in IE, Firefox, Opera, Chrome and Safari.
Here is the example and solution:<form name="myform">
<Table cellspacing="5" cellpadding="0">
<TR><TD id="firstcell">
<select name="list1">
<option>opt1</option>
<option>opt2</option>
</select>
</TD></TR>
<TR><TD>
<select name="list2">
<option>my_opt1</option>
<option>my_opt2</option>
</select>
</TD></TR>
</Table>
</form>
<Script language="JavaScript">
var firstcell = document.getElementById('firstcell');
firstcell.style.width = firstcell.offsetWidth;
document.myform.list1.style.width = '100%';
document.myform.list2.style.width = '100%';
</Script>
A bit about
Hello, everyone! All you can see below is just my bank of information. Some material I've found in the fathomless net, some I've learned myself. Don't think all of the information here is right or actual, but may be it could be of use for you :) All feedback is welcome, especially constructive ones :)
Wednesday, February 25, 2009
Web: Stretch all elements to the width of the longest one
Labels:
experience,
howto,
HTML,
JavaScript,
Web
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment