【问题标题】:Table row not expanding to full width表格行未扩展到全宽
【发布时间】:2014-03-10 01:40:36
【问题描述】:

我有一张表格,当我将表格设置为 100% 的宽度并将表格行设置为 100% 的宽度时,没有任何反应或宽度发生变化。

.Table-Normal {
    position: relative;
    display: block;
    margin: 10px auto;
    padding: 0;
    width: 100%;
    height: auto;
    border-collapse: collapse;
    text-align: center;
}

.Table-Normal thead tr {
    background-color: #E74C3C;
    font-weight: bold;
}

.Table-Normal tr {
    margin: 0;
    padding: 0;
    border: 0;
    border: 1px solid #999;
    width: 100%;
}

.Table-Normal tr td {
    margin: 0;
    padding: 4px 8px;
    border: 0;
    border: 1px solid #999;
}

.Table-Normal tbody tr:nth-child(2) {
    background-color: #EEE;
}
<table id="top-leader" class="Table-Normal">
    <thead>
        <tr>
            <td>Position</td>
            <td>Name</td>
            <td>Kills</td>
            <td>Deaths</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>John Doe</td>
            <td>16 Kills</td>
            <td>0 Deaths</td>
        </tr>

        <tr>
            <td>2</td>
            <td>John Smith</td>
            <td>13 Kils</td>
            <td>1 Death</td>
        </tr>

        <tr>
            <td>3</td>
            <td>Bob Smith</td>
            <td>11 Kills</td>
            <td>0 Deaths</td>
        </tr>
    </tbody>
</table>

【问题讨论】:

    标签: html css html-table


    【解决方案1】:

    删除.Table-Normal中的display: block

    Fiddle

    .Table-Normal {
        position: relative;
        //display: block;
        margin: 10px auto;
        padding: 0;
        width: 100%;
        height: auto;
        border-collapse: collapse;
        text-align: center;
    }
    

    【讨论】:

    • 移除显示块属性导致表格不再滚动 x 轴
    • 或使用“display: table;”
    【解决方案2】:

    通过指定display: block,您覆盖了浏览器样式表中table 元素的默认值:display: table。删除 display: block 或将其替换为 display: table 可以解决此问题。

    【讨论】:

    • 解决了我的问题。谢谢。
    【解决方案3】:

    正如人们所提到的,您必须删除 display:block; 才能使其正常工作。如果您需要保持滚动功能,请将表格包装在 div 中并在其上设置溢出规则

    <div class = "table-container">
       <table>...</table>
    </div>
    
    .table-container{
       height:100px
       width:100px
       overflow-x:scroll;
       
       table{
          display: table;
          width:100%;
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 2021-06-23
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多