简体   繁体   English

HTML / CSS:如何在段落中左右对齐文本

[英]HTML/CSS: how to put text both right and left aligned in a paragraph

What would be the best code to have two bits of text in a single paragraph, one left aligned, the other right aligned, that also is: 在单个段落中包含两段文本,一个左对齐,另一个右对齐的最佳代码是:

  • the least code as possible 最少的代码
  • the easiest for clients to render (ie using least resources) 最容易呈现给客户(即使用最少的资源)

I add this last one to be sure <table><tr><td></td><td align=right></td></tr></table> would get ruled out. 我添加了最后一个,以确保<table><tr><td></td><td align=right></td></tr></table>被排除在外。 Not only is this a beast of code compared to a couple of properly styled <div> , <span> or <p> 's, it's also a beast if you know what a HTML render engine has to load and calculate to decide on the cell sizes before it even get's to painting text in them... 与几个正确样式的<div><span><p>相比,这不仅是代码的野兽,而且如果您知道HTML渲染引擎必须加载并计算决定使用哪种HTML渲染引擎,它也是一种野兽。甚至在开始在其中绘制文本之前的单元格大小...

If you're not sure what I mean, here's an example: a page footer with left aligned the name of the user currently logged on, and on the same line right aligned the current date and time and/or website version. 如果您不确定我的意思,请看以下示例:页面页脚的左对齐当前登录用户的名称,而同一行右对齐当前日期和时间和/或网站版本。

Least amount of markup possible (you only need one span): 标记数量最少(您只需要一个跨度):

<p>This text is left. <span>This text is right.</span></p>

How you want to achieve the left/right styles is up to you, but I would recommend an external style on an ID or a class. 如何实现左右样式由您决定,但是我建议在ID或类上使用外部样式。

The full HTML: 完整的HTML:

<p class="split-para">This text is left. <span>This text is right.</span></p>

And the CSS: 和CSS:

.split-para      { display:block;margin:10px;}
.split-para span { display:block;float:right;width:50%;margin-left:10px;}

The only half-way proper way to do this is 唯一可行的方法是

<p>
  <span style="float: right">Text on the right</span>
  <span style="float: left">Text on the left</span>
</p> 

however, this will get you into trouble if the text overflows. 但是,如果文本溢出,这会给您带来麻烦。 If you can, use div s (block level elements) and give them a fixed width . 如果可以,请使用div (块级元素)并为其设置固定的width

A table (or a number of div s with the according display: table / table-row / table-cell properties) would in fact be the safest solution for this - it will be impossible to break, even if you have lots of difficult content. 实际上,一个表(或带有相应display: table / table-row / table-cell的多个div display: table / table-row / table-cell属性)将是最安全的解决方案-即使您有很多困难的内容,也无法打破。

I wouldn't put it in the same <p> , since IMHO the two infos are semantically too different. 我不会将其放在相同的<p> ,因为恕我直言,这两个信息在语义上过于不同。 If you must, I'd suggest this: 如果需要,我建议这样做:

<p style="text-align:right">
 <span style="float:left">I'll be on the left</span>
 I'll be on the right
</p>

I have used this in the past: 我过去曾经使用过:

html html

January<span class="right">2014</span>

Css 的CSS

.right {
    margin-left:100%;
}

Demonstration 示范

在此处输入图片说明

If the texts has different sizes and they must be underlined this is the solution: 如果文本大小不同 ,必须在其下划线,则可以解决此问题:

<table>
  <tr>
    <td class='left'>January</td>
    <td class='right'>2014</td>
  </tr>
</table>

css: CSS:

table{
    width: 100%;
    border-bottom: 2px solid black;
    /*this is the size of the small text's baseline over part  (≈25px*3/4)*/
    line-height: 19.5px; 
}
table td{
    vertical-align: baseline;
}
.left{
    font-family: Arial;
    font-size: 40px;
    text-align: left;

}
.right{
    font-size: 25px;
    text-align: right;
}

demo here 在这里演示

Ok what you probably want will be provide to you by result of: 好吧,您可能想要的将通过以下结果提供给您:

  1. in CSS: 在CSS中:

    div { column-count: 2; div {column-count:2; } }

  2. in html: 在html中:

    < div> some text, bla bla bla < /div> < div>一些文字,bla bla bla < / div>

In CSS you make div to split your paragraph on to column, you can make them 3, 4... 在CSS中,使div将段落拆分为列,可以将其设为3、4 ...

If you want to have many differend paragraf like that, then put id or class in your div: 如果您希望拥有许多这样的differend paragraf,则将id或class放入div中:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM