Don P
Don P

Reputation: 63667

Center a div in a container, and have the div be the width of its child

I want a <div> to be the size of its child element, but I also want to center it within a containing <div>. How can I do this?

http://jsfiddle.net/xjCGM/1/

In that jsfiddle I try do display: inline-block and margin: auto but they don't work together.

Upvotes: 0

Views: 38

Answers (2)

j08691
j08691

Reputation: 207900

Add text-align:center; to the .container div

jsFiddle example

Upvotes: 1

deviousdodo
deviousdodo

Reputation: 9172

You can use text-align: center: http://jsfiddle.net/xjCGM/2/ (and you'll most likely use text-align: left on the element itself)

Upvotes: 1

Related Questions