Access color string from code behind in .css-File in ASP

I have 3 files for my Login-Screen: Login.aspx, Login.aspx.cs, LoginStyle.css

On the Page_Load method I am recieving some data from the current system among other things the theme-color as string (e.g #003b67).

In my .css-File i have defind 3 style rules, which should use this color.

.linkButtonPassword:hover {
        color: #38d39f;
}

.login-input-div.focus > .login-faItem > i {
    color: #38d39f;
}

.login-input-div:before, .login-input-div:after {
    content: '';
    position: absolute;
    bottom: -2px;
    width: 0%;
    height: 2px;
    background-color: #38d39f;
    transition: .4s;
}

so my question is, is there a way to replace the current color-string “#38d39f” with my color from code behind without defining the style in my .aspx-File? (is there a way to access a kind of variable in my css-file)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

CSS files aren’t processed by ASP.NET (unless you reconfigure your entire HTTP pipeline, which is a bad idea in general), so the short answer is “no”.

…however a solution exists with CSS Custom Properties!

Part 1:

In your .aspx page, ideally in the <head> (you’re using <asp:ContentPlaceHolder, I hope?), put this after your current <link rel="stlyesheet"> element:

<style type="text/css">
:root {
    --theme-color: <%= this.customColor %>;
}

.linkButtonPassword:hover,
.login-input-div.focus > .login-faItem > i,
.login-input-div:before,
.login-input-div:after {
    color: var(--theme-color);
}
</style>

You could argue this is the same thing as overwriting the properties with <%= %> directly – but the difference here is that you can now remove the color: property from the above and use color: var(--theme-color); directly in your .css file!

Part 2:

So now your <head> should contain this:

<style type="text/css">
:root {
    --theme-color: <%= this.customColor %>;
}
</style>

And your .css file should contain this:

.linkButtonPassword:hover {
    color: #38d39f;
    color: var(--theme-color);
}

.login-input-div.focus > .login-faItem > i {
    color: #38d39f;
    color: var(--theme-color);
}

.login-input-div:before,
.login-input-div:after {
    content: '';
    position: absolute;
    bottom: -2px;
    width: 0%;
    height: 2px;
    background-color: #38d39f;
    background-color: var(--theme-color);
    transition: .4s;
}

The color values are repeated twice (first as a literal hex-color, then again with var(--theme-color)) to ensure backwards compatibility with older browsers that don’t support CSS Custom Properties (i.e. IE11 – which I assume you have to support seeming as you’re using ASP.NET WebForms).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x