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

首选纯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/

10-09 00:01