Buttons have a magic property where you can use margin: auto
on them without setting a width. I'd like to be able to do this with a div. Is it possible?
div, button {
background-color: #FFA;
display: block;
margin: auto
}
<div> Center me </div>
<button> I Center Magically </button>
They do this by having a sort of magic internal sizing function which makes them only as big as their text:
Why doesn't "display: block" & "width: auto" stretch a button to fill the container?
I would like to know if I can make a div do this, without wrapping it in any containers (I know flex box around it would work).
Can I make a div shrink to text-width like a button, while still being display block, without necessitating a specific container?
EDIT: inline-block
will not work for my use case.