简体   繁体   English

右对齐并左对齐同一HTML表格单元格中的文本

[英]Right align and left align text in same HTML table cell

I have a cell in an HTML <table> . 我在HTML <table>有一个单元格。 I would like part of the cell contents to be left justified and part to be right justified. 我希望细胞内容的一部分是左对齐的,而部分是正确的。 Is this possible? 这可能吗?

If you want them on separate lines do what Balon said. 如果你想把它们放在不同的线上,请按照Balon所说的做。 If you want them on the same lines, do: 如果您想要它们在同一行,请执行以下操作:

<td>
  <div style="float:left;width:50%;">this is left</div>
  <div style="float:right;width:50%;">this is right</div>
</td>

I came up with this while trying to figure out how to display currency ('$' to left, number to right) in table cells: 我试图弄清楚如何在表格单元格中显示货币(左边的'$',右边的数字),我想出了这个:

<div class="currency">20.34</div>

.currency {
   text-align: right;
}

.currency:before {
   content: "$";
   float: left;
   padding-right: 4px;
}

It is possible but how depends on what you are trying to accomplish. 这是可能的,但取决于你想要完成什么。 If it's this: 如果是这样的话:

| | Left-aligned Right-aligned | 左对齐右对齐| in one cell then you can use floating divs inside the td tag: 在一个单元格中,您可以在td标记内使用浮动div:

<td>
<div style='float: left; text-align: left'>Left-aligned</div>
<div style='float: right; text-align: right'>Right-aligned</div>
</td>

If it's | 如果是的话 Left-aligned 左对齐
Right Aligned | 右对齐|

Then Balon's solution is correct. 然后Balon的解决方案是正确的。

If it's: | 如果是:| Left-aligned | 左对齐| Right-Aligned | 右对齐|

Then it's: 那就是:

<td align="left">Left-aligned</td>
<td align="right">Right-Aligned</td>

Do you mean like this? 你的意思是这样的吗?

<!-- ... --->
<td>
   this text should be left justified
   and this text should be right justified?
</td>
<!-- ... --->

If yes 如是

<!-- ... --->
<td>
   <p style="text-align: left;">this text should be left justified</p>
   <p style="text-align: right;">and this text should be right justified?</p>
</td>
<!-- ... --->

Tor Valamo's answer with a little contribution form my side: use the attribute "nowrap" in the "td" element, and you can remove the "width" specification. Tor Valamo的答案有点贡献在我的身边:在“td”元素中使用属性“nowrap”,你可以删除“宽度”规范。 Hope it helps. 希望能帮助到你。

<td nowrap>
  <div style="float:left;">this is left</div>
  <div style="float:right;">this is right</div>
</td>

td style is not necessary but will make it easier to see this example in browser td样式不是必需的,但可以更容易在浏览器中看到此示例

<table>
 <tr>
  <td style="border: 1px solid black; width: 200px;">
  <div style="width: 50%; float: left; text-align: left;">left</div>
  <div style="width: 50%; float: left; text-align: right;">right</div>
  </td>
 </tr>
</table>

确定但您需要将这些“块”包装在单独的标签中并将对齐应用于这些标签。

You could use something like: 你可以使用类似的东西:

<td> 
  <div style="float:left;width:49%;text-align:left;">this is left</div> 
  <div style="float:right;width:49%;text-align:right;">this is right</div> 
</td>

The 49% is to give some room for the renderer to wrap things around. 49%是为渲染器提供一些空间来包装物品。

And you can use either <div> or <span> 您可以使用<div><span>

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

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