|  | 
| 1 | 1 | --- | 
| 2 | 2 | layout: post | 
| 3 |  | -title: Override Default Styles in .NET MAUI MarkdownViewer | Syncfusion | 
|  | 3 | +title: Apply CSS Style Rules in .NET MAUI MarkdownViewer | Syncfusion | 
| 4 | 4 | description: Learn how to override the default appearance of Markdown content using custom CSS in the Syncfusion .NET MAUI MarkdownViewer control. | 
| 5 | 5 | platform: MAUI | 
| 6 | 6 | control: SfMarkdownViewer | 
| 7 | 7 | documentation: ug | 
| 8 | 8 | --- | 
| 9 | 9 | 
 | 
| 10 |  | -# How to Override Default Styles in .NET MAUI SfMarkdownViewer | 
|  | 10 | +# How to Apply CSS Style Rules in .NET MAUI SfMarkdownViewer | 
| 11 | 11 | 
 | 
| 12 |  | -The [SfMarkdownViewer]() control in .NET MAUI comes with a set of built-in default styles that ensure Markdown content is rendered cleanly and consistently across platforms. However, in many real-world applications, developers may need to override these defaults to match their app’s branding, improve readability, or create a custom visual experience. | 
|  | 12 | +The `SfMarkdownViewer` control in .NET MAUI comes with a set of built-in default styles that ensure Markdown content is rendered cleanly and consistently across platforms. However, in many real-world applications, developers may need to override these defaults to match their app’s branding, improve readability, or create a custom visual experience. | 
| 13 | 13 | 
 | 
| 14 |  | -This guide explains how to override the default styles using the [MarkdownStyleSettings]() class and the [CssStyleRules]() property. | 
|  | 14 | +This guide explains how to override the default styles using the `MarkdownStyleSettings` class and the `CssStyleRules` property. | 
| 15 | 15 | 
 | 
| 16 |  | -### Defining Styles | 
|  | 16 | +## Defining Styles | 
| 17 | 17 | 
 | 
| 18 | 18 | **XAML** | 
| 19 | 19 | 
 | 
| 20 | 20 | {% highlight xaml %} | 
| 21 | 21 | 
 | 
