【问题标题】:Table with 3 columns. Fixed center column width. How to have shared width on other two columns?具有 3 列的表。固定中心列宽度。如何在其他两列上共享宽度?
【发布时间】:2011-12-14 13:00:01
【问题描述】:

我有一个 100% 宽度的 3 列表格。中心列的宽度必须为 600 像素。如何在用完剩余空间的同时让其他两个宽度相等?

<table style="width: 100%">
    <tr>
        <td>left</td>
        <td style="width: 600px">center</td>
        <td>right</td>
    </tr>
</table>

目前,根据左列或右列的内容,它们总是不均匀的。我已经尝试在左右设置 50% 的宽度以及其他数字和最小宽度试验。

请不要使用 jQuery/Javascript。

【问题讨论】:

  • 用 colgroup 标签检查下面提到的解决方案

标签: html css html-table


【解决方案1】:

我想与中心列一起使用的“align=center”可能会对您有所帮助。

【讨论】:

    【解决方案2】:

    使用“colgroup”标签可能对此有很大帮助。

    <table class="fixed-center-table" border="1">
            <thead>
                <colgroup>
                    <col>
                    <col id="middle-column">
                    <col>
                </colgroup>
                <tr>
                    <th>First Column</th>
                    <th></th>
                    <th>Third Column</th>
                </tr>
            </thead>
    
            <tbody>
                <tr>
                    <td>AAAAAA</td>
                    <td>BBBB</td>
                    <td>CCCCC</td>
                </tr>
                <tr>
                    <td>AAAAAA</td>
                    <td>BBBB</td>
                    <td>CCCCC</td>
                </tr>
                <tr>
                    <td>AAAAAA</td>
                    <td>BBBB</td>
                    <td>CCCCC</td>
                </tr>
            </tbody>
    </table>
    
    .fixed-center-table {
        table-layout: fixed;
        width: 100%;
        border-collapse: collapse;
    }
    
    .fixed-center-table  td{
       text-align: center;
    }
    
    col#middle-column {
       width: 600px;
    }
    

    【讨论】:

    • 这是更好的解决方案,应该用于管理表格的宽度。
    【解决方案3】:

    这个老问题的新答案。

    1. 给中间列th 一个max-widthmin-width 而不是width
    2. 给左右th一个width50%
    3. 根本不要给table width

    我已将这个示例中间列 width 修复为 300px

    jsBin Example!

    CSS

    table {
        border-collapse: collapse;
    }
    th, td {
        border: solid 1px #CCC;
        padding: 10px;
    }
    .fixed {
        max-width: 300px;
        min-width: 300px;
    }
    .fluid {
            width: 50%;
    }
    

    HTML

    <table>
        <thead>
            <tr>
                <th class="fluid">First Column</th>
                <th class="fixed">Fixed Column</th>
                <th class="fluid">Third Column</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
    

    【讨论】:

      猜你喜欢
      • 2020-11-30
      • 2010-09-13
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多