Reputation: 2669
I'm trying to set the maximum width of a table, but it doesn't seem to be working. If a word inside the table is too long, it automatically expands the width of the table. How do I stop that from happening, and instead have the word that's too long go to the next line?
<style>table {
max-width: 300px;
}
table,
td {
border: 1px solid red;
}
div {
border: 1px solid blue;
word-wrap: break-word;
}
</style>
<table>
<tr>
<td>
<div> hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
</div>
</td>
</tr>
</table>
Upvotes: 10
Views: 29744
Reputation: 121
Putting max-width inside the div solves the problem.
<style>
table,td {
border: 1px solid red;
}
div {
border: 1px solid blue;
word-wrap: break-word;
max-width: 300px;
}
</style>
<table>
<tr>
<td>
<div> hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello </div>
</td>
</tr>
</table>
Upvotes: 0
Reputation: 192
If you want to make your max-width to work, you need to set the CSS property table-layout: fixed;
on the table and use width
, not max-width
.
https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout
Upvotes: 16
Reputation: 296
word-wrap: break-word;
will work if you have width assigned to the container, you are applying this property to. Thus, you will have to assign width:100px;
or desired width to div
tag.
<style type="text/css">
div {
width: 100px; /* this will limit the length of the word to 100px */
border: 1px solid blue;
word-wrap: break-word;
}
</style>
Upvotes: 0
Reputation: 2498
Add the following rule your css section.
div{
overflow-wrap: break-word;
max-width: 300px;
Upvotes: 0