开发者

Centering flexible width content with css

开发者 https://www.devze.com 2023-03-08 20:54 出处:网络
I am having difficulty trying to horizontally center an (inline?) element within my containing div.I want \"some text\" to have a background which collapses to it, and for that all to be centered with

I am having difficulty trying to horizontally center an (inline?) element within my containing div. I want "some text" to have a background which collapses to it, and for that all to be centered within the containing element. I know I can do this if i specify a width, but I am trying to avoid that

Check out a simple example I put together with some nonworking css... JSfiddle: http://jsfiddle.net/Mqw5R/ (works great even without js!)

<div>
<p>Some 开发者_StackOverflowtext</p>
</div>

=========================
=                       =
=      |some text|      =
=                       =
=========================


http://jsfiddle.net/Mqw5R/3/

Why don't you just put

text-align:center;

Inside of your DIV css?


Add text-align:center to the containing element, not the child, like so: http://jsfiddle.net/thomas4g/Mqw5R/1/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号