簡體   English   中英

在HTML中,如何使控件占據其容器的所有水平空間而不重疊邊框?

[英]In HTML, how can I make a control take up all the horizontal space of its container without overlapping the border?

考慮以下HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<table cellspacing = "10" cellpadding="0" border="1" width="100%">
<tr>
    <td>
        <input style="border-width:1px; border-style:solid; width:100%; background-color:aqua">
    </td>
</tr>
</table>
</html>

它輸出以下內容,如您所見,右側有一點重疊。

在IE和Chrome中輸出

如何更改此代碼,以使輸入框占據TD的整個內部空間而不會重疊?

請注意,我正在尋找以下解決方案:

  • DOCTYPE標簽和html標簽如上所述。
  • 如上所述,容器元素的大小不是固定的。

謝謝。

如JamWaffles所述,在輸入標簽的style元素上添加box-sizing:border-box解決了該問題。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<table cellspacing = "10" cellpadding="0" border="1" width="100%">
<tr>
    <td>
        <input style="box-sizing: border-box; border-width:1px; border-style:solid; width:100%; background-color:aqua">
    </td>
</tr>
</table>
</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM