生成不相同的 table 行(table 内 table 内 table)

Making non identical table rows (table within table within table)

我在创建与其上方的行不同的行时遇到问题。 我想要一行有 3 个单元格,在这一行下面扩展到行尾,依此类推。第一行 - 3 个单元格,第二行 - 1 个长单元格(长度等于上面三个单元格)并重复。

这是我使用的 HTML & CSS 模板

#table {
  margin: 0px;
  margin-left: 585px;
  margin-top: 50px;
  z-index: 2;
  width: 385;
  border-collapse: collapse;
}

.divStyle {
  /* in charge of the scroller*/
  width: 385px;
  height: 428px;
  overflow: auto;
}

.innerTables {
  width: 360;
  border-collapse: collapse;
  vertical-align: middle;
}

.productionTable tr td {
  width: 360px;
  border: 2px solid black;
}

.innerTables tr td,
th {
  margin: 0px;
  padding: 0px;
  width: 120px;
}

.innerTables tr td {
  border: 1px solid black;
}
<table id='table'>
  <tr>
    <td>
      <table class='innerTables'>
        <tr>
          <th>Building</th>
          <th>Amount</th>
          <th>1 Per</th>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td>
      <div class='divStyle'>
        <table class='innerTables'>
          <tr>
            <td>Fishery</td>
            <td class='inputText' id="fishery"></td>
            <td>800 Farmers</td>
          </tr>
          <tr>
            <td colspan="0">
              <table class='productionTable'>
                <tr>
                  test
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </div>
    </td>
  </tr>
</table>

我尝试使用边框来指示其是否有效。

This is what I try to achieve

This is what I achieve

最好的方法是使用 colspan 或 rowspan。在那里阅读更多 https://www.w3schools.com/tags/att_td_rowspan.asp

<tr>
 <td colspan="3">
  <table class='productionTable'>

使用colspan扩展列宽

我做了一个fiddle给你https://jsfiddle.net/vzy97s2a/

                    <tr>
                      <td colspan="3">
                        wider
                      </td>
                    </tr>

我会说使用 colspan="3" 作为其他答案,但是如果您希望它是动态的并且您不想将 colspan 添加到每个 table 单元格中,您可以尝试使用这种方法

.row{
    display: flex;
}

.column{
    border: 1px solid red;
}

.table> .row:nth-child(odd) > .column{
    width: 33.3%;
    flex-grow: 1;
}

.table> .row:nth-child(even) > .column{
    width: 100%;
}
<div class="table">
    <div class="row">
        <div class="column">test</div>
        <div class="column">test</div>
        <div class="column">test</div>
    </div>
    <div class="row">
        <div class="column">test</div>
    </div>
    <div class="row">
        <div class="column">test</div>
        <div class="column">test</div>
        <div class="column">test</div>
    </div>
    <div class="row">
        <div class="column">test</div>
    </div>
</div>

您可以改用 colspan 属性

<table id='table'>
    <tr>
        <td>
            <table class='innerTables'>
                <tr>
                    <th>Building</th>
                    <th>Amount</th>
                    <th>1 Per</th>
                </tr>
                <tr>
                    <td>Fishery</td>
                    <td class='inputText' id="fishery"></td>
                    <td>800 Farmers</td>
                </tr>
                <tr>
                  <td colspan="3">
                    Test
                  </td>
                </tr>
            </table>
        </td>
    </tr>
</table>