JoJo
JoJo

Reputation: 20115

Fill a percentage width of table cell

How do I make an element take up a percentage width of a table cell? When I attempt to do a percentage, say 70%, it grows much larger than the containing cell. 70% should be smaller than the containing cell! Here's a barebones example of my page. The absolute positioning is necessary for some layering I want to do.

HTML

<table>
 <tr>
  <td>
   <div>hello</div>
  </td>
  <td>
   <div>world</div>
  </td>
 </tr>
</table>

CSS

td {
 position: relative;
 width: 10em;
}

div {
 position: absolute;
 width: 70%;
}

Upvotes: 1

Views: 3534

Answers (1)

JoJo
JoJo

Reputation: 20115

Matt Ball's comment got me on the right track. It's some extra markup.

HTML

<table>
 <tr>
  <td>
   <div class="container">
    <div class="bar">hello</div>
   </div>
  </td>
  <td>
   <div class="container">
    <div class="bar">world</div>
   </div>
  </td>
 </tr>
</table>

CSS

td {
 width: 10em;
}

.container {
 position: relative;
}

.bar {
 position: absolute;
 width: 70%;
}

Upvotes: 3

Related Questions