智能 HTML 表格列宽

发布于 2024-12-29 10:24:38 字数 3110 浏览 0 评论 0原文

这就是我的表格当前的样子:

在此处输入图像描述

我想缩小两个按钮之间的间隙并移动按钮更倾向于文本内容。但是,如果我输入一些很长的文本,或者在大/小屏幕上查看它,我仍然希望表格进行调整。

jsFiddle 链接

这是我当前的 HTML:

<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
<thead>
    <tr>
        <th scope="col">Manufacturer</th>
        <th scope="col">&nbsp;</th>
        <th scope="col">&nbsp;</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td class="fullWidth">Arrow International</td>
        <td class="fixedWidth"><input type="button" value="Report" class="button" /></td>
        <td class="fixedWidth"><input type="button" value="Delete"  class="button" /></td>
    </tr>
    <tr>
        <td class="fullWidth">Cardinal Health</td>
        <td class="fixedWidth"><input type="button" value="Report" class="button" /></td>
        <td class="fixedWidth"><input type="button" value="Delete" class="button" /></td>
    </tr>
    <tr>
        <td class="fullWidth">DeRoyal</td>
        <td class="fixedWidth"><input type="button" value="Report" class="button" /></td>
        <td class="fixedWidth"><input type="button" value="Delete" class="button" /></td>
    </tr>
    <tr>
        <td class="fullWidth">Kimberly-Clark</td>
        <td class="fixedWidth"><input type="button" value="Report" class="button" /></td>
        <td class="fixedWidth"><input type="button" value="Delete" class="button" /></td>
    </tr>
</tbody>

这是我当前的 CSS:

#main-content table {
            width: 100%;
            border-collapse: collapse;
            }
            
#main-content table thead th {
            font-weight: bold;
            font-size: 15px;
            border-bottom: 1px solid #ddd;
            }
            
#main-content tbody {
            border-bottom: 1px solid #ddd;
            }
            
#main-content tbody tr {
            background: #fff;
            }
          
#main-content tbody tr.alt-row {
            background: #f3f3f3;
            }
            
#main-content table td,
#main-content table th {
            padding: 10px;
            line-height: 1.3em;
            }        
            
#main-content table tfoot td .bulk-actions {
            padding: 15px 0 5px 0;
            } 
            
#main-content table tfoot td .bulk-actions select {
            padding: 4px;
            border: 1px solid #ccc;
            }
            
#main-content td .fixedWidth {
            white-space: nowrap;
            width: 0%;
            }
            
#main-content td .fullWidth {
            width: 100%;
            white-space: normal;
            }

我一直在尝试使用空白: nowrap 但还没有找到解决方案!

我很接近吗?

This is what my table currently looks like:

enter image description here

I want to reduce the gap between the two buttons and move the buttons more towards the text content. However I still want the table to adjust if I enter in some really long text, or look at it on a big / small screen.

jsFiddle link

This is my current HTML:

<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
<thead>
    <tr>
        <th scope="col">Manufacturer</th>
        <th scope="col"> </th>
        <th scope="col"> </th>
    </tr>
</thead>
<tbody>
    <tr>
        <td class="fullWidth">Arrow International</td>
        <td class="fixedWidth"><input type="button" value="Report" class="button" /></td>
        <td class="fixedWidth"><input type="button" value="Delete"  class="button" /></td>
    </tr>
    <tr>
        <td class="fullWidth">Cardinal Health</td>
        <td class="fixedWidth"><input type="button" value="Report" class="button" /></td>
        <td class="fixedWidth"><input type="button" value="Delete" class="button" /></td>
    </tr>
    <tr>
        <td class="fullWidth">DeRoyal</td>
        <td class="fixedWidth"><input type="button" value="Report" class="button" /></td>
        <td class="fixedWidth"><input type="button" value="Delete" class="button" /></td>
    </tr>
    <tr>
        <td class="fullWidth">Kimberly-Clark</td>
        <td class="fixedWidth"><input type="button" value="Report" class="button" /></td>
        <td class="fixedWidth"><input type="button" value="Delete" class="button" /></td>
    </tr>
</tbody>

This is my current CSS:

#main-content table {
            width: 100%;
            border-collapse: collapse;
            }
            
#main-content table thead th {
            font-weight: bold;
            font-size: 15px;
            border-bottom: 1px solid #ddd;
            }
            
#main-content tbody {
            border-bottom: 1px solid #ddd;
            }
            
#main-content tbody tr {
            background: #fff;
            }
          
#main-content tbody tr.alt-row {
            background: #f3f3f3;
            }
            
#main-content table td,
#main-content table th {
            padding: 10px;
            line-height: 1.3em;
            }        
            
#main-content table tfoot td .bulk-actions {
            padding: 15px 0 5px 0;
            } 
            
#main-content table tfoot td .bulk-actions select {
            padding: 4px;
            border: 1px solid #ccc;
            }
            
#main-content td .fixedWidth {
            white-space: nowrap;
            width: 0%;
            }
            
#main-content td .fullWidth {
            width: 100%;
            white-space: normal;
            }

I have been experimenting with the white-space: nowrap but have not found the solution yet!

Am I close?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠意 2025-01-05 10:24:38

您的表格始终调整为 100%,请在 CSS 中进行更改:

#main-content table {
        /*width: 100%;*/
        border-collapse: collapse;
        }

这将使表格调整为内容宽度。

Your table always adjusts to 100%, change that in CSS:

#main-content table {
        /*width: 100%;*/
        border-collapse: collapse;
        }

This will have the table being adjusted to content widths.

酒与心事 2025-01-05 10:24:38

也许是这样的? http://jsfiddle.net/tEyQH/

#main-content table thead th:last-child {
    width: 100%;
}

Maybe something like this? http://jsfiddle.net/tEyQH/

#main-content table thead th:last-child {
    width: 100%;
}
爱你是孤单的心事 2025-01-05 10:24:38

我确信有人有一个适用于所有浏览器的 CSS 解决方案,但我发现解决这个问题的最快方法(也就是说,不要继续在上面浪费钱)是使用 元素及其下方的 '' 元素:

<table>
    <colgroup>
        <col id='foo'/>
        <col id='bar'/>
        <col id='baz'/>
    </colgroup>
...
</table>

然后在样式表中设置 col 元素的宽度样式。

I am sure someone has a CSS solution that works across all browsers somewhere, but I've found that the fastest way to solve this problem (that is, not keep wasting money on it), is to use a <colgroup/> element with its '' elements below it:

<table>
    <colgroup>
        <col id='foo'/>
        <col id='bar'/>
        <col id='baz'/>
    </colgroup>
...
</table>

Then style the width of the col element in your stylesheet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文