以未知宽度居中div [英] Centering div with unknown width

查看:158
本文介绍了以未知宽度居中div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有div < div id =container>< / div> 其中包含所有页面内容,包括页眉页脚等。

i have div <div id="container"></div> which contains all page content including header footer etc.

所以我想把这个div集中到我的页面的中心,现在我有这个css:

So i would like to center this div to the center of my page, now i have this css:

#page{
position:relative;
margin:auto;
width:1000px;
}

它的工作原理,但我的问题是这个div的内容不断变化宽度也改变,它可以 1000px 10100px 所以我需要像

And it works, but my problem is that content in this div keeps changing so the width changes too, it can be 1000px or 10100px so i need something like width:auto;, how can do something like that?

推荐答案

查看: http://jsfiddle.net/thirtydot/rjY7F/

HTML

HTML:

<div id="container">
    i'm as wide as my content
</div>

CSS:

body {
    text-align: center;
}
#container {
    text-align: left;
    border: 1px solid red;
    display: inline-block;
    /* for ie6/7: */
    *display: inline;
    zoom: 1;
}

这篇关于以未知宽度居中div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