| 22 |  | -    <ContentPage.Resources> | 
| 23 |  | -        <ResourceDictionary> | 
| 24 |  | -            <x:String x:Key="CustomStyle"> | 
| 25 |  | -            body { | 
| 26 |  | -                background: #F5F7FA; | 
| 27 |  | -                font-family: 'Segoe UI', sans-serif; | 
| 28 |  | -                font-size: 16px; | 
| 29 |  | -                color: #2E2E2E; | 
| 30 |  | -                line-height: 1.7; | 
| 31 |  | -            } | 
| 32 |  | - | 
| 33 |  | -            h1 { | 
| 34 |  | -                font-weight: 700; | 
| 35 |  | -                font-size: 30px; | 
| 36 |  | -                line-height: 38px; | 
| 37 |  | -                letter-spacing: 0.5px; | 
| 38 |  | -                color: #1E3A8A; | 
| 39 |  | -                margin-bottom: 16px; | 
| 40 |  | -            } | 
| 41 |  | - | 
| 42 |  | -            h2 { | 
| 43 |  | -                font-weight: 600; | 
| 44 |  | -                font-size: 24px; | 
| 45 |  | -                line-height: 32px; | 
| 46 |  | -                letter-spacing: 0.4px; | 
| 47 |  | -                color: #3B5BAA; | 
| 48 |  | -                margin-top: 24px; | 
| 49 |  | -                margin-bottom: 12px; | 
| 50 |  | -            } | 
| 51 |  | - | 
| 52 |  | -            h3 { | 
| 53 |  | -                font-weight: 500; | 
| 54 |  | -                font-size: 20px; | 
| 55 |  | -                line-height: 28px; | 
| 56 |  | -                letter-spacing: 0.3px; | 
| 57 |  | -                color: #6C83C1; | 
| 58 |  | -                margin-top: 20px; | 
| 59 |  | -                margin-bottom: 10px; | 
| 60 |  | -            } | 
| 61 |  | -            </x:String> | 
| 62 |  | -        </ResourceDictionary> | 
| 63 |  | -    </ContentPage.Resources> | 
|  | 22 | +<ContentPage.Resources> | 
|  | 23 | +    <ResourceDictionary> | 
|  | 24 | +        <x:String x:Key="CustomStyle"> | 
|  | 25 | +        body { | 
|  | 26 | +            background: #F5F7FA; | 
|  | 27 | +            font-family: 'Segoe UI', sans-serif; | 
|  | 28 | +            font-size: 16px; | 
|  | 29 | +            color: #2E2E2E; | 
|  | 30 | +            line-height: 1.7; | 
|  | 31 | +        } | 
|  | 32 | + | 
|  | 33 | +        h1 { | 
|  | 34 | +            font-weight: 700; | 
|  | 35 | +            font-size: 30px; | 
|  | 36 | +            line-height: 38px; | 
|  | 37 | +            letter-spacing: 0.5px; | 
|  | 38 | +            color: #1E3A8A; | 
|  | 39 | +            margin-bottom: 16px; | 
|  | 40 | +        } | 
|  | 41 | + | 
|  | 42 | +        h2 { | 
|  | 43 | +            font-weight: 600; | 
|  | 44 | +            font-size: 24px; | 
|  | 45 | +            line-height: 32px; | 
|  | 46 | +            letter-spacing: 0.4px; | 
|  | 47 | +            color: #3B5BAA; | 
|  | 48 | +            margin-top: 24px; | 
|  | 49 | +            margin-bottom: 12px; | 
|  | 50 | +        } | 
|  | 51 | + | 
|  | 52 | +        h3 { | 
|  | 53 | +            font-weight: 500; | 
|  | 54 | +            font-size: 20px; | 
|  | 55 | +            line-height: 28px; | 
|  | 56 | +            letter-spacing: 0.3px; | 
|  | 57 | +            color: #6C83C1; | 
|  | 58 | +            margin-top: 20px; | 
|  | 59 | +            margin-bottom: 10px; | 
|  | 60 | +        } | 
|  | 61 | +        </x:String> | 
|  | 62 | +    </ResourceDictionary> | 
|  | 63 | +</ContentPage.Resources> | 
| 64 | 64 | 
 | 
| 65 | 65 | {% endhighlight %} | 
| 66 | 66 | 
 | 
| 67 | 67 | Apply the defined CSS style to the Markdown Viewer using the `CssStyleRules` property, as shown in the XAML code below: | 
| 68 | 68 | 
 | 
| 69 | 69 | {% highlight xaml %} | 
| 70 | 70 | 
 | 
| 71 |  | -    <ContentPage | 
| 72 |  | -        . . .     | 
| 73 |  | -        xmlns:markdown="clr-namespace:Syncfusion.Maui.MarkdownViewer; | 
| 74 |  | -        assembly=Syncfusion.Maui.MarkdownViewer"> | 
| 75 |  | -        . . . | 
| 76 |  | -        <markdown:SfMarkdownViewer Source={Binding MarkdownContent}> | 
| 77 |  | -            <markdown:SfMarkdownViewer.Settings> | 
| 78 |  | -                <markdown:MarkdownStyleSettings CssStyleRules="{StaticResource CustomStyle}" /> | 
| 79 |  | -            </markdown:SfMarkdownViewer.Settings> | 
| 80 |  | -        </markdown:SfMarkdownViewer> | 
|  | 71 | +<ContentPage | 
|  | 72 | +    . . .     | 
|  | 73 | +    xmlns:markdown="clr-namespace:Syncfusion.Maui.MarkdownViewer; | 
|  | 74 | +    assembly=Syncfusion.Maui.MarkdownViewer"> | 
|  | 75 | +    . . . | 
|  | 76 | +    <markdown:SfMarkdownViewer Source={Binding MarkdownContent}> | 
|  | 77 | +        <markdown:SfMarkdownViewer.Settings> | 
|  | 78 | +            <markdown:MarkdownStyleSettings CssStyleRules="{StaticResource CustomStyle}" /> | 
|  | 79 | +        </markdown:SfMarkdownViewer.Settings> | 
|  | 80 | +    </markdown:SfMarkdownViewer> | 
| 81 | 81 | 
 | 
