简体   繁体   English

div 容器内的 HTML 表 scollbar 问题

[英]HTML table scollbar issue inside the div container

I am using the bootstrap for most CSS.我正在为大多数 CSS 使用引导程序。 I have divided a section into two columns, In left column showing a table with some data & on right side a Google map.The problem is that the table not showing scrollbar naturally when the table row grows.我将一个部分分为两列,在左列显示一个包含一些数据的表格,在右侧显示一个 Google map。问题是当表格行增长时表格没有自然显示滚动条。 I have tried to control the scrollbar using the CSS but it is not working properly as you can see in the image.我尝试使用 CSS 来控制滚动条,但正如您在图像中看到的那样,它无法正常工作。 在此处输入图像描述

There are two problems with scrollbar, first is far from the actual table & as per my guess it is because I am setting scrollbar on the parent div container.滚动条有两个问题,第一个与实际表格相去甚远,根据我的猜测,这是因为我在父div容器上设置滚动条。 The second issue is that when I try to scroll it using the mouse wheel then the table starts flickering.I can't get my head around to solve this problem.第二个问题是,当我尝试使用鼠标滚轮滚动它时,表格开始闪烁。我无法解决这个问题。 Here is my code.这是我的代码。

HTML Part HTML 零件

I have removed the irrelevant parts我已经删除了不相关的部分

<main class="flex-shrink-0">
        <div class="container" style="max-width: 100% !important;">

            <section>

                <div class="row">
                    <div class="col">
                        
                        <div class="container" style="overflow-y:auto;overflow-x:auto;;height:400px;width: 100%;">
                            <table class="" width="100%" style="overflow:auto; height: 400px;width: 100%;">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Code</th>
                                        <th>Size</th>
                                        <th>Illumination Type</th>
                                        <th>Location</th>
                                        <th>Availability</th>
                                        <th>Media</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for hoarding in road_hoardings %}
                                    <tr>
                                        <td>{{forloop.counter}}</td>
                                        <td>{{hoarding.site_code}}</td>
                                        <td>{{hoarding.size}}</td>
                                        <td>{{hoarding.illumination_type}}</td>
                                        <td>{{hoarding.location}}</td>
                                        <td>{{hoarding.available_by_date}}</td>
                                        <td>
                                            <button class="btn btn-primary">
                                                <i class="far fa-images"></i>
                                            </button>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="col">
                        <div id="map" style="width: 100%; height: 100%;"></div>
                    </div>
                </div>

            </section>

        </div>
    </main>

Table Related CSS表相关 CSS

body {
            /* overflow-x: hidden; */
            margin: 0;
            background: linear-gradient(45deg, #49a09d, #5f2c82);
            font-family: sans-serif;
            font-weight: 100;
        }


        table {
            width: 800px;
            /* height: 400px; */
            /* border-collapse: collapse; */
            /* overflow: hidden;
            overflow-y: auto; */
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
        }

        th,
        td {
            padding: 15px;
            background-color: rgba(255, 255, 255, 0.2);
            color: #fff;
        }

        th {
            text-align: left;
        }

        thead th {
            background-color: #55608f;
        }

        tbody tr:hover {
            background-color: rgba(255, 255, 255, 0.3);
        }

        tbody td {
            position: relative;
        }

        tbody td:hover:before {
            content: "";
            position: absolute;
            left: 0;
            right: 0;
            top: -9999px;
            bottom: -9999px;
            background-color: rgba(255, 255, 255, 0.2);
            z-index: -1;
        }

You have added height and overflow in the container as well as table:您在容器和表格中添加了高度和溢出:

<div class="container" style="overflow-y:auto;overflow-x:auto;;height:400px;width: 100%;">
    <table class="" width="100%" style="overflow:auto; height: 400px;width: 100%;">

You don't need to add overflow and height property in the table.您不需要在表格中添加溢出和高度属性。 Just keep it like this:保持这样的状态:

<table class="" width="100%" style="width: 100%;">

I have improved your code in below fiddle.我在下面的小提琴中改进了你的代码。 Open it in full screen to see the best result.全屏打开它以查看最佳效果。

 body { /* overflow-x: hidden; */ margin: 0; background: linear-gradient(45deg, #49a09d, #5f2c82); font-family: sans-serif; font-weight: 100; } table { width: 800px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.1); position: relative; overflow: hidden; } th, td { padding: 15px; background-color: rgba(255, 255, 255, 0.2); color: #fff; } th { text-align: left; } thead th { background-color: #55608f; } tbody tr:hover { background-color: rgba(255, 255, 255, 0.3); } tbody td { position: relative; } tbody td:hover:before { content: ""; position: absolute; left: 0; right: 0; top: -9999px; bottom: -9999px; background-color: rgba(255, 255, 255, 0.2); z-index: -1; }
 <main class="flex-shrink-0"> <div class="container" style="max-width: 100%;important:"> <section> <div class="row"> <div class="col"> <div class="container" style="overflow-y;auto:overflow-x;auto;:height;400px:width; 100%:"> <table class="" width="100%" style="width; 100%:"> <thead> <tr> <th>#</th> <th>Code</th> <th>Size</th> <th>Illumination Type</th> <th>Location</th> <th>Availability</th> <th>Media</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> <tr> <td>1</td> <td>123</td> <td>20</td> <td>Type 1</td> <td>Location 1</td> <td>Date</td> <td><button class="btn btn-primary">Click</button></td> </tr> </tbody> </table> </div> </div> <div class="col"> <div id="map" style="width; 100%: height; 100%;"></div> </div> </div> </section> </div> </main>

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

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