Currently the table is too wide and causes the browser to add a horizontal scroll bar.
To make an HTML table tit the screen: Set the width to 100%, so that your code will look like this: <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td>My html table is set to fit the screen</td> </tr> ...
CSS:
table { table-layout:fixed; }
Update with CSS from the comments:
td { overflow: hidden; text-overflow: ellipsis; word-wrap: break-word; }
For mobile phones I leave the table width but assign an additional CSS class to the table to enable horizontal scrolling (table will not go over the mobile screen anymore):
@media only screen and (max-width: 480px) { /* horizontal scrollbar for tables if mobile screen */ .tablemobile { overflow-x: auto; display: block; } }
Sufficient enough.
If the table content is too wide (as in this example), there's nothing you can do other than alter the content to make it possible for the browser to show it in a more narrow format. Contrary to the earlier answers, setting width to 100% will have absolutely no effect if the content is too wide (as that link, and this one, demonstrate). Browsers already try to keep tables within the left and right margins if they can, and only resort to a horizontal scrollbar if they can't.
Some ways you can alter content to make a table more narrow:
white-space: nowrap
on any of the content (or the old nowrap
attribute,
, a nobr
element, etc.), see if you can live without them so the browser has the option of wrapping that content to keep the width down.If the table is too wide but you don't see a good reason for it (the content isn't that wide, etc.), you'll have to provide more information about how you're styling the table, the surrounding elements, etc. Again, by default the browser will avoid the scrollbar if it can.
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