tags:

views:

86

answers:

4

Hey all,

I've looked through several posts on StackOverflow, but haven't been able to find an answer to this rather simple question.

I have an HTML construct like this:

<table>
  <tr>
    <td class="thatSetsABackground">
      <div class="thatSetsABackgroundWithAnIcon">
        <dl>
          <dt>yada
          </dt>
          <dd>yada
          </dd>
        </dl>
      <div>
    </td>
    <td class="thatSetsABackground">
      <div class="thatSetsABackgroundWithAnIcon">
        <dl>
          <dt>yada
          </dt>
          <dd>yada
          </dd>
        </dl>
      <div>
    </td>
  </tr>
  <tr>
    <td class="thatSetsABackground">
      <div class="thatSetsABackgroundWithAnIcon">
        <dl>
          <dt>yada
          </dt>
          <dd>yada
          </dd>
        </dl>
      <div>
    </td>
    <td class="thatSetsABackground">
      <div class="thatSetsABackgroundWithAnIcon">
        <dl>
          <dt>yada
          </dt>
          <dd>yada
          </dd>
        </dl>
      <div>
    </td>
  </tr>
</table>

What I need is for the div to fill the height of the TD, so I can be able to position the DIV's background (the icon) at the bottom-right corner of the TD.

How do you suggest I go about that?

Thanks in advance!

A: 

Modify the background image of the <td> itself.

Or apply some css to the div:

.thatSetsABackgroundWithAnIcon{
    height:100%;
}
Jage
as I write in the code. The TD already has a background-image set using a class. So that option is not viable. I have tried setting the height of the div to 100%, but that does not work. It simply wraps to the variable height of the contained <dl>. So something is needed to make the div "understand" that it should fill the height. And height: 100% does not do that. (not alone atleast)
CodeMonkey
+1  A: 

CSS height: 100% only works if the element's parent has an explicitly defined height. For example, this would work as expected:

td {
    height: 200px;
}

td div {
    /* div will now take up full 200px of parent's height */
    height: 100%;
}

Since it seems like your <td> is going to be variable height, what if you added the bottom right icon with an absolutely positioned image like so:

td {
    /* required to make the td a coordinate map for the icon */
    position: relative;
}

td img.theIcon {        
    position: absolute;
    bottom: 0;
    right: 0;
}

With the table cell markup like so:

<td class="thatSetsABackground">      
    <dl>
      <dt>yada
      </dt>
      <dd>yada
      </dd>
    </dl>
    <img class="theIcon" src="foo-icon.png" alt="foo!"/>
</td>

Edit: using jQuery to set div's height

If you keep the <div> as a child of the <td>, this snippet of jQuery will properly set its height:

// Loop through all the div.thatSetsABackgroundWithAnIcon on your page
$('div.thatSetsABackgroundWithAnIcon').each(function(){
    // Get their parent td's height
    var tdHeight = $(this).closest('td').height();

    // Set the div's height to their parent td's height
    $(this).height(tdHeight);
});
Pat
That could work. Let me get back to you when I've tried. I did read up on the subject and understood that <div> needs a specified height to be able to scale to 100%. From what I read that was possible to do with jQuery though, since it can calculate it for me.
CodeMonkey
Yes, that's exactly it. For the `<div>`'s height: 100% to work, the `<td>` (which is the `<div>`s parent) must have a specified height. And you're also right - jQuery would do it quite easily for you - see my updated example.
Pat
I didn't really work all that well, and we've decided on different approach. But I'll accept your answer since it's the best one around.
CodeMonkey
Hah, if only I had a dollar for the number of times CSS has made me change my approach :)
Pat
A: 

You could try making your div float.

.thatSetsABackgroundWithAnIcon{

float:left;

}

Alternativelly:

.thatSetsABackgroundWithAnIcon{

display:inline-block;

}

angryobject
A: 

If you give your TD a height of 1px, then the child div would have a heighted parent to calculate it's % from. Because your contents would be larger then 1px, the td would automatically grow, as would the div. Kinda a garbage hack, but I bet it would work.

psayre23