Table with 3 columns. Fixed center column width. How to have shared width on other two columns?

后端 未结 3 1758
南旧
南旧 2021-01-12 03:22

i have a 100% width table with 3 columns. The center column must be 600px width. How can I have the other two equal width while using up the remaining space?

相关标签:
3条回答
  • 2021-01-12 03:47

    The use of "colgroup" tag could be a great help for this.

    <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;
    }
    
    0 讨论(0)
  • 2021-01-12 03:50

    I guess "align=center" used with center column may help you.

    0 讨论(0)
  • 2021-01-12 04:03

    New answer to this old question.

    1. Give the middle column th a max-width and min-width instead of width
    2. Give the left and right th a width of 50%.
    3. Don't give the table a width at all.

    I have fixed this examples middle column width at 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>
    
    0 讨论(0)
提交回复
热议问题