对齐表格单元格中的表格

时间:2021-11-18 22:22:37

I have the following html, where the ERROR table is centered not on the right. The other content, which is not a (sub) table is aligned correctly according to their class.

我有以下html,其中ERROR表不在右侧居中。其他内容(不是(子)表)根据其类正确对齐。

<table>
<tr>
  <td class="r">a</td>
  <td>b</td>
  <td class="l">c</td>
</tr>
<tr>
 <td class="r">
    <table class="r" id="ERROR">
    <tr>
      <td>e</td>
      <td>f</td>
      <td>g</td>
    </tr>
    </table>  
 </td>
 <td>h</td>
 <td class="l">i</td>
</tr>
</table>

This css use:

这个css使用:

td.r {
    text-align: right;
}
td.l{
    text-align: left;
}

Missing something in the code? I can align a table inside another? Thanks

在代码中遗漏了什么?我可以在另一张桌子内对齐吗?谢谢

2 个解决方案

#1


1  

If you set display: inline-table to your inner table, then it will behave properly to the text align settings that you set for the table cells.

如果将display:inline-table设置为内部表,则它将正确表现为您为表格单元格设置的文本对齐设置。

The text-align property affects inline elements, not block level elements.

text-align属性影响内联元素,而不影响块级元素。

table {
  width: 100%;
}
table.inner {
  display: inline-table;
  width: auto;
  border: 1px solid blue;
}
td {
  border: 1px dotted gray;  
}
td.r {
  text-align: right;
}
td.l {
  text-align: left;
}
<table>
  <tr>
    <td class="r">a</td>
    <td>b</td>
    <td class="l">c</td>
  </tr>
  <tr>
    <td class="r">
      <table class="inner" id="ERROR">
        <tr>
          <td>e</td>
          <td>f</td>
          <td>g</td>
        </tr>
      </table>
    </td>
    <td>h</td>
    <td class="l">i</td>
  </tr>
</table>

#2


0  

in the css when you want to modify a class try to mark it as .classname much like how id is #idname so in your case try changing your td.r to just .r and your td.l to just .l

在css中,当你想要修改一个类时,试着将它标记为.classname,就像id是#idname一样,所以在你的情况下尝试将你的td.r改为.r而你的td.l改为.l

#1


1  

If you set display: inline-table to your inner table, then it will behave properly to the text align settings that you set for the table cells.

如果将display:inline-table设置为内部表,则它将正确表现为您为表格单元格设置的文本对齐设置。

The text-align property affects inline elements, not block level elements.

text-align属性影响内联元素,而不影响块级元素。

table {
  width: 100%;
}
table.inner {
  display: inline-table;
  width: auto;
  border: 1px solid blue;
}
td {
  border: 1px dotted gray;  
}
td.r {
  text-align: right;
}
td.l {
  text-align: left;
}
<table>
  <tr>
    <td class="r">a</td>
    <td>b</td>
    <td class="l">c</td>
  </tr>
  <tr>
    <td class="r">
      <table class="inner" id="ERROR">
        <tr>
          <td>e</td>
          <td>f</td>
          <td>g</td>
        </tr>
      </table>
    </td>
    <td>h</td>
    <td class="l">i</td>
  </tr>
</table>

#2


0  

in the css when you want to modify a class try to mark it as .classname much like how id is #idname so in your case try changing your td.r to just .r and your td.l to just .l

在css中,当你想要修改一个类时,试着将它标记为.classname,就像id是#idname一样,所以在你的情况下尝试将你的td.r改为.r而你的td.l改为.l