中心文本和浮动div在右边

时间:2012-11-10 01:00:59

标签: css html styles

我有一个容器div,其中包含我想要居中的文本。我还想在容器中插入一个div,它向右浮动,如下所示:

<div id="container" style="text-align:center">
  Text
  <div id="child" style="float:right"></div>
</div>

不幸的是,文本不再以容器为中心,而是向左移动了孩子的宽度。

有没有人知道如何让文本居中,同时保持div包含在右边?

2 个答案:

答案 0 :(得分:9)

像这样......

<div style='position:relative;'>
  my centered text
  <div style='position:absolute;right:0;top:0'>
    my right div
  </div>
</div>

您显然可以将内联样式放入CSS中。

答案 1 :(得分:0)

这可能吗?创建3个相等的部分。左中左右?

<div id="container">
<div id="child1" style="float:right width: 30px;"></div>
<div id="child2" style="float:right width: 30px; text-align:center;">TEXT</div>
<div id="child3" style="float:right width: 30px;"></div>
</div>