Html 如何使用 CSS 将整个表格向右对齐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22285633/
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
How to align entire table to right with CSS?
提问by Dims
How to align entire table to right with CSS?
如何使用 CSS 将整个表格向右对齐?
margin-rightdoes not help
margin-right没有帮助
Here: http://jsfiddle.net/dimskraft/Y2FKy/
在这里:http: //jsfiddle.net/dimskraft/Y2FKy/
HTML:
HTML:
<table class="block logo">
<colgroup>
<col style="width:50%"/>
<col style="width:50%"/>
</colgroup>
<tr>
<td>something</td>
<td>
<table class="menu">
<tr>
<td>Item 1</td>
<td>Item 2</td>
</tr>
</table>
</td>
</tr>
</table>
CSS:
CSS:
table {
width: 100%;
}
table.menu {
width: auto;
margin-right: 0px;
}
回答by Quentin
You need to set the left margin to autotoo. That will make the left margin push the table as far right as is allowed by the right margin.
您也需要将左边距设置为auto。这将使左边距将表格推到右边距允许的最右边。
table {
width: 100%;
}
table, td {
border: solid black 1px;
}
table.menu {
width: auto;
margin-right: 0px;
margin-left: auto;
}
<table class="block logo">
<colgroup>
<col style="width:50%"/>
<col style="width:50%"/>
</colgroup>
<tr>
<td>something</td>
<td>
<table class="menu">
<tr>
<td>Item 1</td>
<td>Item 2</td>
</tr>
</table>
</td>
</tr>
</table>
回答by Eddie C
Easy -
简单 -
table {
width: 60%; /* whichever width you want */
margin-left: 40%; /* minus the amount to make it 100% */
}
回答by TrungDQ
Use float: rightinstead:
使用float: right来代替:
table.menu {
width: auto;
float: right;
}
JSFiddle
JSFiddle
Solution 2:
解决方案2:
As floatis a not a good practice to align elements, you can use <td style="text-align: right">and display: inline-tablein the table:
由于float对齐元素不是一个好习惯,您可以在表中使用<td style="text-align: right">和display: inline-table:
<td style="text-align: right">
<table border="1" class="menu">
<tr>
<td>Item 1</td>
<td>Item 2</td>
</tr>
</table>
</td>
table.menu {
width: auto;
display: inline-table;
}

