Flex/Grid 布局不适用于按钮或字段集元素

我试图中心的内部元素的 <button>标签与柔性盒的 justify-content: center。但是 Safari 并不是他们的中心。我可以将相同的样式应用于任何其他标记,并且它可以按预期的方式工作(参见 <p>-tag)。只有按钮是左对齐的。

试试 Firefox 或 Chrome,你会发现其中的差别。

是否有任何用户代理样式我必须覆盖? 或任何其他解决方案,这个问题?

div {
display: flex;
flex-direction: column;
width: 100%;
}
button, p {
display: flex;
flex-direction: row;
justify-content: center;
}
<div>
<button>
<span>Test</span>
<span>Test</span>
</button>
<p>
<span>Test</span>
<span>Test</span>
</p>
</div>

还有一个小提琴: Http://jsfiddle.net/z3sfwtn2/2/

64677 次浏览

The Problem

In some browsers the <button> element doesn't accept changes to its display value, beyond switching between block and inline-block. This means that a <button> element cannot be a flex or grid container, or a <table>, either.

In addition to <button> elements, you may find this constraint applying to <fieldset> and <legend> elements, as well.

See the bug reports below for more details.

Note: Although they cannot be flex containers, <button> elements can be flex items.


The Solution

There is a simple and easy cross-browser workaround to this problem:

Wrap the content of the button in a span, and make the span the flex container.

Adjusted HTML (wrapped button content in a span)

<div>
<button>
<span><!-- using a div also works but is not valid HTML -->
<span>Test</span>
<span>Test</span>
</span>
</button>
<p>
<span>Test</span>
<span>Test</span>
</p>
</div>

Adjusted CSS (targeted span)

button > span, p {
display: flex;
flex-direction: row;
justify-content: center;
}

Revised Demo


References / Bug Reports

Flexbox on a <button> blockifies the contents but doesn't establish a flex formatting context

User (Oriol Brufau): The children of the <button> are blockified, as dictates the flexbox spec. However, the <button> seems to establish a block formatting context instead of a flex one.

User (Daniel Holbert): That is effectively what the HTML spec requires. Several HTML container-elements are "special" and effectively ignore their CSS ABC0 value in Gecko [aside from whether it's inline-level vs. block-level]. ABC1 is one of these. ABC2 & <legend> are as well.

Add support for display:flex/grid and columnset layout inside <button> elements

User (Daniel Holbert):

<button> is not implementable (by browsers) in pure CSS, so they are a bit of a black box, from the perspective of CSS. This means that they don't necessarily react in the same way that e.g. a <div> would.

This isn't specific to flexbox -- e.g. we don't render scrollbars if you put overflow:scroll on a button, and we don't render it as a table if you put display:table on it.

Stepping back even further, this isn't specific to <button>. Consider <fieldset> and <table> which also have special rendering behavior.

And old-timey HTML elements like <button> and <table> and <fieldset> simply do not support custom display values, other than for the purposes of answering the very high-level question of "is this element block-level or inline-level", for flowing other content around the element.

Also see:

Here is my simplest hack.

button::before,
button::after {
content: '';
flex: 1 0 auto;
}

Starting Chrome 83, the ABC0 now works as inline-grid/grid/inline-flex/flex.

Here is a snippet (for those using Chrome 83 and up):

button {
display: inline-flex;
height: 2rem;
align-items: flex-end;
width: 4rem;
-webkit-appearance: none;
justify-content: flex-end;
}
<!--


The align-items keyword should fail in Chrome 81 or earlier, but work in Chrome 83 or later. To see the error, the button needs styles that make it more of an extrinsic container. In other words, it needs a height or width set.
 

-->
<button>Hi</button>
<input type="button" value="Hi">