# Responsive Columns

The Telerik UI for ASP.NET MVC Grid includes built-in options that show or hide columns based on the viewport width.

For a runnable example, refer to the [demo on Responsive Columns for the Grid component](https://demos.telerik.com/aspnet-mvc/grid/responsive-columns).

> important
>
> * The [`Hidden()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/gridboundcolumnbuilder#hiddensystemboolean) configuration method takes precedence over `Media()`.
> * The [`Media()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/gridboundcolumnbuilder#mediasystemstring) configuration method cannot be used together with [`MinScreenWidth()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/gridboundcolumnbuilder#minscreenwidthsystemint32).

## Configuration

Set the [`Media()`](https://docs.telerik.com/aspnet-mvc/api/kendo.mvc.ui.fluent/gridboundcolumnbuilder#mediasystemstring) configuration method of the column to a [supported value](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/columns/responsive-columns#accepted-values). When the media query matches, the column stays visible.

```Razor HtmlHelper
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.Customer>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(c => c.ContactName).Media("(min-width: 450px)");
                columns.Bound(c => c.ContactTitle).Width(250).Media("(min-width: 850px)");
                columns.Bound(c => c.CompanyName).Width(250).Media("(min-width: 850px)");
                columns.Bound(c => c.Country).Media("(min-width: 450px)");
            })
            .HtmlAttributes(new { style = "height: 550px;" })
            .Scrollable()
            .Groupable()
            .Sortable()
            .Pageable(pageable => pageable
                .Refresh(true)
                .PageSizes(true)
                .ButtonCount(5))
            .DataSource(dataSource => dataSource
                .Ajax()
                .Read(read => read.Action("Responsive_Columns_Customers_Read", "Grid"))
                .PageSize(20)
            )
    )
```

## Accepted Values

1. Use a valid media query string for the [`matchMedia browser API`](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia). The Grid toggles column visibility based on the query result.

2. Use one of the device identifiers available in [Bootstrap 5](https://getbootstrap.com/docs/5.0/layout/grid/#grid-options):

* **xs** is equivalent to "(max-width: 576px)"
* **sm** is equivalent to "(min-width: 576px)"
* **md** is equivalent to "(min-width: 768px)"
* **lg** is equivalent to "(min-width: 992px)"
* **xl** is equivalent to "(min-width: 1200px)"

## Column Template

Responsive Columns work with Column Templates to render multiple field values in a single compact layout.

The content in the `Template()` method is displayed when the `Media()` condition is met. For example, the following template column appears when the viewport is up to 450px wide.

```Razor HtmlHelper
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.Customer>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(c => c.ContactName).Media("(min-width: 450px)");
            columns.Bound(c => c.ContactTitle).Width(250).Media("(min-width: 850px)");
            columns.Bound(c => c.CompanyName).Width(250).Media("(min-width: 850px)");
            columns.Bound(c => c.Country).Media("(min-width: 450px)");
            columns.Template("#=resColTemplate(data)#").Title("Items").Media("(max-width: 450px)");
        })
        .HtmlAttributes(new { style = "height: 550px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("Responsive_Columns_Customers_Read", "Grid"))
            .PageSize(20)
        )
)
```

```javascript
    <script>
        var resColTemplate;

        $(document).ready( function () {
            // Extract the template content from script tag with id "responsive-column-template" and compile a template.
            resColTemplate = kendo.template($("#responsive-column-template").html()); 
        });
    </script>
```

```html
    <script id="responsive-column-template" type="text/x-kendo-template">
        <strong>Contact Name</strong>
        <p class="col-template-val">#=data.ContactName#</p>

        <strong>Contact Title</strong>
        <p class="col-template-val">#=data.ContactTitle#</p>

        <strong>Company Name</strong>
        <p class="col-template-val">#=data.CompanyName#</p>

        <strong>Country</strong>
        <p class="col-template-val">#=data.Country#</p>
    </script>
```

## See Also

* [Responsive Columns for the Grid HtmlHelper for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/grid/responsive-columns)
* [Server-Side API](https://www.telerik.com/aspnet-mvc/documentation/api/grid)
