html - 表格单元格在行内的高度不同

标签 html css

我正在用 html 创建一个表格。每个表格单元格的高度都必须完全相同。当我在表格单元格中放置一个 div 时,行中表格单元格之间的高度不同。包含文本的表格单元格高 61.6 像素,而包含 div 的表格单元格高 61.2 像素。我使用的是谷歌浏览器,这是我唯一需要表格单元格高度均匀的浏览器。如何确保所有表格单元格的高度相同?

首选纯 CSS 解决方案,但可以接受 javascript 解决方案。

table {
        border-collapse: collapse;
        width: 600px;
    }

    tbody {
        border: 1px solid black;
    }

    th {
        padding: 0 6px;
        text-align: center;
        background: black;
        color: white;
    }

    tr td {
        background: darkgray;
    }

    tr:nth-child(even) td {
        background: gray;
    }

    td.small {
        font-size: 9px;
    }

    td.large {
        font-size: 18px;
    }

    td {
        padding: 6px;
        border-color: black;
        border-width: 0 1px;
        border-style: solid;
        height: 18px;
    }

    .evilobject {
        height: 50px;
        background: red;
    }
<table>
    <tbody>
        <tr>
            <th>Large Font</th>
            <th>Small Font</th>
            <th>Contains Div</th>
        </tr>
        <tr>
            <td class="large">What the font!?</td>
            <td class="small">What the font!?</td>
            <td class="small"><div class="evilobject">Evil div object</div></td>
        </tr>
        <tr>
            <td class="large">What the font!?</td>
            <td class="small">What the font!?</td>
            <td class="small"><div class="evilobject">Evil div object</div></td>
        </tr>
        <tr>
            <td class="large">What the font!?</td>
            <td class="small">What the font!?</td>
            <td class="small"><div class="evilobject">Evil div object</div></td>
        </tr>
    </tbody>
</table>

最佳答案

使用 tr 代替 td 并使用通用选择器`

*{
   box-sizing: border-box;
   padding: 0;
   margin: 0;
}
table {
        border-collapse: collapse;
        width: 600px;
    }

    tbody {
        border: 1px solid black;
    }

    th {
        padding: 0 6px;
        text-align: center;
        background: black;
        color: white;
    }

    tr td {
        background: darkgray;
    }

    tr:nth-child(even) td {
        background: gray;
    }

    td.small {
        font-size: 9px;
    }

    td.large {
        font-size: 18px;
    }

     tr{
        padding: 6px;
        border-color: black;
        border-width: 0 1px;
        border-style: solid;
        height: 18px;
       
    }

    .evilobject {
        height: 50px;
        background: red;
    }
<table >
    <tbody>
        <tr>
            <th>Large Font</th>
            <th>Small Font</th>
            <th>Contains Div</th>
        </tr>
        <tr>
            <td class="large">What the font!?</td>
            <td class="small">What the font!?</td>
            <td class="small"><div class="evilobject">Evil div object</div></td>
        </tr>
        <tr>
            <td class="large">What the font!?</td>
            <td class="small">What the font!?</td>
            <td class="small"><div class="evilobject">Evil div object</div></td>
        </tr>
        <tr>
            <td class="large">What the font!?</td>
            <td class="small">What the font!?</td>
            <td class="small"><div class="evilobject">Evil div object</div></td>
        </tr>
    </tbody>
</table>

`

关于html - 表格单元格在行内的高度不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54136950/

相关文章:

html - Chrome Dev Tools 添加了 <br> 标签,而这些标签不存在?

javascript - HTML 表单提交按钮未运行与其关联的 OnSubmit 脚本

javascript - 如何使用 Twitter Bootstrap 设置下拉子菜单

javascript - 在 <br> 之后缩进文本

html - 如何让下拉菜单显示在谷歌地图上?

jquery - 显示多个隐藏的 div

python - 如何在 python 中使用正则表达式将 HTML 子字符串附加到匹配的字符串中?

javascript - Chart.js 将折线图向左对齐

javascript - 使用 Javascript 对象动态生成的 html 不采用 Bootstrap 或 CSS 样式

html - 如何在行之间和 "paragraphs"之间获得不同的间距?