Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent child div from expanding parent without specifying width of parent?

I'm hoping to get someone to clean out the cobwebs of my brain on this one. I have tried to illustrate here (system won't allow me to post a GIF so I went with ASCII art)

|--------------------------------------------------------------------|
| Browser Window                                                     |
|                                                                    |
| |--------------------------------------------------|               |
| |  Parent DIV                                      |               |
| |                                                  |               |
| |  |-----------------------------------------|     |               |
| |  | Child DIV #1                            |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |-----------------------------------------|     |               |
| |                                                  |               |
| |                                                  |               |
| |  |-----------------------------------------|     |               |
| |  | Child DIV #1                            |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |                                         |     |               |
| |  |-----------------------------------------|     |               |
| |                                                  |               |
| |                                                  |               |
| |--------------------------------------------------|               |
|                                                                    |
|                                                                    |
|                                                                    |
|                                                                    |
|--------------------------------------------------------------------|

I would like to have a parent DIV that contains 2 child DIVs. Child div #1 has a long paragraph in it. Child DIV #2 contains variable width content which is displayed in a table.

I would like the parent DIV to expand to the width of Child DIV #2, and I would like Child DIV #1 to only expand to the width of the parent DIV, and not expand the parent any further.

I have all 3 DIVs set to float:left.

If I only include child DIV #2, the parent DIV expands to the width of the table (plus padding, etc) - just what I want. As soon as I include DIV #1, the parent DIV expands to the width of the browser window because Child DIV #1 is that wide too.

I could constrain the width of the parent DIV, however I don't want to do this because of the variable width of child DIV #2. I could constrain the width of child DIV #1 but I don't want to do this because I would like it to be the same width as child DIV #2 and DIV #2 has a variable width.

I would not like to constrain any heights because the amount of content in each box is variable.

Is there another approach to get these results?

Thanks,

Ralph

like image 553
Ralph Slate Avatar asked Nov 25 '22 18:11

Ralph Slate


1 Answers

Interesting question. I tried a lot before surrendering to a js solution, but the only way that I could get these results was doing what Hope I Helped suggested in his answer.

I came here with a working sample:

Basically, using jQuery, you could code something like that:

$(document).ready(function() {
    $('#div1').css('width', $('#div2').css('width'));
});
like image 134
falsarella Avatar answered Nov 27 '22 08:11

falsarella