Though Cascading Style Sheets are often used to style webpages, we can now use CSS to theme our .NET MAUI mobile and desktop apps. Let’s learn how!
Applying styles to our applications helps us save code by allowing us to reuse different properties in one or more than one visual element without the need to repeat it. It also allows us to better maintain the visual identity (or branding) of our apps. In .NET MAUI, we can apply these styles with XAML, and also with CSS!
In this article we will go through all the necessary steps for you to learn how to add styles with CSS to your .NET MAUI applications!
Before going into the material, let’s refresh with some important points.
Cascading Style Sheets (CSS) allow you to define multiple visual properties of interface elements in a reusable way. CSS is a language used for the composition and structuring of webpages. It is a list of rules to define the styles of your application, generally used for web applications, but today we will learn how to integrate CSS into mobile and desktop applications with .NET MAUI.
The explanation of this article will be based on the following topic structure:
Let’s see each one of them in detail.
The first thing you should have at hand is your .NET MAUI application (you can create a new one or use an existing one). Then apply the following instructions:
Go to Resources β‘ Styles folder and inside this one, create your .css file. (For this example, my file will be called StyleCSS.css.)
π You can add it to any other folder. However, to maintain a better organization and structure, Resources β‘ Styles is recommended.
^contentpage {
background-color: lightpink;
}
stacklayout {
-maui-spacing: 6;
margin: 40;
}
grid {
row-gap: 6;
column-gap: 6;
}
stacklayout>image {
height: 200;
width: 200;
}
Go to Resources β‘ Styles β‘ Your CSS File-β‘ Right Click β‘ Build Action β‘ MauiCSS. As you will see below:
β The CSS file will fail to load if its Build Action is not set to MauiCss.
Once you have done the previous step, now we are going to load this CSS sheet in our app as follows:
Let’s add the StyleSheet class which allows us to load and parse the style sheet. Go to your SamplePage.xaml file and inside the <ContentPage.Resources>
tags, add the following:
<ContentPage.Resources>
<StyleSheet Source="Resources/Styles/StyleCSS.css" />
</ContentPage.Resources>
π Remember that “StyleCSS.css” is the name of my CSS file sample. If yours has another name, you must replace it.
Another option to do it is adding a CDATA section:
<ContentPage ...>
<ContentPage.Resources>
<StyleSheet>
<![CDATA[
^contentpage {
background-color: lightpink;
}
]]>
</StyleSheet>
</ContentPage.Resources>
...
</ContentPage>
A CSS document can be loaded from a StringReader to later be added to a ResourceDictionary:
using Microsoft.Maui.Controls.StyleSheets;
public partial class MyPage : ContentPage
{
public MyPage()
{
InitializeComponent();
using (var reader = new StringReader("^contentpage { background-color: lightpink; }"))
{
this.Resources.Add(StyleSheet.FromReader(reader));
}
}
}
Child style sheets override parent style sheets if the same properties are set.
Therefore, for the occasions in which properties are repeated, the following precedence rules are followed:
Selectors are responsible for determining the element to which the style will be applied. To be able to apply the styles, it’s a priority to know the selectors in CSS. You can use the same selectors as CSS in .NET MAUI!
There are different selector types. Below we will see their definitions and examples of each one:
These allow you to indicate the name or the tag to which the CSS properties will be assigned.
Let’s see an example:
This example selects all elements of type StackLayout and sets the Margin property to 50 as its value.
β The element selector does not identify subclasses of the specified type.
These allow you to indicate the elements in the visual tree that can be selected by base class with ^base selector.
Let’s see an example:
This example is selecting all elements with ContentPage as the base class (including ContentPage itself) and sets it to a Green background color.
Important to keep in mind:
^base
selector is specific to .NET MAUI and is not part of the CSS specification.These allow you to select an element by its ID or name. Identifies the element whose StyleId property is set to an element. In case the StyleId property is not established, it will take the data from the x:Name
property of the element.
Let’s see an example:
Imagine you already have a button named SendButton.
<Button x:Name="SendButton"
Text="Send"
Background="Pink"/>
Now, let’s continue with the CSS styling:
A blue background was added to elements with the id #SendButton
.
These allow you to select an element by class name. A CSS class can be assigned to a XAML element by setting the element’s StyleClass property to the name of the CSS class.
Let’s see an example:
Imagine you already have two Buttons with some assigned classes:
<Button StyleClass="resumenName" />
<Button StyleClass="resumenLastName" />
Now, let’s continue with the CSS styling:
Here we apply different properties to the resumeName and resumeLastName classes which are contained in two buttons.
These allow selecting the child elements in the visual tree.
Let’s see an example:
Imagine you already have an image inside a StackLayout:
<StackLayout>
<Image Source="dotnet_bot"/
</StackLayout>
Now, let’s continue with the CSS styling:
The example sets the height and width of the images within the StackLayouts.
The selected element does not have to be direct—it can be secondary. It may happen that the element is an ancestor.
In case you only want to refer to the direct element, you can do it using the “>” sign, as follows:
π Selectors can be combined without limitation. For example:
StackLayout>ContentView>label.email.
The following CSS properties are supported by .NET MAUI:
π§ Property | π Applies to |
---|---|
| FlexLayout |
| VisualElement |
| Page |
| Button, Frame, ImageButton |
| BoxView, Button, Frame, ImageButton |
| Button, ImageButton |
| ActivityIndicator, BoxView, Button, CheckBox, DatePicker, Editor, Entry, Label, Picker, ProgressBar, SearchBar, Switch, TimePicker |
| Grid |
| Button, DatePicker, Editor, Entry, Label, Picker, SearchBar, TimePicker, Span |
| Button, DatePicker, Editor, Entry, Label, Picker, SearchBar, SearchHandler, Span, TimePicker |
| Label, Span |
| View |
| Label |
| Button, ImageButton, Layout, Page |
| Entry, EntryCell, Label, SearchBar |
The following .NET MAUI specific CSS properties are also supported:
π§ Property | π Applies to |
---|---|
| NavigationPage, TabbedPage |
| ScrollView |
| Entry, Editor, SearchBar |
| Slider |
| ScrollView, StackLayout |
| StackLayout |
| Slider, Switch |
| Label |
| VisualElement |
The following .NET MAUI Shell specific CSS properties are also supported:
π§ Property | π Applies to |
---|---|
| Shell |
| Element |
And done! π In this article you have learned all the necessary information so that you can start adding styling to your .NET MAUI applications with CSS!
Thanks for reading this article! ππ
See you next time! πβοΈ
References: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/styles/css?view=net-maui-7.0
Use a component library in sync with .NET MAUI’s release cadence. Try Telerik UI for .NET MAUI for free.
Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! ππ You can follow her: Twitter, LinkedIn , AskXammy and Medium.