Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: not2easy

Message Too Old, No Replies

Set a table columns width

     

Rain_Lover

6:34 pm on Mar 9, 2012 (gmt 0)

5+ Year Member Top Contributors Of The Month



Hi,

I tried to do it by adjusting the <td> width:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sample Page</title>
<style type="text/css">
div {width:100%; overflow:auto;}
table, td {border:1px solid #000;}
td {width:1000px;}
</style>
</head>
<body>
<div>
<table>
<tr>
<td>First cell</td>
<td>Second cell</td>
<td>Third cell</td>
</tr>
<tr>
<td>Forth cell</td>
<td>Fifth cell</td>
<td>Sixth cell</td>
</tr>
</table>
</div>
</body>
</html>


But it didn't work.

Many thanks for any help!
Mike

not2easy

7:04 pm on Mar 9, 2012 (gmt 0)

WebmasterWorld Administrator 5+ Year Member Top Contributors Of The Month



td {width:1000px;}

You have set the <td> width, but 1000px is too wide. What is the purpose of the <div> surrounding the table? Do you want all cells in your table to be the same width? Change it to: td {width:33%;}

birdbrain

8:17 pm on Mar 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Rain Lover,

if you check the table offsetWidth with javascript, you will see that it has a width of 3022px.

Setting the div container width to 100% is pointless as that is it's default width.

It must be set to the table width.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sample Page</title>
<style type="text/css">
div {width:3022px;}
table, td {border:1px solid #000;}
td {width:1000px;}
</style>
<script type="text/javascript">


function init(){
alert('overall table width='
+document.getElementsByTagName('table')[0].offsetWidth+'px');
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>

</head>
<body>
<div>
<table>
<tr>
<td>First cell</td>
<td>Second cell</td>
<td>Third cell</td>
</tr>
<tr>
<td>Forth cell</td>
<td>Fifth cell</td>
<td>Sixth cell</td>
</tr>
</table>
</div>
</body>
</html>

coothead
 

Featured Threads

Hot Threads This Week

Hot Threads This Month