Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting TD cell width to wrap around div with specified width %

Here's my current HTML / CSS: http://jsfiddle.net/S8Bne/

And here's a screenshot that annotates what I'd like to try to do:

enter image description here

Basically, I'd like the width of the parent TD to dynamically wrap based on the width of the nested div. Is this possible?

like image 484
keruilin Avatar asked Mar 17 '26 21:03

keruilin


1 Answers

Well...as Sheepy pointed out, what you are asking does not seem to make sense, initially. But if you really want to make the TD adjust with the inner DIV, you will need to do some JavaScript...

Say, you have the following table cell:

<td rowspan="4" id="tdwrap"> <div id="divwrap">...</div></td>

You could have the following javascript calls:

// set cell width to its inner div width (in pixel)
$('#tdwrap').width($('#divwrap').width());

// set the div width back to its parent cell width (in pixel)
$('#divwrap').width($('#tdwrap').width());

And then, you can set the width of the div:

#divwrap {
    width: 30%;
}

This way, you can let the cell width to adjust to its inner div's width, which is based on its parent cell's width! :p

Example: http://jsfiddle.net/william/S8Bne/89/

Note that I have used the jQuery library in the code snippet.

Is this what you are after?

like image 123
William Niu Avatar answered Mar 19 '26 11:03

William Niu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!