javascript - CSS child 100% INNER 宽度

标签 javascript jquery html css

我有一种情况需要指定子级的宽度是父级内部宽度的 100%,而不是外部宽度,这意味着父级水平滚动。

情况看起来与此类似:

http://codepen.io/anon/pen/ygqPZG?editors=1100

HTML

<div id='parent'>
  <table id='child1'>
    <colgroup>
      <col width='400px'></col>
    </colgroup>
    <tbody>
      <tr>
        <td>Child1withareallyreallylongtitle</td>
      </tr>
    </tbody>
  </table>
  <div id='child2'>
    <p>Child 2</p>
  </div>
</div>

CSS

#parent {
  margin: 16px;
  border: 1px solid black;
  box-sizing: border-box;
  overflow-x: auto;
}

#child1 {
  width: 100%;
  border: 1px solid red;
}

#child2 {
  width: 100%;
  border: 1px solid blue;
}

当您将屏幕缩小到足够小以至于表格(chld 1)停止缩小并迫使父级溢出并因此显示滚动条时,第二个子级仍然保留父级 OUTER 宽度的 100%,我想它是父级 INNER 宽度的 100%,以便它与第一个子级(表)的宽度相同。

我想尽可能保持纯 CSS 的答案,只要 JavaScript 不依赖于窗口调整大小事件就可以了,因为 #parent 可能会因其他原因而缩小(水平兄弟增长)。

最佳答案

您必须将长标题保持为一行吗?如果没有,你可以试试这个代码;

    #parent {
      margin: 16px;
      border: 1px solid black;
      box-sizing: border-box;
       box-sizing: border-box;
    }

    #child1 {
      width: 100%;
      border:1px solid red;
      box-sizing: border-box;
      white-space: pre-wrap;
      word-break: break-all;
      word-wrap: break-word;
    }

    #child2 {
      border:1px solid blue;
      box-sizing: border-box;
    }
    <div id='parent'>
      <table id='child1'>
        <colgroup>
          <col width='400px'></col>
        </colgroup>
        <tbody>
          <tr>
            <td>Child1withareallyreallylongtitle</td>
          </tr>
        </tbody>
      </table>
      <div id='child2'>
        <p>Child 2</p>
      </div>
    </div>

关于javascript - CSS child 100% INNER 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42080563/

相关文章:

javascript - Vimeo API 无法处理多个视频

jquery - 如何修复引导 Accordion 使其表现正常?

html - Bootstrap Navbar 折叠图标未出现

javascript - 在 .html() 方法中使用 jQuery inside image 标签

javascript - Google Apps 脚本拆分 ('\n' ) 不适用于 Google 管理目录中的数据

javascript - React (NextJS) 函数未定义

javascript - 从javascript多次调用ajax

javascript - jQuery 数据表 row.add 没有添加隐藏的 td 属性

javascript - 如何在 Joomla 模块中添加 javascript

javascript - 用户界面上的排列元素