Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent a table in overflow:auto div from shrinking

I'm having a bit of an issue getting some stylesheet behavior that I want. I'm not even sure if it's possible. Basically I'm attempting to place a table with a variable number of cells with static cell width in a DIV with overflow: auto, and my goal is that when the tables width extends past the width of the container DIV that it becomes scrollable.

This isn't the case. The cells get shrunk together. A very basic representation (with inline styles for ease on this; not actually in the application haha) of the code:

<div style="width: 1000px; overflow-x: auto;">
    <table>
        <tr>
            <td style="width:400px;">
                This
            </td>
            <td style="width:400px;">
                Should
            </td>
            <td style="width:400px;">
                Scroll!
            </td>
        </tr>
    </table>
</div>

Is there anyway I can do this with CSS, or am I going to have to go back to setting the width inline on a second div containing the table through calculations?

like image 599
James Richard Avatar asked Oct 16 '25 03:10

James Richard


2 Answers

This should help

<table style="width: max-content;">
like image 56
Yash Patil Avatar answered Oct 18 '25 19:10

Yash Patil


Works if you set the width on the table itself.

<table style="width:1200px;">

The td will always shrink to the necessary size, they won't push the table wider in that situation.

like image 41
Nate B Avatar answered Oct 18 '25 21:10

Nate B