|
| 1 | +--- |
| 2 | +title: How to Customize the Default Menu Styles - Fonts and Background Colors |
| 3 | +description: Learn how to change the font color, weight, and background colors of the Menu component in a Telerik Blazor application to improve UI contrast. |
| 4 | +type: how-to |
| 5 | +page_title: How to Customize the Default Menu Styles - Fonts and Background Colors |
| 6 | +slug: menu-kb-custom-styling |
| 7 | +tags: menu, styling |
| 8 | +res_type: kb |
| 9 | +ticketid: 1657882, 1543208 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Menu for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +This KB article answers the following questions: |
| 26 | +- How do I change the font color and weight of the top-level menu items in a Blazor application? |
| 27 | +- How can I customize the background color of dropdown menus in a Blazor application? |
| 28 | +- What CSS selectors are needed to style the Telerik Menu in Blazor? |
| 29 | + |
| 30 | +## Solution |
| 31 | + |
| 32 | +To customize the appearance of the Menu items: |
| 33 | + |
| 34 | +1. Override the default theme styles. |
| 35 | +2. Use a custom CSS to change the font color and weight of the root menu items. |
| 36 | +3. Customize the background and font colors of the child menu items. |
| 37 | + |
| 38 | +>caption Menu component with custom CSS styles |
| 39 | +
|
| 40 | +````CSHTML |
| 41 | +<style> |
| 42 | + /* root menu item default state */ |
| 43 | + .custom-menu-styles.k-menu > .k-item { |
| 44 | + color: #fff; |
| 45 | + font-weight: bold; |
| 46 | + } |
| 47 | +
|
| 48 | + /* root menu item hover state */ |
| 49 | + .custom-menu-styles.k-menu > .k-item:hover { |
| 50 | + background: #fff; |
| 51 | + color: #000; |
| 52 | + } |
| 53 | +
|
| 54 | + /* child menu item default state */ |
| 55 | + .k-menu-group .k-item { |
| 56 | + background: #ff9; |
| 57 | + } |
| 58 | +
|
| 59 | + /* child menu item hover state */ |
| 60 | + .k-menu-group .k-item:hover { |
| 61 | + background: #ff3; |
| 62 | + } |
| 63 | +</style> |
| 64 | +
|
| 65 | +<div style="background: orange; padding:1em; width: 60%"> |
| 66 | + <TelerikMenu Data="@Data" Class="custom-menu-styles"></TelerikMenu> |
| 67 | +</div> |
| 68 | +
|
| 69 | +@code { |
| 70 | + public List<Product> Data { get; set; } |
| 71 | +
|
| 72 | + protected override void OnInitialized() |
| 73 | + { |
| 74 | + Data = new List<Product>(); |
| 75 | +
|
| 76 | + string[] mainMenuItems = { "Home", "About Us", "Services" }; |
| 77 | + string[][] subMenuItems = { |
| 78 | + new string[] { "Overview", "News", "Updates" }, |
| 79 | + new string[] { "Company", "Team", "Careers" }, |
| 80 | + new string[] { "Consulting", "Support", "Development" } |
| 81 | + }; |
| 82 | +
|
| 83 | + int idCounter = 1; |
| 84 | +
|
| 85 | + for (int i = 0; i < mainMenuItems.Length; i++) |
| 86 | + { |
| 87 | + var mainProduct = new Product |
| 88 | + { |
| 89 | + ID = idCounter++, |
| 90 | + Text = mainMenuItems[i], |
| 91 | + Items = new List<Product>() |
| 92 | + }; |
| 93 | +
|
| 94 | + foreach (var subItem in subMenuItems[i]) |
| 95 | + { |
| 96 | + mainProduct.Items.Add(new Product |
| 97 | + { |
| 98 | + ID = idCounter++, |
| 99 | + Text = subItem |
| 100 | + }); |
| 101 | + } |
| 102 | +
|
| 103 | + Data.Add(mainProduct); |
| 104 | + } |
| 105 | + } |
| 106 | +
|
| 107 | + public class Product |
| 108 | + { |
| 109 | + public int ID { get; set; } |
| 110 | + public string Text { get; set; } |
| 111 | + public List<Product> Items { get; set; } |
| 112 | + } |
| 113 | +} |
| 114 | +```` |
| 115 | + |
| 116 | +## See Also |
| 117 | + |
| 118 | +- [Telerik Blazor Menu - Overview]({%slug components/menu/overview%}) |
| 119 | +- [Telerik Blazor Styling and Themes - Override Theme Styles]({%slug themes-override%}) |
0 commit comments