2

I want to align my checkboxes in my site and I did some search over the stackoverflow and I found some solution that couldn't help me to align me checkboxes as I want. The first solution-question and the second solution-question.

My checkboxes look like the image below...

enter image description here

The RED line is where I want all my checkboxes to be.

Here is the code for my checkboxes...

    <label id="Disease">Disease:</label><br />
    <!--emfanizei tis epiloges gia ta diseases me basi auta p exoume sti basi mas -->
    <?php
  $sql = "SELECT name FROM disease";
  $query_resource = mysql_query($sql);
  while( $name = mysql_fetch_assoc($query_resource) ):
?>
    <span><?php echo $name['name']; ?></span>
    <input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>" /><br />

<?php endwhile; ?>

Can you help me please?

Community
  • 1
  • 1
Waaaaat
  • 614
  • 2
  • 12
  • 28

3 Answers3

6

Add the following CSS to your site:

label#Disease ~ span {
  display: inline-block;
  width: 180px;
}

That's giving the sibling spans that follow your label#Disease element a set width, which will push all the checkboxes to the right.

Play with the width to get it to your desired size, making sure you don't have text overflow or other issues.

Josh Burgess
  • 8,696
  • 30
  • 45
2

Why not wrap the inputs in labels, then float them to the right hand side? The advantage being you then are using the semantic label element instead of span, and can toggle the checkbox with a click on both the text and the input

input[type=checkbox] {
  vertical-align: middle;
  float: right;
}
label {
  display: block;
  overflow: hidden;
  width: 120px;
}
<label>Label
  <input type="checkbox" name="disease[]" />
</label>
<label>Label
  <input type="checkbox" name="disease[]" />
</label>
<label>Label
  <input type="checkbox" name="disease[]" />
</label>
<label>Label
  <input type="checkbox" name="disease[]" />
</label>
<label>Label
  <input type="checkbox" name="disease[]" />
</label>
<label>Label
  <input type="checkbox" name="disease[]" />
</label>
SW4
  • 65,094
  • 17
  • 122
  • 131
  • Thank you for your time :) I will try your solution just to see if it works because I already accept the other answer . Thank you again :) – Waaaaat May 05 '15 at 14:28
0

You can set a text-align:right css rule to the wrapper of the checkboxes.

Or, you can put the checkboxes into table cells like this:

<table>
  <tbody>
    <tr>
      <td>labelsdfsdf</td>
      <td><input type="checkbox"></td>
    </tr>
    <tr>
      <td>label</td>
      <td><input type="checkbox"></td>
    </tr>
    <tr>
      <td>label</td>
      <td><input type="checkbox"></td>
    </tr>
    <tr>
      <td>labelsdf sdfsdfsdf</td>
      <td><input type="checkbox"></td>
    </tr>
    <tr>
      <td>label</td>
      <td><input type="checkbox"></td>
    </tr>
  </tbody>
</table>
kapantzak
  • 10,937
  • 4
  • 36
  • 54