33

How can I prevent div from expanding? I want div with elements not to take 100% of available space and have width that it's children have. I need this for centering parent div horizontally. The trick is that child elements should share float:left or diplay: inline-block and fluid width, so there can be few rows of child elements.

I can not wrap each row in its own div since it will break responsive design.

6
  • 2
    There's several possibilities: display:inline-block is good for this (be aware of the IE7 hack) Commented Aug 8, 2013 at 19:44
  • Set display: inline-block to the parent and wrap it by a container has text-align: center; Commented Aug 8, 2013 at 19:44
  • As this seems like a trivial problem, it might help to show your HTML so we can see what challenge you're facing. Commented Aug 8, 2013 at 19:44
  • Setting display:inline block doesn't prevent element from expanding if there are more than one row of child elements Commented Aug 8, 2013 at 19:47
  • have you trying setting a static width as well as a max-width? Commented Aug 8, 2013 at 19:49

5 Answers 5

23

You can set the width property of the children to fit-content. Doing so will make these elements take up only as much horizontal space as they need and is available within the parent.

You can also set width to max-content but this will ignore the width of the parent and content will extend as far as any descendants need and possibly overflow the parent.

Example:

Problem setup:

.parent {
  width: 15rem;
  height: 5rem;
  border: 1px solid blue;
}

.child {
  border: 1px solid red;
}
<div class="parent">
  <div class="child">
    Content for child
  </div>
</div>

Solution:

.parent {
  width: 15rem;
  height: 5rem;
  border: 1px solid blue;
}

.child {
  width: fit-content;
  border: 1px solid red;
}
<div class="parent">
  <div class="child">
    Content for child
  </div>
</div>

Support for fit-content is pretty good (caniuse?). There's support for fit-content on pretty much all the major desktop browsers (except IE), and unknown support on some of the mobile browsers.

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

Comments

20

You should use display: table; It will shrink to the size of it's contents and can also be centered and positioning without having to assign a given width.

DEMO http://jsfiddle.net/kevinPHPkevin/9VRzM/

3 Comments

try setting float:left or display: inline-block to child elements, it will cause problems
@vlad I actually tried setting float:left and display: inline-block and I don't see a problem. In these situations, the parent container will expand as much as it can to allow the child elements to fill the available width. You can constrain the width using the maximum-width property for the parent container.
still expands when more text
3

If you truly want the parent div to collapse around its child elements (for whatever reason, based on what you're trying to accomplish) and you then want to center that div, then @Vector's answer is spot on, use display: table with margin: 0 auto.

If it's ok for the div to remain expanded to the full width of the container in which you're trying to center your children, then you have at least a couple more options, again depending on your particular situation.

You can use text-align: center.

.content {
  text-align: center;
  border-style: solid;
  border-width: thin;
}

.content span {
  display: inline;
  border-style: solid;
  border-width: thin;
}
<div class="content">
  <div>Test</div>
  <div>Test</div>
</div>

You could also use the newer display: flex with justify-content: center, depending on the level of browser compatibility you're supporting, of course.

.content {
  display: flex;
  justify-content: center;
  border-style: solid;
  border-width: thin;
}

.content div {
  border-style: solid;
  border-width: thin;
}
<div class="content">
  <div>Test</div>
  <div>Test</div>
</div>

Comments

2

If you modify DOM adding extra html elements inside a div and that cause it to be expanded, a simple solution would be to add that css to the root element of those extra html elements:

max-width: fit-content;

That will prevent them to expand the parent div's width.

Comments

1

have you tried using display: inline-block? DIV will take up 100% width because they are block elements.

1 Comment

This will collapse the div but it won't be (easily) centered.

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.