Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making child wider than its parent without width

Tags:

css

<div id="parent">
  <div id="child">
    content...
  </div>
</div>

Is it possible to make #child as wide as the content is? Making it wider than #parent, which has a fixed width, if needed.

(in my case content is an unknown text string that I don't want to line break)

like image 225
user1079082 Avatar asked Dec 03 '11 15:12

user1079082


People also ask

Why did the width collapse in the percentage width child element in an absolutely positioned parent on Internet Explorer 7?

Its width will be worked out based on the pixel width of its content and will be calculated after the contents are rendered. So at the point, IE encounters and renders your relatively positioned div its parent has a width of 0 hence why it itself collapses to 0.

Is width inherited?

width:inherit inherits width that defined by parent. This makes child width 25%, but if I redefine it with width:100% it will define width of child 50%.

What is width max content?

The max-content sizing keyword represents the intrinsic maximum width or height of the content. For text content this means that the content will not wrap at all even if it causes overflows.


2 Answers

You can set display:inline and white-space:nowrap on your #child, which will give you the results you're desiring. The container will only be as wide as the content within, and will exceed the width of the parent if necessary.

Demo: http://jsbin.com/ebuxup/edit#html,live

like image 129
Sampson Avatar answered Oct 07 '22 03:10

Sampson


Try with this CSS to avoid line break at all #child { white-space: nowrap; }.

Infos here: http://www.w3schools.com/cssref/pr_text_white-space.asp

like image 35
lorenzo-s Avatar answered Oct 07 '22 02:10

lorenzo-s