7

我有这个结构:

<div class="father">
 This string is left-aligned
 <div class="divToCenter" style="display:inline-block;">
  //contains other divs.I used inline block for making this div as large as 
  //content ATTENTION: its width is not fixed! 
 </div>

</div>

我怎么能说:

让我只让名为“divToCenter”的类以“父亲”div 为中心。

谢谢

卢卡

4

3 回答 3

3

#left {
  float: left;
   background: #eee;
   width: 200px; /* width is optional */
}
#content {
  overflow: hidden;
  text-align: center;
}
    <div class="father">
     <div id="left">This string is left-aligned</div>
     <div id="content">
      <!-- contains other divs.I used inline block for making this div as large as content -->
     </div>
    </div>

将左对齐的字符串或块浮动到左侧,然后overflow:hidden在内容上它将自动占用剩余空间,您可以按照自己的意愿进行操作text-align

要么将左侧内容也转换为内联块,因此它和内容并排,您将能够text-align分别进行inline-block

于 2011-05-20T10:31:49.913 回答
2
div.divToCenter
{
margin: 0 auto;
}
于 2011-05-20T10:07:24.837 回答
1
.father { text-align: center; }
.father div.divToCenter { margin: 0 auto; }

更新:

.father { text-align: left; }
.father div.divToCenter { width:Xpx; margin: 0 auto; }
于 2011-05-20T10:22:29.563 回答