CSS Equivalent of the “if” statement

前端 未结 12 1383
暖寄归人
暖寄归人 2020-12-01 13:49

Is there any way to use conditional statements in CSS?

12条回答
  •  渐次进展
    2020-12-01 14:29

    I'd say the closest thing to "IF" in CSS are media queries, such as those you can use for responsive design. With media queries, you're saying things like, "If the screen is between 440px and 660px wide, do this". Read more about media queries here: http://www.w3schools.com/cssref/css3_pr_mediaquery.asp, and here's an example of how they look:

    @media screen and (max-width: 300px) {
      body {
         background-color: lightblue;
      }
    }
    

    That's pretty much the extent of "IF" within CSS, except to move over to SASS/SCSS (as mentioned above).

    I think your best bet is to change your classes / IDs within the scripting language, and then treat each of the class/ID options in your CSS. For instance, in PHP, it might be something like:

     B ){
    echo '
    '; } else{ echo '
    '; } ?>

    Then your CSS can be like

    .option-a {
    background-color:red;
    }
    .option-b {
    background-color:blue;
    }
    

提交回复
热议问题