Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML & CSS radio and checkbox buttons - float: left; ?

ramzez

Coder
I ran into a problem with my html/css form.

I have radio buttons and checkboxes in my form. I want them to display like this:
1640862841201.png

It works in Codepen.io when I edit them, but as soon as I click somewhere or open de preview in a new window, it looks like this:

1640862918133.png

I used display: block; and float: left; to get this result.

CSS:
.input-radio, .input-checkbox {
  display: block;
  float: left;
  margin-right: 0.5rem;
  min-height: 1rem;
  min-width: 1.5rem;
  margin-top: 4px;
  vertical-align: middle; 
}

HTML:
  <div class="form-group">
      <p>Are you a part of the BUK girls discord channel?</p>
       <label for="radio1">
       <input type="radio" id="radio1"
              name="discordchannel"
              value="yes"
              class="input-radio">
         Yes </label>
      
      <label for="radio2">     
       <input type="radio" id="radio2"
              name="discordchannel"
              value="no"
              class="input-radio">
      No </label>
    </div>

Link: to the pen: https://codepen.io/hadassahreuvekamp/pen/yLzpebj

So why does this happen? Any tips on how to organize the checkboxes?

Thanks!
 
Solution
Try adding adding a line break, <br>, in between each option:
HTML:
<div class="form-group">
      <p>Are you a part of the BUK girls discord channel?</p>
       <label for="radio1">
       <input type="radio" id="radio1"
              name="discordchannel"
              value="yes"
              class="input-radio">
         Yes </label>
      <br> <!--    add <br> here    -->
      <label for="radio2">     
       <input type="radio" id="radio2"
              name="discordchannel"
              value="no"
              class="input-radio">
      No </label>
    </div>
    
    <div class="form-group">
      <p>What game(s) do you want to play?</p>
      <label for "minecraft">
        <input id="minecraft"...
Try adding adding a line break, <br>, in between each option:
HTML:
<div class="form-group">
      <p>Are you a part of the BUK girls discord channel?</p>
       <label for="radio1">
       <input type="radio" id="radio1"
              name="discordchannel"
              value="yes"
              class="input-radio">
         Yes </label>
      <br> <!--    add <br> here    -->
      <label for="radio2">     
       <input type="radio" id="radio2"
              name="discordchannel"
              value="no"
              class="input-radio">
      No </label>
    </div>
    
    <div class="form-group">
      <p>What game(s) do you want to play?</p>
      <label for "minecraft">
        <input id="minecraft"
               class="input-checkbox"
               type="checkbox"
               name="games"
               value="minecraft"
               > Minecraft
      </label>
      <br> <!--    and here    -->
      <label for "leaugeoflegends">
         <input id="leaugeoflegends"
                class="input-checkbox"
                type="checkbox"
                name="games"
                value="leagueoflegends"
                > League of Legends
      </label>
      <br> <!--    and here    -->
      <label for "valorant">
         <input id="valorant"
                class="input-checkbox"
                type="checkbox"
                name="games"
                value="valorant"
                > Valorant
      </label>
      <br> <!--    and here    -->
      <label for "other">
         <input id="other"
                class="input-checkbox"
                type="checkbox"
                name="games"
                value="other"
                > Other (specify below)
      </label>
    
    </div>
 
Solution
Try adding adding a line break, <br>, in between each option:
HTML:
<div class="form-group">
      <p>Are you a part of the BUK girls discord channel?</p>
       <label for="radio1">
       <input type="radio" id="radio1"
              name="discordchannel"
              value="yes"
              class="input-radio">
         Yes </label>
      <br> <!--    add <br> here    -->
      <label for="radio2">    
       <input type="radio" id="radio2"
              name="discordchannel"
              value="no"
              class="input-radio">
      No </label>
    </div>
   
    <div class="form-group">
      <p>What game(s) do you want to play?</p>
      <label for "minecraft">
        <input id="minecraft"
               class="input-checkbox"
               type="checkbox"
               name="games"
               value="minecraft"
               > Minecraft
      </label>
      <br> <!--    and here    -->
      <label for "leaugeoflegends">
         <input id="leaugeoflegends"
                class="input-checkbox"
                type="checkbox"
                name="games"
                value="leagueoflegends"
                > League of Legends
      </label>
      <br> <!--    and here    -->
      <label for "valorant">
         <input id="valorant"
                class="input-checkbox"
                type="checkbox"
                name="games"
                value="valorant"
                > Valorant
      </label>
      <br> <!--    and here    -->
      <label for "other">
         <input id="other"
                class="input-checkbox"
                type="checkbox"
                name="games"
                value="other"
                > Other (specify below)
      </label>
   
    </div>
That was the trick, thank you!
 
Back
Top Bottom