# CSS: centering a div of unknown width

**URL:** https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451
**Category:** Factual Questions
**Created:** [September 17, 2009, 3:04pm UTC](https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451 "2009-09-17T15:04:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![tim314](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tim314/32/3468_2.png) [@tim314](https://boards.straightdope.com/u/tim314)
#### Post date: [September 17, 2009, 3:04pm UTC](https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451/1 "2009-09-17T15:04:05Z")

</div>

I have a \<div\> that contains some dynamically generated content. If I knew its width, I could center it like this:

\<div style=“border: 1px solid black; width: 600px; margin-left: auto; margin-right: auto;”\>

However, I want it to resize itself to the width of its content.

I can do this by adding “float: left;” to the style attribute, but this causes “margin-left: auto; margin-right: auto;” to stop working.

Is there any way to get both the centering and the auto-sizing?

Thanks in advance.

---

<div class="post-metadata">

### Author: ![KellyCriterion](https://avatars.discourse-cdn.com/v4/letter/k/b3f665/32.png) [@KellyCriterion](https://boards.straightdope.com/u/KellyCriterion)
#### Post date: [September 17, 2009, 3:07pm UTC](https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451/2 "2009-09-17T15:07:45Z")

</div>

What’s the dynamically generated content? Just text?

You could wrap that content in another div, set it’s width as auto, then set 50% margins (left and right) to it. That’d be my first attempt at solving it.

---

<div class="post-metadata">

### Author: ![KellyCriterion](https://avatars.discourse-cdn.com/v4/letter/k/b3f665/32.png) [@KellyCriterion](https://boards.straightdope.com/u/KellyCriterion)
#### Post date: [September 17, 2009, 3:13pm UTC](https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451/3 "2009-09-17T15:13:17Z")

</div>

Also, the best place I have found on the net to ask these types of questions is the [Web Developer Forum](http://www.webdeveloper.com/forum/index.php). It’s a reasonably busy place, and you’ll notice that **CSS** has its own dedicated message board. To give an idea of traffic, there’s currently 69 people viewing the CSS forum.

---

<div class="post-metadata">

### Author: ![Markxxx](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@Markxxx](https://boards.straightdope.com/u/Markxxx)
#### Post date: [September 17, 2009, 3:50pm UTC](https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451/4 "2009-09-17T15:50:18Z")

</div>

Also try [Dynamic Drive](http://www.dynamicdrive.com/), there forums are full of people that will actually help you, plus there are lots of pre-made scripts and you can look at the coding of these and see what you need

---

<div class="post-metadata">

### Author: ![Duckster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/duckster/32/1244_2.png) [@Duckster](https://boards.straightdope.com/u/Duckster)
#### Post date: [September 17, 2009, 5:00pm UTC](https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451/5 "2009-09-17T17:00:04Z")

</div>

> [@tim314](#):
>
> Is there any way to get both the centering and the auto-sizing?

[CSS Centering 101](http://www.simplebits.com/notebook/2004/09/08/centering.html).

Works like a charm.

Oh, and put your CSS in a style sheet and not inline.

---

<div class="post-metadata">

### Author: ![tim314](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tim314/32/3468_2.png) [@tim314](https://boards.straightdope.com/u/tim314)
#### Post date: [September 17, 2009, 10:53pm UTC](https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451/6 "2009-09-17T22:53:13Z")

</div>

Thank you all for the suggestions. I don’t think any of them quite solve my problem, but I will also post to the other boards you mention.

> [@KellyCriterion](#):
>
> You could wrap that content in another div, set it’s width as auto, then set 50% margins (left and right) to it. That’d be my first attempt at solving it.

Adding “style=‘width: auto’” doesn’t seem to do anything for me. I can only get it to auto-size by making the div float, which breaks my auto-centering.

> [@Duckster](#):
>
> [CSS Centering 101](http://www.simplebits.com/notebook/2004/09/08/centering.html).
> 
> Works like a charm.

I tried that, but it only seems to work if the width of the container div is specified. I want it to automatically resize to its contents (like it does if I add “float: left”).

---

<div class="post-metadata">

### Author: ![arseNal](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@arseNal](https://boards.straightdope.com/u/arseNal)
#### Post date: [September 17, 2009, 11:31pm UTC](https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451/7 "2009-09-17T23:31:46Z")

</div>

I had what I think is the same problem as you (well, I wanted it centered vertically as well as horizontally). I’m not much of a designer and was frustrated at not being able to find a simple solution that worked consistently.

Here is what finally worked for me and didn’t involve like a 10 meg .css:

[http://www.sitecrafting.com/blog/to-center-div/](http://www.sitecrafting.com/blog/to-center-div/)

---

<div class="post-metadata">

### Author: ![Duckster](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/duckster/32/1244_2.png) [@Duckster](https://boards.straightdope.com/u/Duckster)
#### Post date: [September 19, 2009, 5:42am UTC](https://boards.straightdope.com/t/css-centering-a-div-of-unknown-width/510451/8 "2009-09-19T05:42:27Z")

</div>

> [@tim314](#):
>
> I tried that, but it only seems to work if the width of the container div is specified. I want it to automatically resize to its contents (like it does if I add “float: left”).

Do you have some code, or a link to share? It’s easier to offer help if you have something to share.
