CSS:左,中,右对齐同一行的文本

use*_*824 37 css css3

我需要在同一行上左,中,右对齐文本.我有以下文字:

  • 左对齐:1/10
  • 中心:02:27
  • 右对齐:100%

我编写了以下代码,该代码适用于左右对齐文本,但对中心文本无法正常工作.

HTML

<div id="textbox">
<p class="alignleft">1/10</p>
<p class="aligncenter">02:27</p>
<p class="alignright">100%</p>
</div>
<div style="clear: both;"></div>
Run Code Online (Sandbox Code Playgroud)

CSS

.alignleft {
    float: left;
}
.aligncenter {
    float: left;
}
.alignright {
    float: right;
}
Run Code Online (Sandbox Code Playgroud)

JCB*_*gar 38

试试这个

更新

HTML

 <div id="textbox">
 <p class="alignleft">1/10</p>
 <p class="aligncenter">02:27</p>
 <p class="alignright">100%</p>
 </div>
 <div style="clear: both;"></div>?
Run Code Online (Sandbox Code Playgroud)

CSS

.alignleft {
  float: left;
  width:33.33333%;
  text-align:left;
}
.aligncenter {
  float: left;
  width:33.33333%;
  text-align:center;
}
.alignright {
 float: left;
 width:33.33333%;
 text-align:right;
}?
Run Code Online (Sandbox Code Playgroud)

在这里演示代码:http://jsfiddle.net/wSd32/1/ 希望这有帮助!

  • 当字体不相同时,这将不起作用-基线都弄乱了。 (2认同)

Pod*_*Pod 8

可响应的神奇HTML5方法是使用flex:

<div id="textbox">
<p class="alignleft">1/10</p>
<p class="aligncenter">02:27</p>
<p class="alignright">100%</p>
</div>
<div style="clear: both;"></div>
Run Code Online (Sandbox Code Playgroud)

的CSS

#textbox {
    display: flex;
    justify-content: space-between;
}
Run Code Online (Sandbox Code Playgroud)

演示:

http://jsfiddle.net/sep4kf6a/1/

您会发现它避免了在小屏幕上出现浮标时出现的尴尬包装。

  • 这使`02:27`在`1 / 10`的右侧与`100%`的左侧之间居中。它不会在包含的#textbox中居中。 (5认同)

Ada*_*11p 8

现在是一种全新的,完全不同的方法。

.same-line {
    height: 10px; /*childrens it's all absolute position, so must set height*/
    position: relative;
}
.same-line div{
    display: inline-block;
    position: absolute;
}
.on-the-left{
 left:0px;
}
.on-the-center{
    left: 0%;
    right: 0%;
    text-align: center;
}
.on-the-right{
   right: 0px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="same-line">
    <div class="on-the-left" >it's Left</div>
    <div class="on-the-center" >this Centrer bla bla bla</div>
    <div class="on-the-right" >it's Right</div>
  </div>
Run Code Online (Sandbox Code Playgroud)


Kae*_*tar 5

Pod 答案的改进:

#textbox {
    display: flex;
}
.alignleft {
    flex: 1;
    text-align: left;
}
.aligncenter {
    flex: 1;
    text-align: center;
}
.alignright {
    flex: 1;
    text-align: right;
}
Run Code Online (Sandbox Code Playgroud)
<div id="textbox">
<p class="alignleft">1/10</p>
<p class="aligncenter">02:27</p>
<p class="alignright">100%</p>
</div>

 
Run Code Online (Sandbox Code Playgroud)

这避免了小屏幕上浮动时出现的尴尬的盒子换行,以及将中间文本居中在 #textbox 的中间。

JSFiddle:

http://jsfiddle.net/secnayup/