Html 带有 3 列的表。固定中心列宽度。如何在其他两列上共享宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7882979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:14:16  来源:igfitidea点击:

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

htmlcsshtml-table

提问by Valamas

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?

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

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

Currently, depending on the content of left or right columns, they are always uneven. I have tried setting 50% width on the left and right as well as other numbers and min-width trials.

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

Please no jQuery/Javascript.

请不要使用 jQuery/Javascript。

回答by misterManSam

New answer to this old question.

这个老问题的新答案。

  1. Give the middle column tha max-widthand min-widthinstead of width
  2. Give the left and right tha widthof 50%.
  3. Don't give the tablea widthat all.
  1. 给中间一列th一个max-widthandmin-width而不是width
  2. 给左,右th一个width50%
  3. 根本不要给tablea width

I have fixed this examples middle column widthat 300px.

我已将此示例中间列固定width300px.

jsBin Example!

jsBin 示例!

CSS

CSS

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

HTML

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>

回答by sergio

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

使用“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;
}

回答by iehrlich

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

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