Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a parent div auto size to the width of its children divs

Tags:

html

css

I'm trying to make the parent div only as wide as the child div's. Auto width is making the parent div fit the entire screen. The children divs will be different widths depending on their content so I need the parent div to adjust accordingly.

<div style='width:auto;'>   <div style="width: 135px; float: left;">     <h4 style="padding-right: 5px; padding-left: 15px;">Column1</h4>     <dl style="padding-right: 5px; padding-left: 15px; margin-top: -8px; overflow-x: hidden;">       <dd style="white-space: nowrap;">1</dd>       <dd style="white-space: nowrap;">2</dd>     </dl>      <h4 style="padding-right: 5px; padding-left: 15px;">Column1</h4>     <dl style="padding-right: 5px; padding-left: 15px; margin-top: -8px; overflow-x: hidden;">       <dd style="white-space: nowrap;">1</dd>       <dd style="white-space: nowrap;">2</dd>     </dl>   </div>    <div style="width: 135px; float: right;">     <h4 style="padding-right: 5px; padding-left: 10px;">Column2</h4>     <dl style="padding-right: 5px; padding-left: 15px; margin-top: -8px; overflow-x: hidden;">       <dd style="white-space: nowrap;">1</dd>       <dd style="white-space: nowrap;">2</dd>       <dd style="white-space: nowrap;">3</dd>     </dl>   </div> </div> 
like image 676
bradley4 Avatar asked Feb 27 '13 01:02

bradley4


People also ask

How do I make my parents div fit my kids?

Divs size to 100% the size of their container width automatically. Try using display:inline-block instead of width:auto on the container div. Or possibly float:left the container and also apply overflow:auto .

How do I make a div auto adjust width?

Set display to inline-block to make the div width fit to its content. Use position: relative and left: 50% to position its left edge to 50% of its containing element. Use transform: translateX(-50%) which will "shift" the element to the left by half its own width.

How do I make DIVS always fit in my parent div?

Method 1: First method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div. Consider this HTML for demonstration: HTML.

How do you make a child div wider than a parent div?

In this case, we set the child element's width to be 100% of the viewport width by using a percentage viewport unit (vw), then, we move it to the left side (by the distance of the viewport's half, minus 50% of the width of the parent element) with the left property.


1 Answers

Your interior <div> elements should likely both be float:left. Divs size to 100% the size of their container width automatically. Try using display:inline-block instead of width:auto on the container div. Or possibly float:left the container and also apply overflow:auto. Depends on what you're after exactly.

like image 152
Madbreaks Avatar answered Sep 28 '22 03:09

Madbreaks