If you are looking to center an element in Bootstrap 4 based on the width of its content, this solution is specifically tailored for that:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<div class="container-fluid">
<div class="row bg-light">
<div class="col-auto mx-auto">
<ul class="bg-secondary text-light">
<li>Short Item #1</li>
<li>Much Longer Item #2 </li>
<li>Short Item #3</li>
<li>Much Much Longer Item #3</li>
</ul>
</div>
</div>
</div>
The .col-auto
class can be used with breakpoints for enhanced responsiveness, allowing different column behaviors at various screen sizes. The mx-auto
class ensures even left and right margins, resulting in the centering of the column on the screen.