views:

43

answers:

2

Hey,

This is my first question here so let me know if i have not understood the site ethos ;)

I have written a html page for showing and hiding nested tables. I would like to get the columns to align correctly. I have got close by setting the columns to have a specific width

in both IE and firefox the columns are a few pixels out... - how can i fix this?

<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>item List</title>
<script type="text/javascript">
      <!--
          function toggle_visibility(id) {
             var e = document.getElementById(id);
             if(e.style.display == '') {
               e.style.display = 'block';
             }
             if(e.style.display == 'block')
                e.style.display = 'none';
             else
                e.style.display = 'block';
          }
      //-->
    </script>
</head>

<body>

<div id="root">
  <table border="1" cellspacing="0" width="100%">
  <tbody>
    <tr>
      <td width="200">
        <p><a href="#" onclick="toggle_visibility('itemAAA');">itemA</a></p>
      </td>
      <td width="200">Feild2</td>
      <td width="200">Feild3</td>
    </tr>
    <tr>
      <td colspan="3">
      <div id="itemAAA">
        <table border="1" cellspacing="0" width="100%">
        <tbody>
          <tr>
            <td width="200">
              <p><a href="#" onclick="toggle_visibility('itemAA');">itemAA</a></p>
            </td>
            <td width="200">Feild2</td>
            <td width="200">Feild3</td>
            </tr>
          <tr>
          <td colspan="3">
            <div id="itemAA">
              <table border="1" cellspacing="0" width="100%">
              <tbody>
                <tr>
                  <td width="200">
                    <p>itemAAA</p>
                  </td>
                  <td width="200">Feild2</td>
                  <td width="200">Feild3</td>
                </tr>
              </tbody>
              </table>
              </div>
            </td>
          </tr>
        </tbody>
        </table>
      </div>
      </td>
    </tr>
    <tr>
      <td width="200">
        <p>itemB</p>
      </td>
      <td width="200">Feild2</td>
      <td width="200">Feild3</td>
    </tr>
    </tbody>
  </table>
</div>

</body>

</html>

any ideas? is there a better option to acheive the same functionality?

David

+1  A: 

Ok, I think the problem is in the fact that you try to use a static value for the column width (you specify 200px) and than you assign a width of 100% to the table.

You ask two very different things to the browser I think. It have to stretch the widths in some manner.

The other problem is in the definition of the borders, paddings and margins: you don't exactly know (or you don't take into account) how the browser draws the borders/paddings/margins, so you can't specify precise values.

if you want to transform your table in a static one, you can use something like this:

<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>item List</title>
<script type="text/javascript">
      <!--
          function toggle_visibility(id) {
             var e = document.getElementById(id);
             if(e.style.display == '') {
               e.style.display = 'block';
             }
             if(e.style.display == 'block')
                e.style.display = 'none';
             else
                e.style.display = 'block';
          }
      //-->
    </script>
    <style>
        table{
            background-color: black;
        }
        td{
            background-color: white;
            margin: 0;
            padding: 2px;
        }
    </style>
</head>

<body>

<div id="root">
  <table cellspacing="2">
  <tbody>
    <tr>
      <td width="200">
        <p><a href="#" onclick="toggle_visibility('itemAAA');">itemA</a></p>
      </td>
      <td width="200">Feild2</td>
      <td width="200">Feild3</td>
    </tr>
    <tr>
      <td colspan="3">
      <div id="itemAAA">
        <table cellspacing="2">
        <tbody>
          <tr>
            <td width="196">
              <p><a href="#" onclick="toggle_visibility('itemAA');">itemAA</a></p>
            </td>
            <td width="200">Feild2</td>
            <td width="196">Feild3</td>
            </tr>
          <tr>
          <td colspan="3">
            <div id="itemAA">
              <table cellspacing="2">
              <tbody>
                <tr>
                  <td width="192">
                    <p>itemAAA</p>
                  </td>
                  <td width="200">Feild2</td>
                  <td width="192">Feild3</td>
                </tr>
              </tbody>
              </table>
              </div>
            </td>
          </tr>
        </tbody>
        </table>
      </div>
      </td>
    </tr>
    <tr>
      <td width="200">
        <p>itemB</p>
      </td>
      <td width="200">Feild2</td>
      <td width="200">Feild3</td>
    </tr>
    </tbody>
  </table>
</div>

</body>

</html>

As you can see, I deleted the widths from the tables and I put som css in the cose. The widths of the columns are exactly calculated to take in account borders, margins and paddings.

s.susini
Width attribute is deprecated; use css instead.
no
Sure, it would be very interesting to know if GreyCloud HAVE TO use tables for his project. Is your content pure data? Otherwise you shuld use divs...
s.susini
Hi, thanks for fixing the widths, it looks much better. I dont specifically need to use tables, but i am looking for a table/grid layout as i'll be populating this data from xml via xlst
GreyCloud
watch out using href="#" makes the page reload .. doing some thing like href="#id" means that the page doesnt refresh [or at least the view isnt reset to the top of the page!]
GreyCloud
A: 

Try this out. The interesting bit is the css border-collapse property. Also notice that deprecated html attributes (width, etc.) have been removed in favor of css rules.

<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>item List</title>
<style>
  table.grid { padding:0; margin:0; width:100%; border-collapse:collapse; }
  table.grid td  { padding:0; margin:0; outline:1px outset silver; width:33% }
  table.grid div { padding:0; margin:0;  }
</style>
<script type="text/javascript">
  function toggle_visibility (id) {
    var e = document.getElementById(id);
    if (e.style.display == 'none')
      e.style.display = 'block';
    else
      e.style.display = 'none';
  }
</script>
</head>

<body>

<div id="root">
  <table class="grid">
  <tbody>
    <tr>
      <td>
        <p><a href="#" onclick="toggle_visibility('itemAAA');">itemA</a></p>
      </td>
      <td>Feild2</td>
      <td>Feild3</td>
    </tr>
    <tr>
      <td colspan="3">
      <div id="itemAAA">
        <table class="grid">
        <tbody>
          <tr>
            <td>
              <p><a href="#" onclick="toggle_visibility('itemAA');">itemAA</a></p>
            </td>
            <td>Feild2</td>
            <td>Feild3</td>
            </tr>
          <tr>
          <td colspan="3">
            <div id="itemAA">
              <table class="grid">
              <tbody>
                <tr>
                  <td>
                    <p>itemAAA</p>
                  </td>
                  <td>Feild2</td>
                  <td>Feild3</td>
                </tr>
              </tbody>
              </table>
              </div>
            </td>
          </tr>
        </tbody>
        </table>
      </div>
      </td>
    </tr>
    <tr>
      <td>
        <p>itemB</p>
      </td>
      <td>Feild2</td>
      <td>Feild3</td>
    </tr>
    </tbody>
  </table>
</div>

</body>

</html>
no
This layout doesn't show the nesting of items to the user so it is unclear which rows they will be hiding by clicking the links, thanks for the css though
GreyCloud