Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make centered DIV inside wide DIV expand with content width

Tags:

html

css

I'm using 2 divs, a parent and a child. The parent has a width of 100% and its contents are text-align'ed to center.

The child div should have a width of zero (or something close to it) and expand its width automatically with its contents, while still being centered in the parent div. Example:

+------------ PARENT DIV ------------+
|                                    |
|          ..some content..          |
|                                    |
|           +--CHILD DIV--+          |
|           |Inside child |          |
|           +-------------+          |
|                                    |
+------------------------------------+

+------------ PARENT DIV ------------+
|                                    |
|          ..some content..          |
|                                    |
|      +------ CHILD DIV ------+     |
|      |Inside child with more |     |
|      +-----------------------+     |
|                                    |
+------------------------------------+

If I put a fixed width to the child div, I can center it correctly:

.parent {
    width: 100%;
}
.child {
    width: 100px;
    margin-left: auto;
    margin-right: auto;
}

But that is not what I need, I need the child div to expand its width according to its content. So I tried using float and nowrap:

.parent {
    width: 100%;
}
.child {
    float: left;
    white-space: nowrap;
    margin-left: auto;
    margin-right: auto;
}

That works for the child itself, but then it is no longer centered as a content of the parent.

I can solve it by using Javascript, but I really prefere not to.

I've been looking around for similar questions in SO but I haven't find one that answers this situation exactly.

Can someone please throw me a light?

Thanks in advance for any comments.

Francisco

like image 538
Francisco Zarabozo Avatar asked Apr 25 '13 08:04

Francisco Zarabozo


People also ask

How do you expand a div with content?

The height property is used to set the height of an element. The height property does not contains padding, margin and border of element. The height property contains many values which define the height of an element.

How do I center align a div inside a div?

You can do this by setting the display property to “flex.” Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do you full width a div?

What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.

How do I center a div inside a div in bootstrap?

Add d-flex align-items-center justify-content-center classes to the parent element to center its content vertically and horizontally.


1 Answers

You can use display: inline-block; (not supported in < IE7) but you must enclose all other content in another block level element (like a <p> or another <div>)

.parent {
    width: 100%;
    text-align: center;
        border: 1px solid #000;
}
.child {
    display: inline-block;
        border: 1px solid #f00;
}

http://jsfiddle.net/4kpsK/

(Click the red bordered div to add more content and see the solution in effect.)

like image 150
Kyle Avatar answered Oct 01 '22 20:10

Kyle