内联块元素上的CSS text-align

时间:2011-05-20 10:04:34

标签: html css text-align

我有这个结构:

<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为中心。

由于

卢卡

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

div.divToCenter
{
margin: 0 auto;
}

答案 2 :(得分:1)

.father { text-align: center; }
.father div.divToCenter { margin: 0 auto; }

更新

.father { text-align: left; }
.father div.divToCenter { width:Xpx; margin: 0 auto; }
相关问题