Setting the maximum width of a table

17,417

If you want to make your max-width to work, you need to set the CSS property table-layout: fixed; on the table

https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout

Share:
17,417
frosty
Author by

frosty

Updated on June 14, 2022

Comments

  • frosty
    frosty almost 2 years

    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>