html - 100% 宽度的 HTML 元素问题

标签 html css dom

当为某些 HTML 元素(如表单输入)分配 100% 宽度时 - 您不能再应用任何可能影响宽度的其他样式。 borderpadding 之类的东西会导致元素超过 100%。这会导致笨拙的元素可能在其父元素之外。

因为 CSS 不支持 width: 100% - 2px; 我知道的唯一方法是使用绝对像素宽度 (width: 98px) 或将元素砍掉 100%,这不是一个真正的选择。

<div style="overflow:hidden;">
<input style="width:100%; border: 1px solid #000;" />
</div>

他们还有其他解决方法吗?

最佳答案

连同 adding another div ,解决方案很快就会使用 CSS 3 添加 box-sizing attribute CSS规则。这个新CSS 3 value 已经在 IE 8 和所有其他浏览器中工作 - 所以如果您不介意跳过 IE 6 和 7,您现在可以使用它!

textarea {
        -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
        -moz-box-sizing: border-box;    /* Firefox, other Gecko */
        box-sizing: border-box;         /* Opera/IE 8+ */
}

关于html - 100% 宽度的 HTML 元素问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4653694/

相关文章:

jquery - 遍历嵌套列表 - jQuery

javascript - 通过javascript自动向页面上的每个DIV添加类(连续编号)?

jquery - 什么是 jquery 堆栈?

html - 为错误的 POST 数据选择正确的 HTTP 响应代码

html - 'state' 验证没有错误消息和不正确的图标颜色

javascript - Bootstrap——连接到主菜单的响应式副菜单的问题

html - 奇怪的 CSS 问题,内容单元在 IE 中移动到页面底部 - 在 Chrome 和 Firefox 中工作正常

javascript - Node.js Express : re-accessing variables injected into . html 文件

HTML5 : What should server do when user uploads folder?

html - 使用 CSS 更改元素布局而不更改 HTML