Why can't <fieldset> be flex containers?
According to Bug 984869 - display: flex
doesn't work for button elements,
<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 putdisplay:table
on it.Stepping back even further, this isn't specific to
<button>
. Consider<fieldset>
andwhich also have special rendering behavior:<table>
data:text/html,<fieldset style="display:flex"><div>abc</div><div>def</div>
In these cases, Chrome agrees with us and disregards the
flex
display mode. (as revealed by the fact that "abc" and "def" end up being stacked vertically). The fact that they happen to do what you're expecting on<button style="display:flex">
is likely just due to an implementation detail.In Gecko's button implementation, we hardcode
<button>
(and<fieldset>
,and) as having a specific frame class (and hence, a specific way of laying out the child elements), regardless of the<table>
display
property.If you want to reliably have the children reliably arranged in a particular layout mode in a cross-browser fashion, your best bet is to use a wrapper-div inside the button, just as you would need to inside of
aa<table>
or<fieldset>
.
Therefore, that bug was marked as "resolved invalid".
There is also Bug 1047590 - display: flex;
doesn't work in <fieldset>
, currently "unconfirmed".
Good news: Firefox 46+ implements Flexbox for <fieldset>
. See bug 1230207.
I find out this might be a bug on Chrome and Firefox where legend
and fieldset
are replaced elements.
Bugs Reported:
Bug Chrome (fixed since v86)
Bug Firefox (fixed since v46)
A possible Workaround:
A possible workaround would be using <div role="group">
in HTML, and applying in CSS div[role='group']
as selector.
UPDATE
In Chrome version 83 button
can work with the display: inline-grid/grid/inline-flex/flex
, you can see the demo below:
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">