I am building up a dynamic table which consists of 1-50 columns depending what the user selects. When the user selects 1-6 colums there is no problem showing all the data on the screen but when the user selects more than 6 columns the table tries to squeeze the view together on the screen resulting in each row being expanded to multiple lines.
I want it to always show the text in one line as this (OK):
But having many columns will wrap the text in to two or more lines (not OK):
The column width is not defined as it also varies depending on the text to show.
How can I make sure the row will always be a one-liner like ex.1 no matter how many columns the user selects?
I have this JSFiddle demo with the code for the two above examples:
<table class='table'>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
<th>Column 5</th>
<th>Column 6</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1</td>
<td>Row 11</td>
<td>Row 11 1</td>
<td>Row 11 11</td>
<td>Row 11 11 1</td>
<td>Row 11 11 11</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 22</td>
<td>Row 22 2</td>
<td>Row 22 22</td>
<td>Row 22 22 2</td>
<td>Row 22 22 22</td>
</tr>
</tbody>
</table>
If you're using <table class="table"> on your table, Bootstrap's table class adds a width of 100% to the table. You need to change the width to auto. Also, if the first row of your table is a header row, you might need to add the width to th rather than td.
The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.
table-stripped class is used to create an alternate dark and light rows. Use the combination of table and table-stripped classes within the <table> tag to create a striped table. Example: HTML.
Try adding this css to your table elements:
white-space: nowrap
jsFiddle
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