I created a simple example to showcase the idea of having an absolutely positioned element with left
and right
set to 0
but with very different results in latest Chrome and Safari.
Based on the comments to this question and this answer, I would think it would work consistently based on how it's outlined in the CSS spec, but it doesn't. Why is this the case?
I'm aware that setting a specific width will resolve, but 1) why the difference cross-browser, and 2) is there a better way to center than specifying explicit width?
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.vis {
width: 100%;
position: relative;
}
.btn {
position: absolute;
left: 0;
right: 0;
margin:auto;
display:block;
}
</style>
<title>Document</title>
</head>
<body>
<div class="vis">
<button class="btn">Test!</button>
</div>
</body>
</html>