简体   繁体   English

可滚动容器内的表格仍会导致容器宽度增加

[英]Table inside scrollable container still causes container width to grow

I am using Bootstrap grid. 我正在使用Bootstrap网格。 I am currently facing a strange issue with displaying tables inside Bootstrap column. 我目前在Bootstrap列内显示表格时遇到一个奇怪的问题。

I have a table with some columns, displayed inside a div (table container). 我有一个带有一些列的表,显示在div(表容器)内。 The table container has col-md-9 bootstrap class, so its width is set to 75%. 该表容器具有col-md-9引导程序类,因此其宽度设置为75%。 However, at a certain point if the table contains too much content and/or column, it causes the container's width to expand. 但是,在某些情况下,如果表中包含过多的内容和/或列,则会导致容器的宽度扩大。 Here is a standalone example: https://output.jsbin.com/xowihu 这是一个独立的示例: https : //output.jsbin.com/xowihu

Here is a screenshot. 这是屏幕截图。 Notice the gray box getting pushed outside, causing the layout to look broken: 注意灰色框被推到外面,导致布局看起来很破损: 问题

And, when I removed some columns from the table, it works and looks as expected: 而且,当我从表中删除了一些列时,它可以正常工作并看起来像预期的那样: 预期

The thing that confuses me is that, I have already specified overflow: scroll on the table container. 让我感到困惑的是,我已经指定了overflow: scroll在表容器上overflow: scroll Why is content length affecting container width? 为什么内容长度会影响容器宽度?

I have tried: 我努力了:

  • Setting table-layout: fixed on the table, and word-wrap: break-word on the table cells. 设置table-layout: fixed在表格上, word-wrap: break-word表格中的word-wrap: break-word This works, however the table contents are visually horrible; 这行得通,但是表格的内容在视觉上是可怕的。 one word is broken into 3 lines, etc. 一个字分成三行,依此类推。
  • Setting max-width on the table container works, but it has to be in px unit. 在表容器上设置max-width可以,但是必须以px单位。 This might be last resort (and will be applied via Javascript) since site has various layout widths. 由于站点具有各种布局宽度,因此这可能是最后的选择(并且将通过Java语言应用)。

EDIT Link to code: https://jsbin.com/xowihu/edit 编辑链接到代码: https : //jsbin.com/xowihu/edit

When I put the <div class="row"> that was inside the <table class="table"> outside the table,tr,td it works just fine.. 当我将<div class="row">放在<table class="table">外的<table class="table">内时,tr,td可以正常工作。

<div class="col-md-7 slide-content">
    <p>
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </p>
    <div class="row">
              <div class="col-sm-9">
                <div class="row">
                  <div class="col-sm-2" style="height: 250px; background-color: lightgreen">Some Label</div>
                  <div class="col-sm-9 scrollable">
                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Exercitationem libero sequi tempore necessitatibus tempora, porro, iure, nisi dicta, rerum enim natus facilis voluptatum soluta dolore molestias vero odit eaque veniam.</p>
                    <table class="table" border="1" cellpadding="1" cellspacing="1">
                      <tbody>
                        <tr>
                          <td>Hello</td>
                          <td>Lipsumdolor</td>
                          <td>Lipsum Gen</td>
                          <td>Lorem ipsum dolor sit amet</td>
                          <td>Lorem ipsum dolor</td>
                          <td>Lorem ipsum</td>
                          <td>Lorem ipsum</td>
                          <td>Lorem ipsum</td>
                          <td>Lorem ipsum</td>
                        </tr>
                        <tr>
                          <td>Hello</td>
                          <td>Lipsumdolor</td>
                          <td>LipsumLipsumLipsumLipsum</td>
                          <td>Lorem ipsum dolor sit amet</td>
                          <td>Lorem ipsum dolor</td>
                          <td>Lorem ipsum</td>
                          <td>Lorem ipsum</td>
                          <td>Lorem ipsum</td>
                          <td>Lorem ipsum</td>
                        </tr>
                        <tr>
                          <td>Hello</td>
                          <td>Lipsumdolor</td>
                          <td>Lipsum Gen</td>
                          <td>Lorem ipsum dolor sit amet</td>
                          <td>Lorem ipsum dolor</td>
                          <td>qwertyuiop</td>
                          <td>qwertyuiop</td>
                          <td>qwertyuiop</td>
                          <td>qwertyuiop</td>
                        </tr>
                      </tbody>
                    </table>
                  </div>

                </div>
              </div>
              <div class="col-sm-3">
                <img src="https://placehold.it/120x120"><br>
                <a href="#">Click to enlarge</a>
              </div>
            </div><table class="table">
      <tbody>
        <tr>
          <td>

          </td>
        </tr>
      </tbody>
    </table>
  </div>

