4

I wanted to enable horizontal scrolling on tables in my articles whenever their width exceeds the available width in webpage layout. I tried to achieve this with CSS only, but failed. So I had to wrap all in a div using jQuery: $('table.data').wrap('<div class="tcontain" />');. Then I applied following CSS to tcontain div: width:100%;overflow-x:scroll;

This works but I want to avoid JavaScript. Please help!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Irfanullah Jan
  • 3,336
  • 4
  • 24
  • 34

2 Answers2

12

I found this solution but unfortunately it doesn't work in IE, not even IE9.

table{
    max-width: 100%;
    overflow-x: auto;
    display: block;
}

Exaple fiddle

In the end, I think your JS solution would be the best if you want to avoid the hassle of changing the markup in your files.

omma2289
  • 54,161
  • 8
  • 64
  • 68
  • 1
    Thanks! I agree I should keep the JS code. But your solution is best so far. Actually we wanted to enable smartphone users to scroll tables horizontally in narrow responsive design. – Irfanullah Jan Jul 21 '13 at 21:15
  • Note that you cannot specify any table-related properties for element if you do this because technically it's no longer a table anymore. Spec compliant browsers will create phantom table element between the
    element and the ``, `` or ``.
    – Mikko Rantalainen Nov 23 '18 at 09:02
-1

Give your table width 100%.

table{width: 100%;}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231