Bootstrap 3 table - table-responsive not working

2020-02-26 04:58发布

I'm using bootstrap 3 for a website project. I'm trying to create a page with a responsive table, so that I'd have the scrollbar when the table is too big. I made a test case like so:

<div class="row">
  <h4>Nuværende kurser</h4>
  <div class="col-12 col-sm-12 col-lg-12">
    <div class="table-responsive">
      <table class="table">
        <thead>
          <tr>
            <th>#</th>
            <th>Table heading</th>
            <th>Table heading</th>
            <th>Table heading</th>
            <th>Table heading</th>
            <th>Table heading</th>
            <th>Table heading</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>1</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
          </tr>
          <tr>
            <td>2</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
          </tr>
          <tr>
            <td>3</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
            <td>Table cell</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div><!-- end col-12 -->
</div><!-- end row -->

Now, the problem is that it doesn't add the scrollbar, it merely expands the website to the width of the table.

See a screenshot here:

enter image description here

I've seen it working on several other websites, so something I'm doing...is wrong.

10条回答
疯言疯语
2楼-- · 2020-02-26 05:03

For me it was the 'min-width' value in body element breaking the responsiveness of this class.

查看更多
可以哭但决不认输i
3楼-- · 2020-02-26 05:03

In my code, I did add a CSS class:

.modal-body{
  max-width:95vw;
}

The layout did work for sm

查看更多
放我归山
4楼-- · 2020-02-26 05:09

Try this
remove <div class="table-responsive">...</div>
and move class table-responsive in
<div class="col-12 col-sm-12 col-lg-12">

example

<div class="col-12 col-sm-12 col-lg-12 table-responsive">

this 100% work but if dont work move class table-responsive to Earlier Div layer, In this case,
<div class="col-12 col-sm-12 col-lg-12">
must be removed.

查看更多
Juvenile、少年°
5楼-- · 2020-02-26 05:12

I'v done it.

<div style="display: table">
    <div style="display: table-row">
        <div style="display: table-cell">


            <div class="container-fluid">
                <div class="row">
                    <div class="col-xs-12">


                       //wrapper that will give opportunity to use: overflow-x: auto; 
                       <table style="table-layout: fixed; width: 100%;">
                           <tr>
                               <td>


                                  //SCROLL
                                  <div style="width: 100%; overflow-x: auto;">

                                      //table, that shoud have "table-responsive" bootstrap class effect
                                      <table class="table table-hover">

                                          //...table content...

                                      </table>

                                  </div>


                               <td>
                           </tr>

                       </table>                           


                    </div>
                </div>

        </div>
    </div>
</div>

You will receive at xs-screen (example from my app):

enter image description here

查看更多
手持菜刀,她持情操
6楼-- · 2020-02-26 05:13

I had this problem and found that it worked to either give the table a set width in px, or to set the table to display: block.

查看更多
趁早两清
7楼-- · 2020-02-26 05:17

Replace your table-responsive with this

<div class='table-responsive'> -> <div style="overflow-x:auto;">
查看更多
登录 后发表回答