Html 在表格单元格中对齐 DIV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3336354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:53:52  来源:igfitidea点击:

Aligning a DIV within a table cell

htmlcss

提问by user336786

I have an HTML table with three columns. In the right most column, I want to right-align the content. In an attempt to do this, I have the following:

我有一个包含三列的 HTML 表格。在最右边的列中,我想右对齐内容。为了做到这一点,我有以下几点:

<table border='0' cellpadding='0' cellspacing='0' style='width:100%;'>
  <tr>
    <td>Content 1</td>
    <td>Content 2</td>
    <td style='text-align-right'>Content 3</td>
  </tr>
</table>

The content in the third cell is actually generated via some server-side code. When the generate content is text, the content is aligned properly. However, when I attempt to right-align a DIV element that is within the third cell, it does not do it. The DIV is always left-aligned. How do I right-align a DIV within a table cell?

第三个单元格中的内容实际上是通过一些服务器端代码生成的。当生成内容为文本时,内容正确对齐。但是,当我尝试右对齐第三个单元格内的 DIV 元素时,它不会这样做。DIV 始终左对齐。如何在表格单元格中右对齐 DIV?

Thank you!

谢谢!

回答by Sarfraz

Your code should look like this to meet both div and non-div situations:

你的代码应该是这样的,以满足 div 和非 div 的情况:

<td style="text-align:right;">
  <div style="float: right;" align="right"></div>
</td>

回答by cesarnicola

Put a class to the td

把一个类放到 td

<td class="rightAlign">

and define a css class with the text-align property with !important

并使用 !important 定义一个带有 text-align 属性的 css 类

.rightAlign{
    text-align: right !important
}

this will make the DIV inherit the right alignment.

这将使 DIV 继承正确的对齐方式。

回答by fantactuka

Try:

尝试:

<td><div style="float: right;"></div></td>