|
| 1 | +--- |
| 2 | +title: Set Width to Predefined Telerik Dialogs |
| 3 | +description: Learn how to set width to predefined Telerik Dialogs such as the alert, confirm, and prompt modal popups. The width can be fixed or a percentage of the browser window's viewport. |
| 4 | +type: how-to |
| 5 | +page_title: How to Set Width to Predefined Telerik Dialogs |
| 6 | +slug: dialog-kb-dialogfactory-alert-confirm-prompt-width |
| 7 | +tags: telerik, blazor, dialog, css, styling |
| 8 | +ticketid: 1667334 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Dialog for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +This KB answers the following questions: |
| 26 | + |
| 27 | +* How to specify a `width` style to the predefined Telerik Blazor Dialogs, such as `AlertAsync()`? |
| 28 | +* How to apply dimensions to modal popups that are used through the Telerik `DialogFactory` cascading parameter? |
| 29 | +* How to define a width of predefined dialogs that are a percentage of the browser window viewport? |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +All [Telerik Blazor Dialogs]({%slug dialog-overview%}) render a `k-dialog` CSS class. In addition, the [predefined dialogs]({%slug dialog-predefined%}) render a `k-alert` CSS class. Use this distinguishing CSS class to target popups that are generated by the Telerik `DialogFactory` and apply CSS styles. |
| 34 | + |
| 35 | +>caption Set width to Telerik Blazor DialogFactory modal popups |
| 36 | +
|
| 37 | +````CSHTML |
| 38 | +<TelerikButton OnClick="@( () => DialogVisible = true )">Show Dialog</TelerikButton> |
| 39 | +
|
| 40 | +<TelerikButton OnClick="@ShowDialogFactoryPopup">Show DialogFactory Alert</TelerikButton> |
| 41 | +
|
| 42 | +<TelerikDialog @bind-Visible="@DialogVisible" |
| 43 | + Width="33vw" |
| 44 | + ButtonsLayout="@DialogButtonsLayout.End"> |
| 45 | + <DialogTitle> |
| 46 | + Dialog Component |
| 47 | + </DialogTitle> |
| 48 | + <DialogContent> |
| 49 | + <p>This Dialog is 33vw wide at all times.</p> |
| 50 | + </DialogContent> |
| 51 | + <DialogButtons> |
| 52 | + <TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary" |
| 53 | + OnClick="@( () => DialogVisible = false )">OK</TelerikButton> |
| 54 | + </DialogButtons> |
| 55 | +</TelerikDialog> |
| 56 | +
|
| 57 | +<style> |
| 58 | + /* This CSS rule will affect only DialogFactory popups */ |
| 59 | + .k-dialog.k-alert { |
| 60 | + min-width: 300px !important; /* overrides an inline style */ |
| 61 | + width: 33vw; |
| 62 | + max-width: 500px; |
| 63 | + } |
| 64 | +</style> |
| 65 | +
|
| 66 | +@code { |
| 67 | + [CascadingParameter] |
| 68 | + public DialogFactory? TelerikDialogs { get; set; } |
| 69 | +
|
| 70 | + private bool DialogVisible { get; set; } |
| 71 | +
|
| 72 | + private async Task ShowDialogFactoryPopup() |
| 73 | + { |
| 74 | + if (TelerikDialogs != null) |
| 75 | + { |
| 76 | + await TelerikDialogs.AlertAsync(@"This Dialog is 33vw wide, but with min/max restrictions. |
| 77 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ultricies congue dolor vel aliquet. |
| 78 | + Nunc ac enim neque. Suspendisse facilisis porta laoreet. |
| 79 | + Sed suscipit mauris lectus, ut porttitor velit porta vitae. |
| 80 | + Suspendisse potenti. Quisque auctor ac ante at egestas.", "Alert Dialog"); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | +```` |
| 85 | + |
| 86 | +## See Also |
| 87 | + |
| 88 | +* [Predefined Telerik Dialogs]({%slug dialog-predefined%}) |
| 89 | +* [Dialog Component Overview]({%slug dialog-overview%}) |
0 commit comments