Solution 1 :

The problem is using width=”100%” for div when you need to use style=”width:100%”

So for following line:

<div width="100%" class="bar"><a class="link" href="#">twenty</a></div>

Should be:

<div style="width:100%" class="bar"><a class="link" href="#">twenty</a></div>    

Apply that for lines for the div containing 50% and 25%

Problem :

I’m struggling to set custom widths of div elements inside a table cell. Various questions (e.g.) talk about position and display parameters, and I’ve tried too many variation to describe without success. I’d greatly appreciate it if someone could either assist to get this minimal example working, or refer me to a specific previous answer that does actually solve this problem. Thanks in advance.

In the example I want to set the divs as horizontal bars to reflect the values. The table is fixed with and all div ancestors set to width: 100%.

.bar-column {
    width: 100%;
  }
  .bar {
    /* some other tested parameters.. */
    /* position: absolute; */
    /* display: block; float: left; */
    /* display: table-cell; */
    position: relative;
    display: inline-block;
    height: 100%;
    background-color: yellow;
  }
  tr { width: 100%; }
<table width="600px" style="background-color: #ddd;">
  <tr>
    <th>Value</th>
    <th class="bar-column">Name and bar</th>
  </tr>
  <tr>
    <td>20</td>
    <td class="bar-column">
      <div width="50%" class="bar"><a class="link" href="#">twenty</a></div>
    </td>
  </tr>
  <tr>
    <td>40</td>
    <td class="bar-column">
      <div width="100%" class="bar"><a class="link" href="#">forty</a></div>
    </td>
  </tr>
  <tr>
    <td>10</td>
    <td class="bar-column">
      <div width="25%" class="bar"><a class="link" href="#">ten</a></div>
    </td>
  </tr>
</table>

Comments

Comment posted by geotheory

Thanks! I wrongly assumed the width parameter was exactly the same as using style.

Comment posted by John

The width=”660px” works for table probably due to backward compatibility reasons. The style method is used

By