Html <th> 的列宽不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25850787/
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 02:42:22  来源:igfitidea点击:

Column width for <th> not working

htmlcsstwitter-bootstrap

提问by Error

Here is my html:

这是我的 html:

     <html>
<head>
<meta charset="utf-8">    

<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/bootstrap-theme.css" rel="stylesheet">

</head>
<body style="padding-top: 47px;">

<table class="table table-hover table-striped-custom">
    <thead>
        <tr>
            <th style="width: 5px;"></th>
            <th>Column2</th>
            <th>column2</td>
        </tr>
    </thead>
    <tbody>
            <tr>
                <td style="background-color: #FF5F5F;width: 5px;"></td>
                <td>blaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</td>
                <td>bla</td>
            </tr>
            <tr>
                <td style="background-color: deepskyblue"></td>
                <td>bla</td>
                <td>bla</td>
            </tr>

    </tbody>
</table>

Problem is when setting width for first column: it won't go under 16px. If I put bigger width(20,50), colored column become larger, but when I try to set them thinner (for example 7px), It doesn't take any effect. How can I do it, without removing any css class from existing template. Colored column have to be first, it's height must be as row's height and I have to be able to set it's width to any value I want.

问题是在为第一列设置宽度时:它不会低于 16px。如果我放更大的宽度(20,50),彩色列会变大,但是当我尝试将它们设置得更细(例如 7px)时,它没有任何效果。在不从现有模板中删除任何 css 类的情况下,我该怎么做。彩色列必须是第一个,它的高度必须是行的高度,我必须能够将它的宽度设置为我想要的任何值。

采纳答案by Pete

It's because your bootstrap css is adding 8px padding to the table cells (which is why your min width is 16px: 8px left plus 8px right) - try adding padding:0;for those cells and it should work

这是因为您的引导 css 向表格单元格添加了 8px 填充(这就是为什么您的最小宽度为 16px:8px 左加 8px 右) - 尝试添加padding:0;这些单元格,它应该可以工作

Example

例子

回答by Paul Roub

It's possible that bootstrap has set min-widthon thor td. If so, I'd add an additional style block (in the HTML, if you can't add/edit external CSS), to allow the first column to be as small as you like:

引导程序可能已设置min-widththtd。如果是这样,我会添加一个额外的样式块(在 HTML 中,如果您无法添加/编辑外部 CSS),以允许第一列尽可能小:

<style type="text/css">
  .table td:first-child, .table th:first-child {
    min-width: 0;
    padding: 0;
  }
</style>