3

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,438
  • 68
  • 55
  • 126
Irfanullah Jan
  • 1,932
  • 1
  • 18
  • 29

2 Answers2

9

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
  • 53,090
  • 8
  • 62
  • 66
  • 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
  • 78,842
  • 31
  • 152
  • 211