【发布时间】:2011-11-27 02:06:15
【问题描述】:
我想在一个流畅的布局中将一个容器 div 居中(id:articles_grid 的内容必须居中): http://www.benskesblog.com/projects/frontend/project/index.htm
我尝试了很多方法,但没有一个奏效。 (例如:margin: 0 auto;)。
谁能告诉我如何解决这个问题?
谢谢
【问题讨论】:
我想在一个流畅的布局中将一个容器 div 居中(id:articles_grid 的内容必须居中): http://www.benskesblog.com/projects/frontend/project/index.htm
我尝试了很多方法,但没有一个奏效。 (例如:margin: 0 auto;)。
谁能告诉我如何解决这个问题?
谢谢
【问题讨论】:
我相信改变很简单就能达到你的需要:
#articles_grid {
...
text-align: center; /*add this*/
}
#articles_grid li {
...
/* float:left; remove this */
display: inline-block; /*add this*/
}
【讨论】:
您可以将display:inline-block 给您DIV,并在他的parent DIV 中定义text-align:center。例如,您可以这样做:
CSS:
.parent{
background:red;
text-align:center;
}
.center{
text-align:left;
display:inline-block;
*display:inline;/* IE7 */
*zoom:1;
background:yellow;
min-height:100px;
}
HTML:
<div class="parent">
<div class="center">lorem ipsum</div>
</div>
查看以下示例:
【讨论】: