I'm trying to center inner elements of a <button>
-tag with flexbox's justify-content: center
. But Safari does not center them. I can apply the same style to any other tags and it works as intended (see the <p>
-tag). Only the button is left-aligned.
Try Firefox or Chrome and you can see the difference.
Is there any user agent style I have to overwrite? Or any other solution to this problem?
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>
And a jsfiddle: http://jsfiddle.net/z3sfwtn2/2/
The problem is that the
<button>
element is not designed to be a flex (or grid) container.At the time of this writing, Webkit-based browsers (Chrome and Safari) adhere to this design principle. Firefox and Edge do not. I didn't test other browsers.
Therefore, you should have no problem making a
<button>
element a flex (or grid) container in Firefox and Edge. But don't expect it to work in Chrome or Safari.This behavior currently applies to at least three elements:
<button>
<fieldset>
<legend>
I'm theorizing, but I believe that the idea is to maintain a level of common sense when styling HTML elements. For instance, the HTML gods wanted to prevent authors from turning a button into a table.
However, at least in this case, there is a simple and easy workaround:
Adjusted HTML (wrapped
button
content in aspan
)Adjusted CSS (targeted
span
)Revised Demo
NOTE: Although they cannot be flex containers,
button
elements can be flex items.References:
Here is my simplest hack.