I have HTML similar to this:
<div class="table-responsive">
<table class="table borderless">
<caption>
<h3>Announcements</h3>
</caption>
<tbody>
<tr >
<td>
If you are waiting for your certificate from your trainer, please contact them. Our turnaround time is between 1-2 months from the time we receive your details from your trainer, which we expect to be at the start of your program. The remainder is dependent upon how quickly your trainer has send in all the required paperwork and made payment, etc.
</td>
</tr>
</tbody>
</table>
</div>
When I view the output in a small view-port, the table is re-sized properly, but the paragraph content in the table cells are not wrapped, so scroll-bars are shown. I expected the responsive behavior would have been to wrap the paragraph content. How do I achieve this?
This question is related to
twitter-bootstrap
responsive-design
twitter-bootstrap-3
html-table
use it in css external file.
.td-table
{
word-wrap: break-word;
word-break: break-all;
white-space: normal !important;
text-align: justify;
}
just simply use as below and it will word wrap any long text within a table . No need to anything else
<td style="word-wrap: break-word;min-width: 160px;max-width: 160px;">long long comments</td>
.table td.abbreviation {_x000D_
max-width: 30px;_x000D_
}_x000D_
.table td.abbreviation p {_x000D_
white-space: nowrap;_x000D_
overflow: hidden;_x000D_
text-overflow: ellipsis;_x000D_
_x000D_
}
_x000D_
<table class="table">_x000D_
<tr>_x000D_
<td class="abbreviation"><p>ABC DEF</p></td>_x000D_
</tr>_x000D_
</table>
_x000D_
EDIT
I think the reason that your table is not responsive to start with was you did not wrap in .container
, .row
and .col-md-x
classes like this one
<div class="container">
<div class="row">
<div class="col-md-12">
<!-- or use any other number .col-md- -->
<div class="table-responsive">
<div class="table">
</div>
</div>
</div>
</div>
</div>
With this, you can still use <p>
tags and even make it responsive.
Please see the Bootply example here
The behaviour is on purpose:
Create responsive tables by wrapping any .table in .table-responsive to make them scroll horizontally on small devices (under 768px). When viewing on anything larger than 768px wide, you will not see any difference in these tables.
Which means tables are responsive by default (are adjusting their size). But only if you wish to not break your table's lines and add scrollbar when there is not enough room use .table-responsive
class.
If you take a look at bootstrap's source you will notice there is media query that only activates on XS screen size and it sets text of table to white-space: nowrap
which causes it to not breaking.
Simply remove .table-responsive
element/class from your html code.
Add your new class "tableresp" with table-responisve class and then add below code in your js file
$(".tableresp").on('click', '.dropdown-toggle', function(event) {
if ($('.dropdown-menu').length) {
var elm = $('.dropdown-menu'),
docHeight = $(document).height(),
docWidth = $(document).width(),
btn_offset = $(this).offset(),
btn_width = $(this).outerWidth(),
btn_height = $(this).outerHeight(),
elm_width = elm.outerWidth(),
elm_height = elm.outerHeight(),
table_offset = $(".tableresp").offset(),
table_width = $(".tableresp").width(),
table_height = $(".tableresp").height(),
tableoffright = table_width + table_offset.left,
tableoffbottom = table_height + table_offset.top,
rem_tablewidth = docWidth - tableoffright,
rem_tableheight = docHeight - tableoffbottom,
elm_offsetleft = btn_offset.left,
elm_offsetright = btn_offset.left + btn_width,
elm_offsettop = btn_offset.top + btn_height,
btn_offsetbottom = elm_offsettop,
left_edge = (elm_offsetleft - table_offset.left) < elm_width,
top_edge = btn_offset.top < elm_height,
right_edge = (table_width - elm_offsetleft) < elm_width,
bottom_edge = (tableoffbottom - btn_offsetbottom) < elm_height;
console.log(tableoffbottom);
console.log(btn_offsetbottom);
console.log(bottom_edge);
console.log((tableoffbottom - btn_offsetbottom) + "|| " + elm_height);
var table_offset_bottom = docHeight - (table_offset.top + table_height);
var touchTableBottom = (btn_offset.top + btn_height + (elm_height * 2)) - table_offset.top;
var bottomedge = touchTableBottom > table_offset_bottom;
if (left_edge) {
$(this).addClass('left-edge');
} else {
$('.dropdown-menu').removeClass('left-edge');
}
if (bottom_edge) {
$(this).parent().addClass('dropup');
} else {
$(this).parent().removeClass('dropup');
}
}
});
var table_smallheight = $('.tableresp'),
positioning = table_smallheight.parent();
if (table_smallheight.height() < 320) {
positioning.addClass('positioning');
$('.tableresp .dropdown,.tableresp .adropup').css('position', 'static');
} else {
positioning.removeClass('positioning');
$('.tableresp .dropdown,.tableresp .dropup').css('position', 'relative');
}
So you can use the following :
td {
white-space: normal !important; // To consider whitespace.
}
If this doesn't work:
td {
white-space: normal !important;
word-wrap: break-word;
}
table {
table-layout: fixed;
}
Fine then. You can use CSS word wrap property. Something like this :
td.test /* Give whatever class name you want */
{
width:11em; /* Give whatever width you want */
word-wrap:break-word;
}
The UberNeo response is Ok and i like it because you do not have to modify anything else except the TD. The only point is that you also have to add "white-space:normal" to the style in order to maintain the responsive characteristics of the table, if not, at certain resolutions the wrap is not made and the scroll of the table does not appear.
style="word-wrap: break-word;min-width: 160px;max-width: 160px;white-space:normal;"
Source: Stackoverflow.com