html - 当我将子元素设置为 "width:80%"时,为什么 <td> 会缩小?

标签 html css html-table

我有一个表格,当我将嵌套元素的样式设置为 width:80% 时,左列之一会缩小。该列缩小并截断输入元素的右边缘。您可以通过从输入元素中删除样式来了解我在说什么。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
    <table width="100%" border="1">
        <tr>
            <td>
                <input type="text" style="width: 80%;" />
            </td>
            <td>
                Lots of text to make the right cell expand.
                Lots of text to make the right cell expand.
                Lots of text to make the right cell expand.
                Lots of text to make the right cell expand.
                Lots of text to make the right cell expand.
                Lots of text to make the right cell expand.
                Lots of text to make the right cell expand.
                Lots of text to make the right cell expand.
                Lots of text to make the right cell expand.
            </td>
        </tr>
    </table>
</body>
</html>

我有一种感觉,这是因为列的大小是根据子项确定的,但是通过根据它的父项确定元素的大小,我创建了一个“第 22 条军规”。

有人可以告诉我发生了什么,以及我如何保持相同的列宽,并将我的元素调整到其宽度的 80%?

最佳答案

您遇到的问题是因为您正在设置 <input>的宽度是没有宽度的单元格的百分比。因此单元格变小以弥补另一个单元格上的文本,一旦完成,输入将获得剩余内容的 80%。

你的选择:
1) 为 <input> 设置宽度(以像素为单位)领域。
2) 为 <td> 设置宽度(以像素/百分比为单位) .

处理您的评论:当您删除样式时,它变大的原因是它会恢复到浏览器的默认大小 <input>元素,大于单元格中剩余内容的 80%。

关于html - 当我将子元素设置为 "width:80%"时,为什么 <td> 会缩小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/463018/

相关文章:

html - 使用 gtm 中的数据属性进行基于事件的跟踪

jquery - 悬停 div 时显示 1px 间隙(在 IE8 中)

javascript - 将整个表格行变成链接的最标准和兼容的方法是什么?

javascript - 单击按钮后向文本字段添加内容

javascript - jQuery 选项卡,多个选项卡只有一个面板

asp.net - Response.Write 在标记的特定部分

c++ - Qt 获取标签以保持底部的一定百分比

javascript - 使用封闭标记对 html 表行进行分组

javascript - 在 HTML 中以两列显示提要

javascript - 为什么 Vue.js 键修饰符只适用于 `<button>` 而不是 `<div>` ?