margin: auto;
will work when you have some fixed width defined to your element, if you have dynamic width, you can use transform: translateX(-50%);
to center your element horizontally(which I assume is what you want) or you can add translateY(-50%)
if you want to center vertically.
For example:
div {
position: absolute;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
/* specifying x and y separately on purpose so that you understand whats going
on and what you can remove what you don't need from the above snippet */
}
Demo
If you want just horizontal centered element, get rid of top: 50%
and translateY(-50%)
respectively. Just note that it's a good practice to nest your absolute
positioned element inside relative
positioned element.