When I set display: block;
and width: auto;
on a button, I'd expect the button to stretch to fill the container as other block elements do. For some reason, it doesn't, at least not in latest Chrome.
When Googling around, I found a lot of people asking the same question, who were satisfied with an answer to "How do I stretch my buttons to fill the container?" That is not what I'm interested in. (I'm perfectly able to stretch my buttons any way I need.) Inspecting button properties, including the ones imposed by default by the browser didn't help me either.
I'd like to understand, what causes buttons to ignore display: block; width: auto;
and stay horizontally sized based on their contents.
Here's a demonstration of what I mean:
button {
display: block;
}
<button style="width: auto;">button with `display:block; width:auto;`</button>
<button style="width: 100%;">button with `display:block; width:100%`</button>
I'd expect the button with width:auto;
to be stretched as well.
Just to be absolutely clear, the above is not a duplicate to this or any similar questions that only have answers describing the ways to stretch the elements in question.
The above is not a duplicate of this question either. Though the answer applies here as well, because apparently the issues with sizing of input and buttons have the same core cause, the question doesn't mention buttons at all. Furthermore, there seems to be some uncertainty on whether <button>
is even a proper "replaced element". This post (arguably) well serves the purpose of leading users with an issue to its solution.