When text in a Bootstrap Table cell wraps, it's usually because the content is wider than the available column width, and the default CSS white-space property allows wrapping.
To force the text onto a single line, you primarily use the CSS property white-space: nowrap;. You'll also likely want to handle the overflow, as long text will extend beyond the cell boundaries.
Here are the most common ways to achieve this in Bootstrap Table:
Method 1: Using the class option for a specific column (Recommended for column-wide styling)
This is often the cleanest way if you want to apply the "no wrap" behavior to an entire column.
1. Add custom CSS:
/* In your custom.css file or within <style> tags */
.no-wrap-text {
white-space: nowrap;
overflow: hidden; /* Hide the overflowing content */
text-overflow: ellipsis; /* Add "..." for hidden content */
}
/* Optional: adjust max-width if needed for ellipsis to show consistently */
/* For responsiveness, use percentages or max-width on the column/cell itself if possible */
/* Example: If you have a .some-column class applied via the 'class' option */
/* .some-column {
max-width: 150px; /* Or whatever fixed width you want */
} */
2. Apply the class in your Bootstrap Table column definition:
$('#myTable').bootstrapTable({
columns: [{
field: 'id',
title: 'ID'
}, {
field: 'name',
title: 'Name',
// Apply the custom class here
class: 'no-wrap-text'
}, {
field: 'description',
title: 'Description',
class: 'no-wrap-text' // Apply to another column if needed
}],
data: [{
id: 1,
name: 'John Doe',
description: 'This is a very long description that should be displayed on a single line with an ellipsis at the end if it overflows the column width.'
}, {
id: 2,
name: 'Jane Smith',
description: 'Short text.'
}]
});
Method 2: Using the cellStyle option (For dynamic styling or more complex conditions)
This option allows you to apply inline styles or classes based on the cell's data.
$('#myTable').bootstrapTable({
columns: [{
field: 'id',
title: 'ID'
}, {
field: 'name',
title: 'Name'
}, {
field: 'description',
title: 'Description',
cellStyle: function(value, row, index, field) {
return {
css: {
'white-space': 'nowrap',
'overflow': 'hidden',
'text-overflow': 'ellipsis'
}
};
}
}],
data: [{
id: 1,
name: 'John Doe',
description: 'This is a very long description that should be displayed on a single line with an ellipsis at the end if it overflows the column width.'
}, {
id: 2,
name: 'Jane Smith',
description: 'Short text.'
}]
});
Method 3: Using the formatter option (Most flexible, but more verbose)
The formatter function lets you completely control the HTML content of a cell. You can wrap the text in a <span> and apply styles to it.
$('#myTable').bootstrapTable({
columns: [{
field: 'id',
title: 'ID'
}, {
field: 'name',
title: 'Name'
}, {
field: 'description',
title: 'Description',
formatter: function(value, row, index) {
// Create a span with the desired styles
return '<span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block;">' + value + '</span>';
// Note: display: block; is important if you want the span to respect overflow/ellipsis within the table cell.
// Alternatively, apply the .no-wrap-text class defined in Method 1 to this span.
// return '<span class="no-wrap-text">' + value + '</span>';
}
}],
data: [{
id: 1,
name: 'John Doe',
description: 'This is a very long description that should be displayed on a single line with an ellipsis at the end if it overflows the column width.'
}, {
id: 2,
name: 'Jane Smith',
description: 'Short text.'
}]
});
Method 4: Global CSS (For all cells in all Bootstrap Tables)
If you want all cells in all your Bootstrap Tables to behave this way, you can apply the CSS more broadly.
/* In your custom.css file or within <style> tags */
.bootstrap-table .table td,
.bootstrap-table .table th { /* Apply to both data cells and headers if needed */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Caution: This is a very broad rule and might affect other parts of your application that use similar table structures. Use with care.
Full Example (Using Method 1 - Recommended)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap Table Single Line Text</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap Table CSS -->
<link href="https://unpkg.com/bootstrap-table@1.22.1/dist/bootstrap-table.min.css" rel="stylesheet">
<style>
/* Custom CSS to prevent text wrapping */
.no-wrap-text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
/* Optional: Set a max-width if you want a fixed size before ellipsis appears */
/* For example, for a description column */
max-width: 200px; /* Adjust as needed */
}
/* Adjust width of table container to demonstrate overflow */
.table-container {
width: 70%; /* Make it narrower than default to show ellipsis */
margin: 20px auto;
}
</style>
</head>
<body>
<div class="container table-container">
<h1>Bootstrap Table Single Line Cells</h1>
<p>Text in the "Name" and "Description" columns will be displayed on one line with an ellipsis if it overflows.</p>
<table id="myTable"
data-toggle="table"
data-height="auto"
data-classes="table table-striped table-hover">
</table>
</div>
<!-- jQuery (required by Bootstrap and Bootstrap Table) -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<!-- Bootstrap JS (optional for Bootstrap Table, but good practice for full Bootstrap features) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<!-- Bootstrap Table JS -->
<script src="https://unpkg.com/bootstrap-table@1.22.1/dist/bootstrap-table.min.js"></script>
<script>
$(function() {
var data = [{
id: 1,
name: 'Alice Johnson',
description: 'This is a very long description that should be displayed on a single line with an ellipsis at the end if it overflows the column width of the table.'
}, {
id: 2,
name: 'Bob Smith',
description: 'A shorter piece of text.'
}, {
id: 3,
name: 'Charlie Brown',
description: 'Another example of text that will eventually overflow if the column is too narrow.'
}, {
id: 4,
name: 'David Lee',
description: 'Just some normal text.'
}];
$('#myTable').bootstrapTable({
columns: [{
field: 'id',
title: 'ID',
sortable: true
}, {
field: 'name',
title: 'Name',
sortable: true,
class: 'no-wrap-text' // Apply the custom class here
}, {
field: 'description',
title: 'Description',
sortable: true,
class: 'no-wrap-text' // Apply to another column
}],
data: data
});
});
</script>
</body>
</html>
Choosing the right method:
class option (Method 1): Best for applying consistent styling to an entire column. It keeps your JavaScript cleaner by separating concerns (CSS for style, JS for data and structure).
cellStyle option (Method 2): Useful when you want to apply styles conditionally (e.g., only for cells with a certain value, or if you need to pass dynamic inline styles directly).
formatter option (Method 3): Most powerful for completely transforming the cell's content, but also the most verbose. Use it if you need to add complex HTML, buttons, or conditional formatting that goes beyond just styling.
- Global CSS (Method 4): Simplest to implement if you want all cells to behave this way, but least flexible and potentially impacts other tables.
For simply displaying text on one line with ellipsis, the class option (Method 1) is usually the most elegant and recommended approach.