Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/CSS - how can I make my table columns never go wider what i speficy as width

Tags:

html

css

I have <td width="25%"> but if it gets populated with something thats largers than 25% it gets bigger. how can I stop it from getting bigger than 25% with css or html

like image 526
code511788465541441 Avatar asked Dec 10 '22 10:12

code511788465541441


2 Answers

use the css table-layout:fixed on the table

Found here: http://www.w3.org/TR/CSS21/tables.html#fixed-table-layout

like image 195
Bazzz Avatar answered May 17 '23 13:05

Bazzz


You can use max-width: 25%; to prevent the maximum width from being exceeded; having said that this is a css solution, rather than an html-attribute, so you'd have to use:

<td style="max-width: 25%;">...</td>

Or, better yet, specify in the stylesheet:

td {
    max-width: 25%;
}

As @Asherer correctly notes (in the comments) this won't necessarily work in all browsers. To enforce the max-width it might be worth wrapping the contents of the td in a div (or, if they're in-line elements, a span) and applying the max-width to that element.

For example:

<td>
    <div>
        Cell contents
    </div>
</td>

td {
    width: 25%;
    max-width: 25%;
}

td div {
    width: 100%;
    overflow: hidden; /* or 'auto', or 'scroll' */
}

This can get a bit messy over time, and I'm not usually a fan of adding unnecessary mark-up, but in this case it does aid the cross-browser compatibility.

like image 27
David Thomas Avatar answered May 17 '23 15:05

David Thomas