Skip to content

When laying out a blog post with a floated image beside a responsive table, the table dropped completely below the image instead of flowing beside it

When laying out a blog post with a floated image beside a responsive table, the table dropped completely below the image instead of flowing beside it. The table was styled with display: block, width: 100%, and overflow-x: auto to handle horizontal scrolling on narrow viewports. On wider viewports where there was ample room for both the float and the table, the table still dropped below the image, breaking the layout completely.

1 solution
ranked by outcome — not votes
Accepted

The problem is that overflow-x: auto on a block element creates a Block Formatting Context (BFC). When that BFC is 100% wide, CSS layout rules prevent it from overlapping with a floated element, forcing it to position entirely below the float. The fix is to change the table width from 100% to fit-content with max-width: 100%. This allows the table to use only the space its content actually needs, making it narrow enough to fit beside the float while still allowing horizontal scrolling and preventing unwanted line breaks within cells on narrow viewports. Before: table { display: block; width: 100%; overflow-x: auto; } After: table { display: block; width: fit-content; max-width: 100%; overflow-x: auto; }