Kudos to SingleNegationElimination above.
My solution uses part of his, plus something I got from somewhere else, plus some of my own code I figured out.
In my CSS file:
#MyTable {
table-layout: fixed;
width: 65vw; /*I want it to take up only 65% of viewport width*/
}
In my MVC .cshtml file (but could be in html file too):
var table = $('#MyTable').DataTable({ columnDefs: [{ target: 0, /*width: "30px",*/ type: "html-num", "className": "dt-center" /*Id*/ }, { target: 1, /*width: "60px",*/ type: "string", "className": "dt-center" /*Text*/ }, { target: 2, /*width: "30px",*/ type: "html-num", "className": "dt-center" /*Number Column*/ }, { target: 3, /*width: "30px",*/ type: "html-num" }], order: [[1, 'desc']], fixedColumns: true, autoWidth: false, bFilter: false, scrollX: '95vw', scrollY: '75vh', paging: false, bInfo: false, ordering: true }); //Set ScrollY 75% of window height
//This code below overrides the initial sizing junk code that DataTable.net is doing so we can decide the widths!
$('#MyTable').css("width", "60vw"); //This resets the DataTable.net squirelly widths that are very hard to override!
$("div.dataTables_scrollHeadInner table.dataTable").css("width", "60vw");
$('#MyTable').DataTable().columns.adjust();
$('#MyTable').css("margin", "0");
//Resize the datatable to fix header widths to be the same size as the body column widths. Whenever the browser window is resized it adjusts the column in the header to match the columns in the body of the table
window.addEventListener('resize', function(event) {
$($.fn.dataTable.tables(true)).DataTable().columns.adjust();
$($.fn.dataTable.tables(true)).css("margin", "0");
/*$($.fn.dataTable.tables(true)).css("width", "60vw");*/
}, true);
What this code does, which is different in some ways, is that it also handles the problem where DataTable Header is split out by DataTable.net. - If the browser is resized the Header doesn't get resized up without the addEventListener. But I added code to adjust the Header Width right after the DataTable was setup. The "table-layout: fixed;" in the CSS for the table is important too, so that you have control over the resizing. And the "width: 65vw;" in the css is important for me so that the table doesn't take up too much space. This helpful if auto sizing fields of the table - if the table is too big, if not table-layout: fixed, the browser would try to add more space to columns to make the width of the table a certain size.
max-widthproperty set. Apparent IE8 doesn't like that property too much and ignored it in relation to datatables, even though the image was sized correclty on the screen. Changing it towidthfixed the problem.