在 TD 内将 DIV 与同一行上的其他元素对齐

Align a DIV along other elements on the same line, inside a TD

我需要第 3 列看起来像第 1 列。

我看到它的行为方式是因为 DIV。如果我把它拿出来,它就可以了。

条件:

我可以将 float: leftdisplay: inline-block 添加到 DIV,但我还需要 select 来拉伸。

我正在使用Bootstrap,所以可以推荐它,因为它满足标准。

table {
  background: yellow;
}

td {
  border: red 2px solid;
}
  <table>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
    </tr>
    <tr>
      <td>
        <select name="person">
          <option value="1">Item 1</option>
        </select>
        <button type="button">ADD</button>
      </td>
      <td>
        <input type="text" value="100">
      </td>
      <td>
        <div>
          <select name="person">
            <option value="1">Item 1</option>
          </select>
        </div>
        <button type="button">ADD</button>
      </td>
    </tr>
  </table>

试试这个 CSS:

table {
   background: yellow;
}

td {
   border: red 2px solid;
}

table th:nth-child(3) {
   width:50%;
}

table td:nth-child(3) div{
   width:80%;
   float:left;
   display: table;
}

table td:nth-child(3) button{
   width:20%;
}

table td:nth-child(3) select{
   width:100%;
}

您可以通过以下方式完成此操作:

  1. 使第三个 td 成为弹性容器。
  2. 仅使其 div 子元素成为弹性元素。
  3. select 设置为 100% 宽度。
  4. 将第三个 th 设置为 50% 宽度。

片段:

table {
  background: yellow;
}

td {
  border: red 2px solid;
}

td:nth-child(3) {
  display: flex;
}

td:nth-child(3) div {
  flex: 1;
}

td:nth-child(3) select {
  width: 100%;
}

th:nth-child(3) {
  width: 50%;
}
<table>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
    </tr>
    <tr>
      <td>
        <select name="person">
          <option value="1">Item 1</option>
        </select>
        <button type="button">ADD</button>
      </td>
      <td>
        <input type="text" value="100">
      </td>
      <td>
        <div>
          <select name="person">
            <option value="1">Item 1</option>
          </select>
        </div>
        <button type="button">ADD</button>
      </td>
    </tr>
  </table>