html - 按钮是一个内联元素,但宽度有效

标签 html css inline

按钮是一个内联元素,但它的宽度属性有效。这是为什么?

button {
    width: 300px;
}
<button>asdfsdf</button>
<button>234234d</button>

最佳答案

根据(非规范的)Appendix D. Default style sheet for HTML 4,大多数浏览器默认将 button 元素显示为 inline-block

因此,您可以期望 width 属性起作用,如 Calculating widths and margins - Inline-block, non-replaced 中所述。

但不仅如此。 button 元素是 replaced elements :

In CSS, a replaced element is an element whose representation is outside the scope of CSS. These are kind of external objects whose representation is independent of the CSS.

因此,它们有一些特殊的行为。例如,无论它们是否具有 display: inline-blockdisplay: inline,它们的大小都根据 width 属性确定,根据 Calculating widths and margins - Inline, replaced .

值得注意的是,HTML5 强制将它们显示为 inline-block。这在 10.5.2 Bindings - The button element 中有解释:

@namespace url(http://www.w3.org/1999/xhtml);
button { binding: button; }

When the button binding applies to a button element, the element is expected to render as an 'inline-block' box rendered as a button whose contents are the contents of the element.

关于html - 按钮是一个内联元素,但宽度有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27764122/

相关文章:

html - 为 html 电子邮件添加额外的 p 和 span 标签

html - 在 jquery mobile 中配置带有数字的自定义密码键盘

html - 创建导航栏 我在 float 时遇到问题 我的链接位于左侧,但我想要右侧

html - 如何创建具有 100% 宽度图像的基于网格的网站?

javascript - 为什么这个导航变得不可点击?

html - 缩进 HTML 和内嵌 HTML 之间的区别

php - 保存客户编辑的表格时出错

html - 使用 jQuery 删除 html 表中的空列

css - 滑动可扩展内嵌菜单

javascript - 额外的 ajax 请求与内联 json/javascript