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

How to keep radio button and label in the same line with long label content in HTML/CSS?

I have an issue with an HTML radio button and its label. I want to keep the radio button and label in the same line. But if the label text is very long, the label will sit below the radio button.
html:

<div class="form-check fs-3 mb-3">
  <input id="low-3" name="low-3" type="radio" value="light">
  <label for="low-3">very long very long very long very long context</label>
</div>

How can I keep the label on the same line as the radio button?

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 :

Problem

Bootstrap’s _reset.scss file sets label { display: inline-block; } by default, as you can see in the screenshot below.

Screenshot

Solution

Simply set the Bootstrap d-inline class on the <label>, which is equivalent to label { display: inline; }.

See the snippet below.

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap demo</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
</head>

<body>
  <div class="form-check fs-3 mb-3">
    <input id="low-3" name="low-3" type="radio" value="light">
    <label class="d-inline" for="low-3">very long very long very long very long very long very long very long context</label>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
</body>

</html>
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