SO everything that I did was, get the div class="row" that is inside the 所以我所做的一切都是让div class =“ row”位于

<table class="table">
  <tr>
    <td>

And put it underneath the 放在下面

<div class="col-md-7 slide-content">

You yourself provide half of the solution, using table-layout: fixed works perfectly. 您可以使用table-layout: fixed自己提供解决方案的一半table-layout: fixed完美地工作。 But the content was looking horrible! 但是内容看起来太恐怖了! So I resolved that issue. 所以我解决了这个问题。 Just add this css - 只需添加此CSS-

.slide-content > table.table {
  table-layout: fixed;
}

I am looking for example a simple CSS hack on the scrollable class (table container) or something like that 我正在寻找例如可滚动类(表容器)上的简单CSS hack之类的东西

Have you tried position: fixed or position: absolute on the grey box .col-sm-3 ? 您是否尝试过将position: fixedposition: absolute在灰色框.col-sm-3上的position: absolute That should take .col-sm-3 out of the flow of the rest of the content (by default everything is position: static ). 那应该将.col-sm-3从其余内容的流中删除(默认情况下,所有内容都是position: static )。

In doing so, col-sm-3 will appear where you expect it to be no matter what the other "static" elements are doing, as long as you have no conflicting styles that is. 这样,只要您没有冲突的样式, col-sm-3就会出现在您期望它出现的位置,而不管其他“静态”元素在做什么。

UPDATE : https://jsfiddle.net/zer00ne/azkn7436/ 更新https : //jsfiddle.net/zer00ne/azkn7436/

HTML 的HTML

    <div class="container">
    <div class="row">
        <div class="col-md-4" style="background-color: lightgray; height:400px;">Aside Menu</div>
        <div class="col-md-7 slide-content">
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
            <table class="table">
                <tbody>
                    <tr>
                        <td>
                            <div class="row">
                                <div class="col-sm-9">
                                    <div class="row">
                                        <div class="col-sm-2" style="height: 250px; background-color: lightgreen">Some Label</div>
                                        <div class="col-sm-9 scrollable">
                                            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Exercitationem libero sequi tempore necessitatibus tempora, porro, iure, nisi dicta, rerum enim natus facilis voluptatum soluta dolore molestias vero odit eaque veniam.</p>
                                            <table class="table" border="1" cellpadding="1" cellspacing="1">
                                                <tbody>
                                                    <tr>
                                                        <td>Hello</td>
                                                        <td>Lipsumdolor</td>
                                                        <td>Lipsum Gen</td>
                                                        <td>Lorem ipsum dolor sit amet</td>
                                                        <td>Lorem ipsum dolor</td>
                                                        <td>Lorem ipsum</td>
                                                        <td>Lorem ipsum</td>
                                                        <td>Lorem ipsum</td>
                                                        <td>Lorem ipsum</td>
                                                    </tr>
                                                    <tr>
                                                        <td>Hello</td>
                                                        <td>Lipsumdolor</td>
                                                        <td>LipsumLipsumLipsumLipsum</td>
                                                        <td>Lorem ipsum dolor sit amet</td>
                                                        <td>FSH 10%</td>
                                                        <td>Lorem ipsum</td>
                                                        <td>Lorem ipsum</td>
                                                        <td>Lorem ipsum</td>
                                                        <td>Lorem ipsum</td>
                                                    </tr>
                                                    <tr>
                                                        <td>Hello</td>
                                                        <td>Lipsumdolor</td>
                                                        <td>Lipsum Gen</td>
                                                        <td>Lorem ipsum dolor sit amet</td>
                                                        <td>Lorem ipsum dolor</td>
                                                        <td>qwertyuiop</td>
                                                        <td>qwertyuiop</td>
                                                        <td>qwertyuiop</td>
                                                        <td>qwertyuiop</td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-3" style="position: fixed; right:0; bottom: 50%">
                                    <img src="https://placehold.it/120x120">
                                    <br> <a href="#">Click to enlarge</a>

                                </div>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

css 的CSS

.scrollable {
    overflow-x: scroll;
    max-width: 75%;
}
.scrollable table {
    margin: 0px;
    border-color: lightgray;
    word-wrap: break-word;
}
.slide-content {
    border: 1px solid lightgray;
}
/*  st    .slide-content > table.table {
  table-layout: fixed;
} */

Your .col-sm-3 is position: fixed; bottom: 50%; right: 0; 您的.col-sm-3 position: fixed; bottom: 50%; right: 0; position: fixed; bottom: 50%; right: 0; Now you can stretch your table as wide as you want, there ain't no way the little grey box on the right will move. 现在,您可以将表格拉伸到所需的最大宽度,右侧的灰色小框也将永远不会移动。

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

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