1. Add a CSS File
Create a
.cssfile in your project (e.g.,Styles/styles.css).Set the Build Action to
MauiCss(right-click the file > Properties > Build Action).
2. Apply CSS to Controls
Option 1: Apply globally via
App.xaml<Application.Resources> <StyleSheet Source="Styles/styles.css" /> </Application.Resources>
Option 2: Apply to a specific page
<ContentPage.Resources> <StyleSheet Source="Styles/styles.css" /> </ContentPage.Resources>
3. Supported CSS Selectors & Properties
MAUI CSS supports a limited subset of CSS, including:
Selectors
By Type (
Button,Label, etc.)Button { background-color: #3498db; color: white; }
By Class (
.my-class).my-label { font-size: 18; font-weight: bold; }
Apply in XAML:
<Label Text="Hello MAUI!" StyleClass="my-label" />By ID (
#myButton)#myButton { background-color: red; }
Apply in XAML:
<Button x:Name="myButton" Text="Click Me" />
Supported Properties
| Category | Example Properties |
|---|---|
| Text | color, font-size, font-weight, font-style |
| Background | background-color |
| Border | border-color, border-width |
| Layout | margin, padding |
| Visibility | visibility (true/false) |
Example: Styling a Button
styles.css
Button { background-color: #3498db; color: white; font-size: 16; margin: 10; padding: 12; } Button:pressed { background-color: #2980b9; } .my-special-button { background-color: #e74c3c; border-radius: 20; }
MainPage.xaml
<Button Text="Click Me" /> <!-- Uses default Button style --> <Button Text="Special Button" StyleClass="my-special-button" />
Limitations of CSS in MAUI
❌ No full CSS support (only a subset of properties).
❌ No animations, transitions, or pseudo-classes (except
:pressed).❌ No Flexbox/Grid layouts (use MAUI's
Grid/FlexLayoutinstead).❌ No media queries (use
OnPlatformin XAML instead).
No comments:
Post a Comment