Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a table in a collapsible Bootstrap panel change width?

I have put together a Bootply demonstration here: http://www.bootply.com/Ss2aAnzqlZ.

It's a panel with a table as per http://getbootstrap.com/components/#panels-tables. However, I've also made the panel collapsible. The collapsing bit works OK, but the table itself doesn't retain shape. As you'll see in the Bootply, it doesn't fill the width of the panel when you first load the page. When you click "Improvements" in the panel header to collapse the panel, the table takes up the full panel width during the animation, then disappears. When you click again to show the panel content, the table is the full width until the animation stops, at which point, it shrinks back to what looks like an "auto" width.

Oddly enough, inspecting the table element shows that the table itself is full width, but the thead, tbody and tfoot aren't.

I've sort of tracked it down to the presence of the "collapse" class in the table. If you start the Bootply without the "collapse" class, it's full width until you collapse the panel. When you expand it, it goes back to auto width. I don't know why ... do you?

Here's the snippet, but the collapsing doesn't appear to run here. The Bootply is better.

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div style="margin:15px">
    <div class="panel panel-default">
        <div class="panel-heading">
            <a href="#" data-toggle="collapse" data-target="#improvementsPanel" aria-expanded="true" class="">Improvements</a>
        </div>
        <table id="improvementsPanel" class="table panel-collapse collapse in" aria-expanded="true" style="">
            <thead>
                <tr>
                    <th>Description</th>
                    <th class="text-right">Qty</th>
                    <th>UOM</th>
                    <th class="text-right">Rate</th>
                    <th class="text-right">Value</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Item 1</td>
                    <td class="text-right">133.00</td>
                    <td>m²</td>
                    <td class="text-right">425.00</td>
                    <td class="text-right">56,525</td>
                </tr>
                <tr>
                    <td>Item 2</td>
                    <td class="text-right">85.00</td>
                    <td>m²</td>
                    <td class="text-right">70.00</td>
                    <td class="text-right">5,950</td>
                </tr>
                <tr>
                    <td>Item 3</td>
                    <td class="text-right">25.00</td>
                    <td>m²</td>
                    <td class="text-right">100.00</td>
                    <td class="text-right">2,500</td>
                </tr>
                <tr>
                    <td>Item 4</td>
                    <td class="text-right"></td>
                    <td></td>
                    <td class="text-right"></td>
                    <td class="text-right">1,500</td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <th class="text-right" colspan="4">Total</th>
                    <th class="text-right">66,475</th>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
like image 941
Phil Cairns Avatar asked Nov 14 '14 05:11

Phil Cairns


People also ask

How do I change the width of a column in Bootstrap?

Bootstrap table column width You can use one of the following classes to manipulate the width of the columns. Table columns with auto width. Just add the .w-auto class to the table element to set an auto width to the table column. The width of the columns will automatically adjust to the content of the column.

How to create a collapsible panel in Bootstrap?

To create a collapsible panel in Bootstrap, use the panel-collapse class. You can try to run the following code to create a collapsible panel

What is the latest version of material design for Bootstrap?

We recommend migrating to the latest version of our product - Material Design for Bootstrap 5. Responsive tables allow tables to be scrolled horizontally with ease. Make any table responsive across all viewports by wrapping a .table with .table-responsive.

Why are my columns not tiering in Bootstrap grid 1?

Table not respecting Bootstrap Grid 1 content in span larger in than span itself, causing columns to not tier in a responsive layout Related 1 fixed table layout resize when column is hiding


2 Answers

The collapse class toggles the display style on the table between none and block and this appears to be interfering with the standard table CSS.

You can resolve this by putting your table inside a div and setting that div to collapse rather than the table.

New Bootply: http://www.bootply.com/L8h2OdpMuD

HTML:

<div style="margin: 15px">
    <div class="panel panel-default">
        <div class="panel-heading">
            <a href="#" data-toggle="collapse" data-target="#improvementsPanel" aria-expanded="true" class="">Improvements</a>
        </div>
        <div id="improvementsPanel" class="panel-collapse collapse in" aria-expanded="true">
            <table class="table">
                <thead>
                    <tr>
                        <th>Description</th>
                        <th class="text-right">Qty</th>
                        <th>UOM</th>
                        <th class="text-right">Rate</th>
                        <th class="text-right">Value</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Item 1</td>
                        <td class="text-right">133.00</td>
                        <td>m²</td>
                        <td class="text-right">425.00</td>
                        <td class="text-right">56,525</td>
                    </tr>
                    <tr>
                        <td>Item 2</td>
                        <td class="text-right">85.00</td>
                        <td>m²</td>
                        <td class="text-right">70.00</td>
                        <td class="text-right">5,950</td>
                    </tr>
                    <tr>
                        <td>Item 3</td>
                        <td class="text-right">25.00</td>
                        <td>m²</td>
                        <td class="text-right">100.00</td>
                        <td class="text-right">2,500</td>
                    </tr>
                    <tr>
                        <td>Item 4</td>
                        <td class="text-right"></td>
                        <td></td>
                        <td class="text-right"></td>
                        <td class="text-right">1,500</td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <th class="text-right" colspan="4">Total</th>
                        <th class="text-right">66,475</th>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
like image 83
ChaoticNadirs Avatar answered Oct 21 '22 21:10

ChaoticNadirs


Alternatively, you can add the css:

table.collapse.in {
   display: table;
}

Arguably this could be a patch to component-animations.less.

like image 9
Brian M. Hunt Avatar answered Oct 21 '22 22:10

Brian M. Hunt