输入内部时奇怪的列宽行为

时间:2016-05-11 20:31:39

标签: html css

我正在努力解决问题:

HTML:

<table>
    <thead>
    <tr>
        <th>Col 1</th>
        <th>Col 2</th>
        <th>Col 3</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td><div>Data 1</div></td>
        <td ><div><input/></div></td>
        <td><div>Data 2</div></td>
    </tr>
    </tbody>
</table>

的CSS:

th {
    width: 50px;
}

input {
    width: 100%;
}

小提琴:

https://jsfiddle.net/Lcdvpk1y/1/

问题是此示例在不同浏览器中的显示方式不同。在Chrome上,列输入具有独立的计算宽度,但在IE和Edge宽度取自指定值。

我很想知道这种不同行为的原因,因为它是更大问题的简化示例,我不能按照自己的意愿自定义代码,必须知道应用最简单的解决方案。

2 个答案:

答案 0 :(得分:1)

你的文件中是否有reset.css链接?这可能会在不同浏览器的大小调整中引起奇怪的行为。

尝试在外部样式表中链接:

&#13;
&#13;
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您移除每个<div>内的<td>,就可以获得正确的外观。

要仅使用CSS进行修复(不更改标记),以下代码段应该可以正常工作。

td {
    width: 50px;
}

td div {
    width: inherit;
}

td div input {
    width: 100%;
}

https://jsfiddle.net/alexndreazevedo/d9fxu41z/

相关问题