Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 276849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:59:03+00:00 2026-05-12T00:59:03+00:00

I would like to use conditions in my CSS. The idea is that I

  • 0

I would like to use conditions in my CSS.

The idea is that I have a variable that I replace when the site is run to generate the right style-sheet.

I want it so that according to this variable the style-sheet changes!

It looks like:

[if {var} eq 2 ]
    background-position : 150px 8px;
[else]
    background-position : 4px 8px; 

Can this be done? How do you do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:59:03+00:00Added an answer on May 12, 2026 at 12:59 am

    Not in the traditional sense, but you can use classes for this, if you have access to the HTML. Consider this:

    <p class="normal">Text</p>
    
    <p class="active">Text</p>
    

    and in your CSS file:

    p.normal {
      background-position : 150px 8px;
    }
    p.active {
      background-position : 4px 8px;
    }
    

    That’s the CSS way to do it.


    Then there are CSS preprocessors like Sass. You can use conditionals there, which’d look like this:

    $type: monster;
    p {
      @if $type == ocean {
        color: blue;
      } @else if $type == matador {
        color: red;
      } @else if $type == monster {
        color: green;
      } @else {
        color: black;
      }
    }
    

    Disadvantages are, that you’re bound to pre-process your stylesheets, and that the condition is evaluated at compile time, not run time.


    A newer feature of CSS proper are custom properties (a.k.a. CSS variables). They are evaluated at run time (in browsers supporting them).

    With them you could do something along the line:

    :root {
      --main-bg-color: brown;
    }
    
    .one {
      background-color: var(--main-bg-color);
    }
    
    .two {
      background-color: black;
    }
    

    Finally, you can preprocess your stylesheet with your favourite server-side language. If you’re using PHP, serve a style.css.php file, that looks something like this:

    p {
      background-position: <?php echo (@$_GET['foo'] == 'bar')? "150" : "4"; ?>px 8px;
    }
    

    In this case, you will however have a performance impact, since caching such a stylesheet will be difficult.


    On a more high-level note, Ahmad Shadeed shows in this article a lot of very useful techniques to decide if/else questions often coming up in UI development purely within CSS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a website that runs on ASP.NET. I would like to run a
I have 1 IIS server that I would like to use to host 2
I have a series of PHP page, and I would like to use conditional
I have an array of Integers in Java, I would like use only a
I have a bunch of CSS written that makes my site target Mobile first,
I have a function in my model that I need to use multiple conditions
I would like generate list like day=>title: $news = $this->News->find('list', array( 'conditions' => array(
I would like to use AND/OR between the conditions in a stored procedure, and
I would like to remove items from a list that fulfill certain conditions. I
I would like to use R to extract the speaker out of scripts formatted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.