45

Pertaining to html, how do I make a div container grow with the content instead of manually defining a width and height.

<div id="container">
  <div id="logo">
    <img src="someimage.png"/>
  </div>
</div>

No matter what the dimensions of logo are, container does not grow with it. :( How would I fix this, or can it be fixed?

0

6 Answers 6

49

If you don't define width and/or height, the div element grows along with its content. Unless his content is set to absolute! If the content is set to float, you have to set to the container

overflow:hidden

in order to let it grow!

Sign up to request clarification or add additional context in comments.

1 Comment

Very interesting! Why does this work? Can you provide more documentation about this?
16

The default behaviour for divs is to fill the entire available width. A few ways to override this:

  • set display: inline-block (not IE-friendly)
  • float it (with the side effect of, well, floating it)
  • set display: inline (but that's almost never what you want)
  • set position: absolute
  • hard-code a width (no dynamic width though)

As a last resort, consider javascript.

1 Comment

Can you elaborate how would js help here?
6

Use the magical css property called "flex", it is really amazing

yourDiv{
display:flex;
}

Just make sure that the children are not position: absolute because this will not work with flex.

5 Comments

needs more information... as your answer stands it doesn't work with nested divs of variable height.
Of course it does , i use it everyday , unless the div are display absolute !
that might have been handy information to include in your answer... so, " needs more information... as your answer stands it doesn't work..."
by the way... absolute is a position property value, not a display property value
ok , well the value flex to the display property will allow you arrang items inside the div invoked on. IT will help you to keep all the items for an example to the left or to the right or even spread them with equal spaces between then , horizontally or vertically. Obviously if the divs are position fixed or sticky or absolute this will not work with flex. Here is more detailed inforamtions because i cant write eveything here, it is too long. css-tricks.com/snippets/css/a-guide-to-flexbox
1

You can specify min-width and min-height and DIV will adjust width / height as the content changes (of course, not below min width/height).

Working code pen example

Most important css properties from the code (DIV is editable, so just type in text and it will grow with it by width / height):

 min-height: 200px;
  min-width: 100px;

1 Comment

If the content is larger than the minimum height, the min-height property has no effect. not sure how this helps
1

If you used float it prevents <div> to grow up with content so you can use clear after float and it will work.

<div class="clear-fix"></div>

.clear-fix{
   clear: both;
}

Comments

0

For my case I use this line of CSS :

    min-height: fit-content;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.