I'm currently making a "Change Theme" button so that I can switch the whole page between "Dark Mode" and "Light Mode". But I can't find a way to change the background colour of the whole page.
Basically, I need to change the style of <body>
(It's the only way I can come up with).
My thought is, when you click "Change Theme" button, the <body>
element will be added a class "dark-theme".
And then I just simply define "dark-theme" in CSS like this:
body {
background-color: #FFFFFF;
color: #000000;
}
body.dark-theme {
background-color: #5A5A5A;
color: #F2F2F2;
}
I think this quite makes sense but as you can see below, I cannot access <body>
element so there is no way for me to add class or change CSS of it.
MainLayout.razor
@inherits LayoutComponentBase
@using System.Web;
<div class="sidebar">
<NavMenu />
</div>
<div>
<LoginDisplay />
<button id="change-theme-btn" @onclick="ThemeChanged">Change Theme</button>
@Body
</div>
@code {
bool isDark = false;
private void ThemeChanged()
{
isDark = !isDark;
}
}
So how do I change class or CSS of <body>
element? Or if you have any alternative solution, please tell me. Thank you!