Html 在一个表格内右对齐一列,左对齐另一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7281482/
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 10:24:42  来源:igfitidea点击:

Right aligning one column and left aligning the other, within one table

htmlcsshtml-tabletext-alignment

提问by smurf

I have created a html table with 2 columns. I want the first column containing text to be right aligned, but i want the second column to stay left aligned as it contains check boxes. For presentation it would look, but ive no idea. I have tried creating a td class="column1" then text-align: right; in the CSS but no luck.

我创建了一个包含 2 列的 html 表。我希望包含文本的第一列右对齐,但我希望第二列保持左对齐,因为它包含复选框。对于演示,它会看起来,但我不知道。我尝试创建一个 td class="column1" 然后 text-align: right; 在 CSS 中,但没有运气。

Any ideas would be greatly appreciated.

任何想法将不胜感激。

HTML

HTML

<td class="column1">Retrieve Logs:</td>
<td class="column2"><input type=checkbox name="getcamlogs" checked="checked"></td>

CSS

CSS

td.column1 {
   text-align: right;
}

采纳答案by Graeme Leighfield

Here you go, this should work :)

给你,这应该有效:)

<style>
    #mytable {width:500px; margin:0 auto;}
    #mytable td {width:250px;}
    #mytable .left {text-align:right; background:#333;}
    #mytable .right {text-align:left; background:#666;}
</style>

<table id="mytable">
    <tr>
        <td class="left">1</td>
        <td class="right">2</td>
    </tr>   
</table>