In my code there is a table in which I have divisions which has table row consisting checkboxes horizontally. Here is my sample code, full code is in fiddle Here
HTML:
<table cellpadding="0" border="0" width="100%" cellspacing="0">
<tr>
<td style="text-align:left" width="65px"><strong> Color: </strong>
</td>
<td style="float:left; text-align:left; width:100%">
<div style="display:table; width:100%">
<div style="width:100%;display:table-row">
<input type="checkbox" />
<label class="btn"> <span>A</span>
</label>
<input type="checkbox" />
<label class="btn"> <span>B</span>
</label>
<input type="checkbox" />
<label class="btn"> <span>C</span>
</label>
<input type="checkbox" />
<label class="btn"> <span>D</span>
</label>
<input type="checkbox" />
<label class="btn"> <span>E</span>
</label>
<input type="checkbox" />
<label class="btn"> <span>F</span>
</label>
<input type="checkbox" />
<label class="btn"> <span>G</span>
</label>
<input type="checkbox" />
<label class="btn"> <span>H</span>
</label>
<input type="checkbox" />
<label class="btn"> <span>I</span>
</label>
</div>
</div>
</tr>
</table>
CSS:
.btn {
display: table - cell;
}
In pc and tablet view it looks perfect as i want, justified from both left and right side, but in mobile view is it possible to break it into two lines for making it responsive? Please look at fiddle.
To make a responsive table, you can make the width of each td 100% and insert a related heading in the td on mobile browsers (that are less 768px width.)
The best way to do this would be with div s and using CSS' display: table , display: table-row and display: table-cell properties, along with some breakpoints. Otherwise, you're going to have a lot of ugly code. This isn't the semantically greatest solution, but as far as responsive goes, it does work.
The primary method of making a responsive table is to add a class table table-responsive inside the table tag. To make a table responsive, we wrap the whole table inside a container tag like div with property overflow-x equals to auto .
You can use a media-query to set the divs as display: block;
. Demo
Leave the css you have for the larger displays, then use the media-query to target the smaller ones. I would recommend wrapping the label and checkbox together also to keep them from breaking apart:
HTML
<div class="table-cell">
<input type="checkbox" class="checkbox" />
<label class="btn"> <span>A</span>
</label>
</div>
CSS
.table {
display: table;
width: 100%;
}
.table-row {
display: table-row;
width: 100%;
}
.table-cell {
display: table-cell;
}
@media screen and (max-width: 479px) {
.table, .table-row {
display: block;
}
.table-cell {
display:inline-block;
}
}
You may need to change the alignment of the labels to your liking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With