I have long data in one Cell, I need to put the data up to a specific width in one line and the rest wraps to the next line.
I have been able to make it in a line but can not wrap after specific width with td{word-wrap: break-word;}, probably because of td{white-space: nowrap}. I don’t intend to wrap in middle of the word.
The style and HTML structure are going to be used to create tables in an ERP.
I have the following CSS:
``
th { /* header cell */
border-bottom: 2px solid #EB9486;
position: sticky;
top: 0;
background-color: #F9F8F8;
}
td { /*body cell */
white-space: nowrap;
border: 1px solid black;
}
/*Adds scroll to the table*/
.table-container {
overflow: auto;
height: 300px;
}
``
``
<div class="table-container">
<table>
<thead>
<tr>
<th class="FirstHeader">Name</th>
<th class="SecondHeader">Job</th>
<th>Salary</th>
<th>Name</th>
<th>Job</th>
<th>Salary</th>
<th>Name</th>
<th>Job</th>
<th>Salary</th>
<th>Name</th>
<th>Job</th>
<th>Salary</th>
<th>Name</th>
<th>Job</th>
<th>Salary</th>
<th>Name</th>
<th>Job</th>
<th>Salary</th>
<th>Name</th>
<th>Job</th>
</tr>
</thead>
<tbody>
<tr class="FirstColumn">
<td>Olivia Saturday</td>
<td>Senior Data Engineer</td>
<td>$320,000</td>
<td>David Smith</td>
<td>Manager Strategic Finance Manager Strategic Finance Manager11</td>
<td>$180,000</td>
<td>David Smith</td>
<td> Manager Strategic Finance Manager Strategic Finance Manager</td>
<td>$180,000</td>
<td>David Smith</td>
<td> Manager Strategic Finance Manager Strategic Finance Manager</td>
<td>$180,000</td>
<td>David Smith</td>
<td> Manager Strategic Finance Manager Strategic Finance Manager</td>
<td>$180,000</td>
<td>David Smith</td>
<td> Manager Strategic Finance Manager Strategic Finance Manager</td>
<td>$180,000</td>
<td>David Smith</td>
<td>Manager Strategic Finance Manager Strategic Finance Manager</td>
<td>$180,000</td>
<td>David Smith</td>
<td>Manager Strategic Finance Manager Strategic Finance Manager</td>
<td>$180,000</td>
</tr>
<tr class="SecondColumn">
<td>David Smith</td>
<td> Manager Strategic Finance Manager Strategic Finance Manager</td>
<td>$180,000</td>
</tr>
<tr>
<td>Margaret Mills</td>
<td>Lead Software Engineer</td>
<td>$250,000</td>
</tr>
<tr>
<td>Paul Brown</td>
<td>Digital Content Writer</td>
<td>$145,000</td>
</tr>
<tr>
<td>Stephanie Tavartkiladze</td>
<td>Digital Project Manager</td>
<td>$250,000</td>
</tr>
<tr>
<td>Olivia Saturday</td>
<td>Senior Data Engineer</td>
<td>$320,000</td>
</tr>
<tr>
<td>David Smith</td>
<td>Strategic Finance Manager</td>
<td>$180,000</td>
</tr>
<tr>
<td>Margaret Mills</td>
<td>Lead Software Engineer</td>
<td>$250,000</td>
</tr>
<tr>
<td>Paul Brown</td>
<td>Digital Content Writer</td>
<td>$145,000</td>
</tr>
<tr>
<td>Stephanie Tavartkiladze</td>
<td>Digital Project Manager</td>
<td>$250,000</td>
</tr>
<tr>
<td>Olivia Saturday</td>
<td>Senior Data Engineer</td>
<td>$320,000</td>
</tr>
<tr>
<td>David Smith</td>
<td>Strategic Finance Manager</td>
<td>$180,000</td>
</tr>
<tr>
<td>Margaret Mills</td>
<td>Lead Software Engineer</td>
<td>$250,000</td>
</tr>
<tr>
<td>Paul Brown</td>
<td>Digital Content Writer</td>
<td>$145,000</td>
</tr>
<tr>
<td>Stephanie Tavartkiladze</td>
<td>Digital Project Manager</td>
<td>$250,000</td>
</tr>
<tr>
<td>Olivia Saturday</td>
<td>Senior Data Engineer</td>
<td>$320,000</td>
</tr>
<tr>
<td>David Smith</td>
<td>Strategic Finance Manager</td>
<td>$180,000</td>
</tr>
<tr>
<td>Margaret Mills</td>
<td>Lead Software Engineer</td>
<td>$250,000</td>
</tr>
<tr>
<td>Paul Brown</td>
<td>Digital Content Writer</td>
<td>$145,000</td>
</tr>
<tr>
<td>Stephanie Tavartkiladze</td>
<td>Digital Project Manager</td>
<td>$250,000</td>
</tr>
</tbody>
</table>
</div>
``
If I don’t use td{white-space: nowrap;}
the data wraps because of the number of columns.
Perhaps I should change the HTML as well. I don’t know. I am confused.
Please help me with that.
2
Answers
Not too sure if I understand the question, but you can use the nowrap keyword in the td tag:
Option 1:
If you need a fixed width you can also create a class…
Option 2:
html:
css:
or do inline styles
Option 3:
Basically I think your problem stems from a missing column, and maybe that was throwing you?
The code below should give you a scrolling table with no wrapping.
and maybe the empty (merged) cells are not really neccessary, if Show_Empty_Cells is
Experiment with it and try changing the styles and review the effects of the changes.