| 82 |  | -    </ContentPage> | 
|  | 82 | +</ContentPage> | 
| 83 | 83 | 
 | 
| 84 | 84 | {% endhighlight %} | 
| 85 | 85 | 
 | 
| 86 | 86 | The output below reflects the styled appearance of the Markdown content. | 
| 87 | 87 | 
 | 
| 88 |  | -. | 
|  | 88 | + | 
| 89 | 89 | 
 | 
| 90 | 90 | By defining custom styles, you can transform the Markdown Viewer into a fully branded and polished content surface that fits seamlessly into your app’s design system. | 
| 91 | 91 | 
 | 
| 92 |  | -N> Apply CSS styles only when necessary as they will override the properties of individual Markdown elements such as `H1FontSize`, `H1Color`, `BodyTextColor`, `TableHeaderFontSize`, etc., defined in the [MarkdownStyleSettings]() class. | 
|  | 92 | +N> Apply CSS styles only when necessary as they will override the properties of individual Markdown elements such as `H1FontSize`, `H1Color`, `BodyTextColor`, `TableHeaderFontSize`, etc., defined in the `MarkdownStyleSettings` class. | 
|  | 93 | + | 
|  | 94 | +## Define Styles for Images | 
|  | 95 | + | 
|  | 96 | +Images are a vital part of Markdown content, providing visual context and detail. SfMarkdownViewer allows you to override the appearance of images through CSS, enabling custom sizing, borders, or even special effects. For example: | 
|  | 97 | + | 
|  | 98 | +{% highlight xaml %} | 
|  | 99 | + | 
|  | 100 | +<x:String x:Key="CustomStyle"> | 
|  | 101 | +img { | 
|  | 102 | +    border-radius: 12px; | 
|  | 103 | +    border: 8px solid #e0e3ea; | 
|  | 104 | +    max-width: 95%; | 
|  | 105 | +    box-shadow: 0 4px 16px rgba(31,45,61,0.75); | 
|  | 106 | +    margin: 16px 16px; | 
|  | 107 | +} | 
|  | 108 | +</x:String> | 
|  | 109 | + | 
|  | 110 | +{% endhighlight %} | 
|  | 111 | + | 
|  | 112 | +This CSS makes all images inside the Markdown content appear with rounded corners, a subtle border, and a shadow that helps them blend elegantly with modern app designs. Adjust the `margin` or `max-width` to fit the layout of your application. | 
|  | 113 | + | 
|  | 114 | + | 
|  | 115 | + | 
|  | 116 | +## Define Styles for Tables | 
|  | 117 | + | 
|  | 118 | +Tables are commonly used to present data and comparisons. While basic table styling (such as header font size, text color, and table background) can be easily set using properties of the `MarkdownStyleSettings` class, see the detailed explanation in the [Customize Appearance](https://help.syncfusion.com/maui/markdownviewer/appearance) documentation. You can take your table designs further by using custom CSS through the `CssStyleRules` property. | 
|  | 119 | + | 
|  | 120 | +This enables effects such as striped rows, rounded corners, advanced padding, and borders for maximum brand consistency. For example: | 
|  | 121 | + | 
|  | 122 | +{% highlight xaml %} | 
|  | 123 | + | 
|  | 124 | +<x:String x:Key="CustomStyle"> | 
|  | 125 | +table { | 
|  | 126 | +    background: #FAFBFC; | 
|  | 127 | +    border-collapse: collapse; | 
|  | 128 | +    margin: 16px 0; | 
|  | 129 | +    width: 100%; | 
|  | 130 | +    border-radius: 6px; | 
|  | 131 | +    overflow: hidden; | 
|  | 132 | +} | 
|  | 133 | +th, td { | 
|  | 134 | +    border: 1px solid #E0E4EA; | 
|  | 135 | +    padding: 10px 16px; | 
|  | 136 | +    text-align: left; | 
|  | 137 | +} | 
|  | 138 | +th { | 
|  | 139 | +    background: #EDF2FB; | 
|  | 140 | +    color: #193466; | 
|  | 141 | +    font-size: 17px; | 
|  | 142 | +    font-weight: 600; | 
|  | 143 | +} | 
|  | 144 | +tr:nth-child(even) { | 
|  | 145 | +    background: #F5F7FB; | 
|  | 146 | +} | 
|  | 147 | +</x:String> | 
|  | 148 | + | 
|  | 149 | +{% endhighlight %} | 
|  | 150 | + | 
|  | 151 | +CSS styles provide much finer control over table appearance than the standard style properties. For thorough details on property based styling including `TableHeaderFontSize`, `TableHeaderTextColor`, and more, please refer to the [Customize Appearance](https://help.syncfusion.com/maui/markdownviewer/appearance) documentation page. When both CSS and style properties are specified, CSS will take precedence. | 
|  | 152 | + | 
|  | 153 | + | 
|  | 154 | + | 
|  | 155 | +## Define Styles for ScrollBar | 
|  | 156 | + | 
|  | 157 | +Scrollbars are crucial for navigating through long Markdown documents, especially on desktop platforms or when embedding the control in a constrained space. SfMarkdownViewer enables you to customize scrollbar appearance and behavior via CSS: | 
|  | 158 | + | 
|  | 159 | +- **Scrollbar Visibility:** You can show or hide the scrollbar by targeting the `::-webkit-scrollbar` selector (on applicable platforms). | 
|  | 160 | +- **Scrollbar Color:** Make the scrollbar match your branding or fade it as needed. | 
|  | 161 | + | 
|  | 162 | +Example CSS for a modern, visible scrollbar: | 
|  | 163 | + | 
|  | 164 | +{% highlight xaml %} | 
|  | 165 | + | 
|  | 166 | +<x:String x:Key="CustomStyle"> | 
|  | 167 | +::-webkit-scrollbar { | 
|  | 168 | +    width: 10px; | 
|  | 169 | +    background: #eeeeee; | 
|  | 170 | +} | 
|  | 171 | +::-webkit-scrollbar-thumb { | 
|  | 172 | +    background: #3B82F6; | 
|  | 173 | +    border-radius: 8px; | 
|  | 174 | +    border: 2px solid #1E293B; | 
|  | 175 | +} | 
|  | 176 | +::-webkit-scrollbar-thumb:hover { | 
|  | 177 | +    background: #2563EB; | 
|  | 178 | +} | 
|  | 179 | +</x:String> | 
|  | 180 | + | 
|  | 181 | +{% endhighlight %} | 
|  | 182 | + | 
|  | 183 | + | 
|  | 184 | + | 
|  | 185 | +You can further hide the scrollbar by setting `display: none;` in the `::-webkit-scrollbar` selector or by setting its width to 0, or match its color and thickness to seamlessly blend with your application's UI. Custom scrollbar styling will be effective on supported platforms (typically Windows and Android). Always verify cross-platform appearance for best results. | 
|  | 186 | + | 
|  | 187 | +With tailored scrollbar, table, and image styles, the SfMarkdownViewer control delivers beautiful, readable, and fully brand-aligned Markdown content in your .NET MAUI applications. | 
0 commit comments