Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Align <h1> and <span> in the same line

I am using Bootstrap 5 to build a webpage. My html code is as following:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <title>Homepage</title>
</head>
<body>
    <div class="container-fluid p-1 bg-secondary text-white text-center">
        <h1>Employee details</h1>
        <span class="badge rounded-pill bg-success">2020-21 period</span>    
    </div>
    
</body>
</html>

I want to make the <h1> and <span> appear in the same line, rather than one below the other, but am unable to do so. Please help me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The updated code is as below:

<div class="container-fluid p-1 bg-secondary text-white d-flex align-items-center justify-content-center">
  <h1>Employee details</h1>
  <span class="badge rounded-pill bg-success ms-3">2020-21 period</span>
</div>

Explanation:

  1. Add class d-flex to make h1 and span in same line.
  2. Add class align-items-center justify-content-center to make them center.
  3. (Optional) Add margin by class ms-3 to the span div to add space between h1 and span
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading