|
| 1 | +--- |
| 2 | +title: Responsive Form |
| 3 | +page_title: Responsive Form |
| 4 | +description: "Get started with the Telerik UI Form component for {{ site.framework }} and learn how to build responsive forms using the grid layout configuration." |
| 5 | +slug: htmlhelpers_form_aspnetcore_responsive |
| 6 | +position: 8 |
| 7 | +--- |
| 8 | + |
| 9 | +# Responsive Form |
| 10 | + |
| 11 | +The Form component in {{ site.framework }} provides responsive layouts using a flexible grid structure. The responsive layout automatically adapts to various screen sizes, ensuring optimal usability of the Form across desktop, tablet, and mobile devices. |
| 12 | + |
| 13 | +To enable responsive behavior, set the `Layout()` option to `"grid"` and configure the desired breakpoints through the `Cols()` configuration. |
| 14 | + |
| 15 | +You can control the width and arrangement of form items and groups based on screen width by using the `ColSpan()` method, which dynamically adjusts the layout as the viewport changes. |
| 16 | + |
| 17 | +## Breakpoint Configuration |
| 18 | + |
| 19 | +Define responsive breakpoints through the `Cols()` configuration inside the `Grid()` method. Each breakpoint includes a `MinWidth`, `MaxWidth`, and a `value` that represents the number of columns in the grid. |
| 20 | + |
| 21 | +```HtmlHelper |
| 22 | +.Grid(g => g.Cols(c => |
| 23 | +{ |
| 24 | + c.Add().MaxWidth(500).Value(3); |
| 25 | + c.Add().MinWidth(501).MaxWidth(768).Value(6); |
| 26 | + c.Add().MinWidth(769).Value(12); |
| 27 | +})) |
| 28 | +``` |
| 29 | + |
| 30 | +{% if site.core %} |
| 31 | +```TagHelper |
| 32 | +<cols> |
| 33 | + <col max-width="500" value="3" /> |
| 34 | + <col min-width="501" max-width="768" value="6" /> |
| 35 | + <col min-width="769" value="12" /> |
| 36 | +</cols> |
| 37 | +``` |
| 38 | +{% endif %} |
| 39 | + |
| 40 | +By default, three breakpoints define the Form columns as follows: |
| 41 | + |
| 42 | +|| **Small** | **Medium** | **Large** | |
| 43 | +|-------|-------|--------|-------| |
| 44 | +**Dimensions** | up to 500px | 501px to 768px | over 768px | |
| 45 | +**Columns** | 3 | 6 | 12 | |
| 46 | + |
| 47 | +The following example shows hot to set a fixed ColSpan for a field. |
| 48 | + |
| 49 | +```HtmlHelper |
| 50 | +i.Add().Field(f => f.Email) |
| 51 | + .Label("Email") |
| 52 | + .ColSpan(3) |
| 53 | + .Editor(e => e.TextBox()); |
| 54 | +``` |
| 55 | + |
| 56 | +{% if site.core %} |
| 57 | +```TagHelper |
| 58 | +<form-item field="Email" col-span="3"> |
| 59 | + <item-label text="Email" /> |
| 60 | + <textbox-editor /> |
| 61 | +</form-item> |
| 62 | +``` |
| 63 | +{% endif %} |
| 64 | + |
| 65 | +## ColSpan on Groups |
| 66 | + |
| 67 | +You can apply `ColSpan()` option to Form [groups](slug:htmlhelpers_form_aspnetcore_groups) to define their width in the parent layout. |
| 68 | + |
| 69 | +```HtmlHelper |
| 70 | +items.AddGroup() |
| 71 | + .Label("Shipping Address") |
| 72 | + .Layout("grid") |
| 73 | + .ColSpan(6) |
| 74 | + .Items(i => |
| 75 | + { |
| 76 | + i.Add().Field(f => f.Country) |
| 77 | + .Label("Country") |
| 78 | + .ColSpan(3); |
| 79 | + i.Add().Field(f => f.City) |
| 80 | + .Label("City") |
| 81 | + .ColSpan(3); |
| 82 | + }); |
| 83 | +``` |
| 84 | + |
| 85 | +{% if site.core %} |
| 86 | +```TagHelper |
| 87 | +<form-item type="group" label="Personal Info" layout="grid" col-span="6"> |
| 88 | + <form-items> |
| 89 | + <form-item field="FullName" col-span="3"> |
| 90 | + <item-label text="Full name" /> |
| 91 | + <textbox-editor /> |
| 92 | + </form-item> |
| 93 | + <form-item field="Email" col-span="3"> |
| 94 | + <item-label text="Email" /> |
| 95 | + <textbox-editor /> |
| 96 | + </form-item> |
| 97 | + </form-items> |
| 98 | +</form-item> |
| 99 | +``` |
| 100 | +{% endif %} |
| 101 | + |
| 102 | +The example below demonstrates a responsive Form with multiple groups. |
| 103 | + |
| 104 | +```HtmlHelper |
| 105 | +
|
| 106 | +@(Html.Kendo().Form<Kendo.Mvc.Examples.Models.Form.ResponsiveFormViewModel>() |
| 107 | + .Name("responsiveForm") |
| 108 | + .HtmlAttributes(new { method = "POST" }) |
| 109 | + .Layout("grid") |
| 110 | + .ClearButton(false) |
| 111 | + .Grid(g => g.Cols(c => |
| 112 | + { |
| 113 | + c.Add().MaxWidth(500).Value(3); |
| 114 | + c.Add().MinWidth(501).MaxWidth(768).Value(6); |
| 115 | + c.Add().MinWidth(769).Value(12); |
| 116 | + })) |
| 117 | + .Validatable(v => |
| 118 | + { |
| 119 | + v.ValidateOnBlur(true); |
| 120 | + v.ValidationSummary(vs => vs.Enable(false)); |
| 121 | + }) |
| 122 | + .Items(items => |
| 123 | + { |
| 124 | + items.AddGroup() |
| 125 | + .Label("Personal Info") |
| 126 | + .Layout("grid") |
| 127 | + .ColSpan(6) |
| 128 | + .Items(i => |
| 129 | + { |
| 130 | + i.Add().Field(f => f.FullName) |
| 131 | + .Label("Full name") |
| 132 | + .ColSpan(3) |
| 133 | + .Editor(e => e.TextBox()); |
| 134 | +
|
| 135 | + i.Add().Field(f => f.Email) |
| 136 | + .Label("Email") |
| 137 | + .ColSpan(3) |
| 138 | + .Editor(e => e.TextBox()); |
| 139 | + }); |
| 140 | +
|
| 141 | + items.AddGroup() |
| 142 | + .Label("Address") |
| 143 | + .Layout("grid") |
| 144 | + .ColSpan(6) |
| 145 | + .Items(i => |
| 146 | + { |
| 147 | + i.Add().Field(f => f.Country) |
| 148 | + .Label("Country") |
| 149 | + .ColSpan(3) |
| 150 | + .Editor(e => e.ComboBox() |
| 151 | + .Placeholder("EU Country") |
| 152 | + .BindTo(new[] { "France", "Germany", "Italy", "Spain", "Bulgaria" })); |
| 153 | +
|
| 154 | + i.Add().Field(f => f.City) |
| 155 | + .Label("City") |
| 156 | + .ColSpan(3) |
| 157 | + .Editor(e => e.TextBox()); |
| 158 | + }); |
| 159 | + })) |
| 160 | +``` |
| 161 | + |
| 162 | +{% if site.core %} |
| 163 | +```TagHelper |
| 164 | +
|
| 165 | +@addTagHelper *, Kendo.Mvc |
| 166 | +@model Kendo.Mvc.Examples.Models.Form.ResponsiveFormViewModel |
| 167 | +
|
| 168 | +<kendo-form name="responsiveForm" layout="grid" clear-button="false"> |
| 169 | + <grid> |
| 170 | + <cols> |
| 171 | + <col max-width="500" value="3" /> |
| 172 | + <col min-width="501" max-width="768" value="6" /> |
| 173 | + <col min-width="769" value="12" /> |
| 174 | + </cols> |
| 175 | + </grid> |
| 176 | + <validatable validate-on-blur="true"> |
| 177 | + <validation-summary enable="false" /> |
| 178 | + </validatable> |
| 179 | + <form-items> |
| 180 | + <form-item type="group" label="Personal Info" layout="grid" col-span="6"> |
| 181 | + <form-items> |
| 182 | + <form-item field="FullName" col-span="3"> |
| 183 | + <item-label text="Full name" /> |
| 184 | + <textbox-editor /> |
| 185 | + </form-item> |
| 186 | + <form-item field="Email" col-span="3"> |
| 187 | + <item-label text="Email" /> |
| 188 | + <textbox-editor /> |
| 189 | + </form-item> |
| 190 | + </form-items> |
| 191 | + </form-item> |
| 192 | + <form-item type="group" label="Address" layout="grid" col-span="6"> |
| 193 | + <form-items> |
| 194 | + <form-item field="Country" col-span="3"> |
| 195 | + <item-label text="Country" /> |
| 196 | + <combobox-editor placeholder="EU Country" bind-to="@ViewBag.CountryData" /> |
| 197 | + </form-item> |
| 198 | + <form-item field="City" col-span="3"> |
| 199 | + <item-label text="City" /> |
| 200 | + <textbox-editor /> |
| 201 | + </form-item> |
| 202 | + </form-items> |
| 203 | + </form-item> |
| 204 | + </form-items> |
| 205 | +</kendo-form> |
| 206 | +``` |
| 207 | +{% endif %} |
| 208 | + |
| 209 | +## Best Practices |
| 210 | + |
| 211 | +When implementing column spanning in the forms, consider the following recommendations: |
| 212 | + |
| 213 | +- Start Mobile-First—Define responsive breakpoints starting from the smallest screen size and working up to larger viewports. |
| 214 | +- Balance ColSpans—Ensure that all items in a row do not exceed the total grid column width. |
| 215 | +- Use Groups—Group related fields to provide logical structure and improve UX. |
| 216 | +- Adjust for Content—Give more space to fields like addresses or longer inputs. |
| 217 | +- Test Across Devices—Verify that the responsive columns appear as expected across different screen sizes and orientations. |
| 218 | + |
| 219 | +## See Also |
| 220 | + |
| 221 | +* [Creating Responsive Form for {{ site.framework }} (Demo)](https://demos.telerik.com/{{ site.platform }}/form/responsive-form) |
| 222 | +* [Server-Side API of the Form](/api/form) |
| 223 | +{% if site.core %} |
| 224 | +* [Server-Side TagHelper API of the Form](/api/taghelpers/form) |
| 225 | +{% endif %} |
0 commit comments