简体   繁体   English

为什么在表格单元中绝对定位的元素要包装?

[英]Why absolutely positioned element in a table cell is wrapping?

Consider the following: (Live Demo) 请考虑以下内容:( 实时演示)

HTML: HTML:

<table>
  <tbody>
    <tr>      
      <td class="col1">
        <button class="btn btn-small">Do Something</button> 
      </td>
      <td class="col2"></td>
    </tr>
    <tr>
      <td class="col1"></td>
      <td class="col2">
        <span style="position: relative">
          Text 
          <button class="btn btn-small" 
                  style="position: absolute; 
                         top: 0; 
                         left: 210px">
            Do Something
          </button>
        </span>
      </td>
    </tr>
  </tbody>
</table>

CSS: CSS:

tr {
  height: 100px;
}
td {
  border: 1px solid black;
}
.col1 {
  width: 150px;
}
.col2 {
  width: 200px;
}

Why the absolutely positioned button is wrapping? 为什么绝对定位的按钮要包装?

I could solve it by: 我可以通过以下方法解决它:

button {
  white-space: nowrap;
}

but I'm really interested to know why this wrapping is happening, and whether there is a better way to solve this. 但我真的很想知道为什么要进行这种包装,以及是否有更好的方法来解决此问题。

It is wrapping to avoid overflowing the parent container in this case. 在这种情况下,它将进行包装以避免溢出父容器。 It is still overflowing it because you have too long of a single word, but the behavior is to try to not overflow it. 它仍然溢出它,因为单个单词的时间太长,但是行为是尝试不使其溢出。 If you change your demo to have more text in your other button you would see it wraps as well once it exceeds the table cell width. 如果将演示更改为在其他按钮中包含更多文本,则超出表格单元格宽度后,它也会自动换行。

Use the white-space: nowrap if you want to avoid this. 如果要避免这种情况,请使用white-space: nowrap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM