Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center div and fit contents?

Tags:

html

css

I want to center a div in the middle of my page, and have the div fit the contents. How can I do that?


I've tried

width: 1px;
white-space: nowrap;
margin: 0 auto;

Which centers the div, but then all the text is to the right of that.

display: inline-block makes the div fit the contents, but then margin: 0 auto; doesn't seem to work...

like image 257
mpen Avatar asked Dec 15 '10 20:12

mpen


1 Answers

Does this work for you: http://vidasp.net/tinydemos/inline-centered-div.html

body { text-align:center; }
div { display:inline-block; }
like image 134
Šime Vidas Avatar answered Oct 20 '22 19:10

Šime Vidas