Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep checkboxes in html table algined

Consider a simple centered aligned table, with some check boxes

Clicking a button will automatically select some check boxes and ADD some dynamic text next to check boxes. Now the check boxes are not aligned any more and the table looks weird.

$('#add').click(function () {
    $('#bike').attr('checked','checked').after("<span>Good Selection</span>");
    $('#airplean').attr('checked','checked').after("<span>Woow</span>");

});

http://jsfiddle.net/m9884jy7/1/ Table

In plain html this can be fixed it by adding extra column....

But Please consider that I have simplified the sample and I can not add column :(

I am looking for css solution, or may be a hint that can be used. Also as I have the id of checkboxes, I could add any html DOM which can help correct alignment.

like image 844
Alireza Fattahi Avatar asked Sep 20 '15 11:09

Alireza Fattahi


2 Answers

here you go: http://jsfiddle.net/m9884jy7/2/

first thing you should do, is to add a span near every checkbox, you have - so add an empty one near car:

$('#car').attr('checked','checked').after("<span></span>");

and then add this css, to set a default width to a span:

td span{
    display: inline-block;
    text-align: left;
    width: 200px;
    padding-left:3px;
}
like image 146
MoLow Avatar answered Oct 23 '22 04:10

MoLow


As the OP asked for simple CSS solution

input[type='checkbox'] {
  float: left;
  margin-left: 50%
}
td>span {
  float: left;
}

Demo

like image 30
J Santosh Avatar answered Oct 23 '22 05:10

J Santosh