I've been trying to horizontally center a toolbar without success. Despite trying various solutions, nothing seems to work in bootstrap 4.
The closest solution I found requires setting a width which doesn't really center the toolbar. Since the number of items in the toolbar may vary, it will never be truly centered. How can I properly achieve a centered toolbar?
<!DOCTYPE html>
<html lang="en>
<head>
<meta charset="utf-8>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous></script>
<link href="css/bootstrap.min.css" rel="stylesheet>
<link href="css/main.css" rel="stylesheet>
</head>
<body class="bg-secondary>
<div class="mx-auto" style="width: 300px; background:red>
Centered element<br/>
<div class="btn-toolbar mx-auto" role="toolbar>
<div class="btn-group mr-2" role="group" aria-label="First group">
<button type="button" class="btn btn-primary>1</button>
<button type="button" class="btn btn-primary>2</button>
<button type="button" class="btn btn-primary>3</button>
<button type="button" class="btn btn-primary>4</button>
</div>
</div>
</div>
</body>
</html>
Edit: this is not a duplicate of [. That question is about a navbar, not a toolbar, in a container. I have seen that question while searching but I don't see how it applies to the html presented. https://i.sstatic.net/3MeIx.png