如何在启用溢出并包含大的div内部时避免td宽度扩展

huangapple go评论77阅读模式
英文:

How to avoid td width expanding when it wrapped with enabled overflow and big div inside

问题

我有一个包含2列的表格,第一列应该占25%的宽度,第二列应该占75%的宽度。

在第二列中,有时会有大内容,因此我在里面创建了一个带有overflow: scroll的div,并在该包装div内创建了大内容,但由于某种原因,它会扩展整个第二个td列,例如:

<div class="content">
  <table>
    <tr>
      <td class="col1">Hello<td>
      <td class="col2">World</td>
    </tr>
    <tr>
      <td class="col1">big field<td>
      <td class="col2">
        <div class="wrapper">
          <div class="big_content">some big content here</div>
        </div>
      </td>
    </tr>
  </table>
</div>

我如何防止第二列由于包含大内容而扩展?因为我将大内容包装在具有overflow: scroll的另一个div(.wrap)中。

英文:

I have a table with 2 columns, the first one should be 25% wide width and and 2nd one should be 75% wide.

In the 2nd column, sometimes there's big content, therefore, I made a div inside with overflow: scroll and inside that wrap div I made the big content, but from some reason, it expands the full 2nd td column, an example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

table {
	background: red;
	width: 100%;
}

.col1 {
	width: 25%;
	background: green;
}

.col2 {
	width: 75%;
	background: skyblue;
}

.wrapper {
	width: 100%;
	height: 200px;
	overflow: scroll;
}

.big_content {
	width: 2000px;
	height: 500px;
}

<!-- language: lang-html -->

&lt;div class=&quot;content&quot;&gt;
  &lt;table&gt;
  &lt;tr&gt;
	  &lt;td class=&quot;col1&quot;&gt;Hello&lt;td&gt;
	  &lt;td class=&quot;col2&quot;&gt;World&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
	  &lt;td class=&quot;col1&quot;&gt;big field&lt;td&gt;
	  &lt;td class=&quot;col2&quot;&gt;
		&lt;div class=&quot;wrapper&quot;&gt;
			&lt;div class=&quot;big_content&quot;&gt;some big content here&lt;/div&gt;
		&lt;/div&gt;
	  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
&lt;/div&gt;

<!-- end snippet -->

How can I avoid the 2nd column from being expanded due to the big content it has? because I wrapped that big content inside another div (.wrap) that has overflow: scroll

答案1

得分: 1

使用table-layout:fixed来设置表格的布局:

table {
    background: red;
    width: 100%;
    table-layout: fixed;
}

.col1 {
    width: 25%;
    background: green;
}

.col2 {
    width: 75%;
    background: skyblue;
}

.wrapper {
    width: 100%;
    height: 200px;
    overflow: scroll;
}

.big_content {
    width: 2000px;
    height: 500px;
}
<div class="content">
  <table>
    <tr>
      <td class="col1">Hello<td>
      <td class="col2">World</td>
    </tr>
    <tr>
      <td class="col1">big field<td>
      <td class="col2">
        <div class="wrapper">
          <div class="big_content">some big content here</div>
        </div>
      </td>
    </tr>
  </table>
</div>
英文:

use table-layout:fixed for table:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->

table {
    background: red;
    width: 100%;
    table-layout:fixed
}

.col1 {
    width: 25%;
    background: green;
}

.col2 {
    width: 75%;
    background: skyblue;
}

.wrapper {
    width: 100%;
    height: 200px;
    overflow: scroll;
}

.big_content {
    width: 2000px;
    height: 500px;
}

<!-- language: lang-html -->

&lt;div class=&quot;content&quot;&gt;
  &lt;table&gt;
  &lt;tr&gt;
	  &lt;td class=&quot;col1&quot;&gt;Hello&lt;td&gt;
	  &lt;td class=&quot;col2&quot;&gt;World&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
	  &lt;td class=&quot;col1&quot;&gt;big field&lt;td&gt;
	  &lt;td class=&quot;col2&quot;&gt;
		&lt;div class=&quot;wrapper&quot;&gt;
			&lt;div class=&quot;big_content&quot;&gt;some big content here&lt;/div&gt;
		&lt;/div&gt;
	  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月9日 16:56:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866100-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定