I am currently working on a .net MAUI Blazor Hybrid app and I am looking to create both a dark and light theme using CSS stylesheets, without relying on MudBlazor.
My approach involves using two separate CSS files, lightMode.css
and darkMode.css
, each containing the appropriate color schemes for their respective themes. The goal is to have the MainLayout, and subsequently all pages, switch to using the darkMode.css
when dark mode is activated.
For example, within MainLayout.razor
:
<HeadContent>
@if (isDark)
{
<link href="css/darkMode.css" rel="stylesheet"/>
}
else
{
<link href="css/lightMode.css" rel="stylesheet"/>
}
</HeadContent>
// HTML and C# code here
However, I am facing an issue where the HeadContent is not being placed within the head tag of the page. Even when directly added to the index.html
page, only the last stylesheet mentioned seems to take effect.
I have searched for alternative solutions, but most do not mention how to achieve this using CSS, unlike the approach I am trying to implement. References like the following post have not provided the specific guidance I need: Light and dark theme for my Maui Blazor app
Any advice on how I can successfully implement a dark/light theme switch using CSS? I am open to other ideas as well, though this method seems the most straightforward to me.
Thank you in advance for your help.