0

Problem:

  • I have two build configurations. Each has its own preprocessor symbol: DEV and PROD.
  • I would like each build to have its own background-image color.
  • For C# files, I know that I can use preprocessor directives with the build symbols to conditionally generate code.

Question: How can I do this with a CSS file (like below)?

.page {
    position: relative;
    display: flex;
    flex-direction: column;
}

main {
    flex: 1;
}

.sidebar {
#if PROD
    background-image: linear-gradient(180deg, #ff80ed 0%, gainsboro 90%);
#else
    background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
#endif
}

UPDATE:

  • Alternatively, I'd be just as happy creating a site.css.dev and site.css.prod if that would solve the problem.
2
  • 1
    Three approaches: 1.) Choose a CSS Preprocessor and add it to your build steps. A starting point for learning about available tools may be "Popular CSS preprocessors ...". You need a preprocessor that supports conditional logic and that supports passing your DEV|PROD flag. 2.) Maintain separate dev and prod files and have MSBuild copy the appropriate file, e.g. when the configuration is DEV, copy site.css.dev to site.css. The build and/or packaging steps should use site.css. Commented Sep 23, 2023 at 14:40
  • 1
    3.) If it is only the background color that is different between configurations, maintain a site.css file with say the PROD info. On a DEV build, modify the file before it is packaged. Commented Sep 23, 2023 at 14:45

1 Answer 1

0

Inspired by this answer:

  1. Remove the .sidebar code from MainLayout.razor.css.
  2. Add two new files in the wwwroot\css directory:
    1. sidebar.Default.css

.sidebar {
    background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
}

  1. sidebar.Prod.css

.sidebar {
    background-image: linear-gradient(180deg, #ff80ed 0%, gainsboro 90%);
}

  1. Modify _Layout.cshtml as follows:

    <link href="css/site.css" rel="stylesheet" />
        @{
            var isProd = false;
            #if PROD
            isProd = true;
            #endif
        }
        @if (isProd)
        {
            <link href="css/sidebar.Prod.css" rel="stylesheet" />
        }
        else
        {
            <link href="css/sidebar.Default.css" rel="stylesheet" />
        }
        <link href="FooBar.styles.css" rel="stylesheet" />

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.