【问题标题】:How to make table cell expand based on content如何使表格单元格根据内容展开
【发布时间】:2017-01-14 22:33:36
【问题描述】:

我想在一张桌子旁边放 3 个 div。表格应该根据里面的内容展开,3个div应该平均占用剩余的空间。

我需要 div 像这样在移动视图的桌子下堆叠...

Desktop:
[div-1] [div-2] [div-3] [table]

Mobile:
[table]
[div-1]
[div-2]
[div-3]

我一直在玩弄 Flex 以使其正常工作,但我无法让表格根据里面的内容展开。

我不能使用td {white-space: nowrap;},因为在移动设备上它会将文本从屏幕一侧推出。我需要根据里面的内容来扩展表格,但内容不应该被强制到屏幕之外。它需要像平常一样在较小的屏幕上环绕。

这是我的工作示例:http://codepen.io/anon/pen/qadbkK?editors=1100

/* Do this on tablet size and up */

@media (min-width: 481px) {
  /* Main outer div of helper items AND subtotals */
  .cart-collaterals {
    display: inline-flex;
  }
  /* Outer div of helper items */
  .cart-helper-outer {
    order: -1;
    display: inline-flex;
    justify-content: space-around;
  }
  /* Helper item width */
  .cart-helper-inner {
    max-width: 25%;
  }
  /* Helper item font */
  .cart-helper-inner p {
    text-align: center;
  }
}
<div class="cart-collaterals">

  <div class="cart_totals">
    <table class="shop_table" cellspacing="0">
      <tbody>
        <tr class="cart-subtotal">
          <th>Subtotal</th>
          <td data-title="Subtotal">$ 348</td>
        </tr>
        <tr class="shipping">
          <th>Shipping</th>
          <td data-title="Shipping">
            The table should expand so that this is one line of text
          </td>
        </tr>
        <tr class="order-total">
          <th>Total</th>
          <td data-title="Total">$ 348</td>
        </tr>
      </tbody>
    </table>
  </div>

  <div class="cart-helper-outer">
    <div class="cart-helper-inner">
      <p>This is some text. Blah blah blah. Here is some text.</p>
    </div>
    <div class="cart-helper-inner">
      <p>This is some text. Blah blah blah. Here is some text.</p>
    </div>
    <div class="cart-helper-inner">
      <p>This is some text. Blah blah blah. Here is some text.</p>
    </div>
  </div>

</div>

【问题讨论】:

标签: html css


【解决方案1】:

white-space: nowrap 设置为td,以便将整个文本放在一行中。

/* Do this on tablet size and up */

@media (min-width: 481px) {
  /* Main outer div of helper items AND subtotals */
  .cart-collaterals {
    display: inline-flex;
  }
  /* Outer div of helper items */
  .cart-helper-outer {
    order: -1;
    display: inline-flex;
    justify-content: space-around;
  }
  /* Helper item width */
  .cart-helper-inner {
    max-width: 25%;
  }
  /* Helper item font */
  .cart-helper-inner p {
    text-align: center;
  }
}

td{
  white-space: nowrap;
}
<div class="cart-collaterals">

  <div class="cart_totals">
    <table class="shop_table" cellspacing="0">
      <tbody>
        <tr class="cart-subtotal">
          <th>Subtotal</th>
          <td data-title="Subtotal">$ 348</td>
        </tr>
        <tr class="shipping">
          <th>Shipping</th>
          <td data-title="Shipping">
            The table should expand so that this is one line of text
          </td>
        </tr>
        <tr class="order-total">
          <th>Total</th>
          <td data-title="Total">$ 348</td>
        </tr>
      </tbody>
    </table>
  </div>

  <div class="cart-helper-outer">
    <div class="cart-helper-inner">
      <p>This is some text. Blah blah blah. Here is some text.</p>
    </div>
    <div class="cart-helper-inner">
      <p>This is some text. Blah blah blah. Here is some text.</p>
    </div>
    <div class="cart-helper-inner">
      <p>This is some text. Blah blah blah. Here is some text.</p>
    </div>
  </div>

</div>

【讨论】:

  • white-space: nowrap 的问题是表格内容在移动设备上被推出屏幕边缘。抱歉,我应该在这个问题上说清楚。已更新。
  • 实际上,只需将其包装在媒体查询中即可!
猜你喜欢
  • 2012-07-09
  • 2021-07-15
  • 1970-01-01
  • 2017-02-25
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 1970-01-01
相关资源
最近更新 更多