MVC 4 Webgrid - Column Width settings using css style is not working

Thiru picture Thiru · Jul 10, 2014 · Viewed 8.7k times · Source

I am trying to set width for Webgrid column. But it is not working. Anyone help me please.

Please find below my code

@grid.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
alternatingRowStyle: "webgrid-alternating-row",
mode: WebGridPagerModes.All,
columns:grid.Columns(
grid.Column(columnName:"ID", header: "ID",canSort:true, style: "id"),
grid.Column(columnName:"Name", header: "Name",canSort:true,style: "name")
)

And my css file

.webgrid-table { border: 1px solid #98BF21;   }
.webgrid-header { background-color: #A7C942; color: #FFFFFF; text-align: left;}
.webgrid-alternating-row { background-color: #EAF2D3; }
.id { width: 20px; }
.name { width: 40px; }

Anyone help me please?

Answer

user2217303 picture user2217303 · Feb 18, 2015

You need to specify the width of the WebGrid itself in order to solve this issue.

Add this

.webgrid-table { *width*: *specify the width value here* ;border: 1px solid #98BF21;}