If you set the parent container as a flexbox and then apply align-items: center;
, you can achieve the desired layout.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe9c91918a8d8a8a69cc8c9c9cb6c6cbc6">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<div style="width:350px;" class="d-flex align-items-center">
<h1 class="d-inline" style="white-space: no-wrap;">This is a Test This is a Test</h1>
<span class="badge badge-secondary ml-2">My Badge</span>
</div>
It's also possible to use the transform:translateY(-50%);
CSS property to position elements vertically align. Just keep in mind that this method is based on the element's height, which may cause issues if the font size is changed.
.badge {
transform:translateY(-50%);
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="51333e3e252225233e3a3e363833373b2c2b3f3f7b6b7b6b6">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<div style="width:350px;">
<h1 class="d-inline">This is a Test This is a Test</h1>
<span class="badge badge-secondary ml-2">My Badge</span>
</div>
For better control over the layout, consider adding the span
element inside the h1
tag. This approach not only provides more flexibility but also ensures proper interpretation by search engines like Google.