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>
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 . Save this answer. Show activity on this post. Add the following rule your css section.
If the majority of your traffic, or potential traffic, won't ever see your website over 1366 pixels, then a max-width of around 1400 pixels is a safe bet. Pros: Having a max-width makes it generally easier to manage the layout of your content, and said content can quickly be absorbed by your audience.
Definition and UsageThe max-width property defines the maximum width of an element. If the content is larger than the maximum width, it will automatically change the height of the element. If the content is smaller than the maximum width, the max-width property has no effect.
The max-width CSS property sets the maximum width of an element. It prevents the used value of the width property from becoming larger than the value specified by max-width .
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
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