|
| 1 | +--- |
| 2 | +title: Responsive Window |
| 3 | +description: How to adjust the size of a Window when the browser window size changes so that it is responsive |
| 4 | +type: how-to |
| 5 | +page_title: Responsive Window |
| 6 | +slug: window-kb-responsive |
| 7 | +position: |
| 8 | +tags: |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Window for Blazor</td> |
| 18 | + </tr> |
| 19 | + </tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +When the user resizes the browser window you may want to have the window resized with the new dimensions, so it is also responsive. |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +The following examples show how you can make responsive Window: |
| 32 | +* [Dimensions set in percent](#dimensions-set-in-percent) |
| 33 | +* [CSS media queries](#css-media-queries) |
| 34 | + |
| 35 | + |
| 36 | +### Dimensions set in percent |
| 37 | + |
| 38 | +Generally, the `Width` and `Height` parameters of the Window can take values in `%`, `vw` or `vh`, and the window will render according to the dimensions of its parent element (which is the `TelerikRootComponent` which should match the browser viewport). |
| 39 | + |
| 40 | +>caption Observe the behavior of a Window with set `Width` and `Height` in `%` |
| 41 | +
|
| 42 | +````CSHTML |
| 43 | +<TelerikWindow Modal="true" |
| 44 | + Visible="true" |
| 45 | + Width="40%" |
| 46 | + Height="40%"> |
| 47 | + <WindowTitle> |
| 48 | + <strong>The Title</strong> |
| 49 | + </WindowTitle> |
| 50 | + <WindowContent> |
| 51 | + I am modal so the page behind me is not available to the user. |
| 52 | + </WindowContent> |
| 53 | + <WindowActions> |
| 54 | + <WindowAction Name="Minimize" /> |
| 55 | + <WindowAction Name="Maximize" /> |
| 56 | + <WindowAction Name="Close" /> |
| 57 | + </WindowActions> |
| 58 | +</TelerikWindow> |
| 59 | +```` |
| 60 | + |
| 61 | + |
| 62 | +>note The `Width` and `Height` parameters render as inline CSS styles, meaning that they will have the highest priority. If you want to override them in a stylesheet (see below) you must use the `!important` statement. |
| 63 | +
|
| 64 | + |
| 65 | +### CSS media queries |
| 66 | + |
| 67 | +You can change the dimensions of the window based on the viewport size through media queries, not only through percentage units. |
| 68 | + |
| 69 | +If you want to use the CSS media queries, you have to create a separate CSS file under the `wwwroot` folder. This is required because the media queries start with `@` which conflicts with the Razor syntax. Technically, you could escape them as `@@`. |
| 70 | + |
| 71 | +>caption Observe the behavior of a Window made responsive with media queries |
| 72 | +
|
| 73 | +**Component:** |
| 74 | +````Component |
| 75 | +@* The Class parameter is set to make cascading the styles easier *@ |
| 76 | +
|
| 77 | +<TelerikWindow Modal="true" |
| 78 | + Visible="true" |
| 79 | + Class="myWindow"> |
| 80 | + <WindowTitle> |
| 81 | + <strong>The Title</strong> |
| 82 | + </WindowTitle> |
| 83 | + <WindowContent> |
| 84 | + I am modal so the page behind me is not available to the user. |
| 85 | + </WindowContent> |
| 86 | + <WindowActions> |
| 87 | + <WindowAction Name="Minimize" /> |
| 88 | + <WindowAction Name="Maximize" /> |
| 89 | + <WindowAction Name="Close" /> |
| 90 | + </WindowActions> |
| 91 | +</TelerikWindow> |
| 92 | +```` |
| 93 | +````Stylesheet |
| 94 | +@* The myWindow class used in the media queries is the same as in the Class parameter *@ |
| 95 | +@* Add the CSS file in the _Host.cshtml *@ |
| 96 | +
|
| 97 | +@media only screen and (min-width: 992px) { |
| 98 | + .myWindow{ |
| 99 | + max-width: 800px; |
| 100 | + } |
| 101 | +} |
| 102 | +
|
| 103 | +@media only screen and (min-width: 576px) and (max-width: 992px) { |
| 104 | + .myWindow { |
| 105 | + width: 500px; |
| 106 | + } |
| 107 | +} |
| 108 | +
|
| 109 | +@media only screen and (min-width: 300px) and (max-width: 576px) { |
| 110 | + .myWindow { |
| 111 | + width: 350px; |
| 112 | + } |
| 113 | +} |
| 114 | +```` |
| 115 | + |
| 116 | +## See also: |
| 117 | + |
| 118 | + * [Documentation Window for Blazor | Size]({%slug components/window/size%}) |
0 commit comments