CSS Equivalent of the “if” statement

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

Is there any way to use conditional statements in CSS?

相关标签:
12条回答
  • 2020-12-01 14:23

    css files do not support conditional statements.

    If you want something to look one of two ways, depending on some condition, give it a suitable class using your server side scripting language or javascript. eg

    <div class="oh-yes"></div>
    <div class="hell-no"></div>
    
    0 讨论(0)
  • 2020-12-01 14:24

    CSS has become a very powerful tool over the years and it has hacks for a lot of things javaScript can do

    There is a hack in CSS for using conditional statements/logic.

    It involves using the symbol '~'

    Let me further illustrate with an example.

    Let's say you want a background to slide into the page when a button is clicked. All you need to do is use a radio checkbox. Style the label for the radio underneath the button so that when the button is pressed the checkbox is also pressed.

    Then you use the code below

    .checkbox:checked ~ .background{
    opacity:1
    width: 100%
    }
    

    This code simply states IF the checkbox is CHECKED then open up the background ELSE leave it as it is.

    0 讨论(0)
  • 2020-12-01 14:26

    I would argue that you can use if statements in CSS. Although they aren't worded as such. In the example below, I've said that if the check-box is checked I want the background changed to white. If you want to see a working example check out www.armstrongdes.com. I built this for a client. Re size your window so that the mobile navigation takes over and click the nav button. All CSS. I think it's safe to say this concept could be used for many things.

         #sidebartoggler:checked + .page-wrap .hamb {
            background: #fff;
          }
    
    // example set as if statement sudo code.
    
    if (sidebaretoggler is checked == true) {
    set the background color of .hamb to white;
    }
    
    0 讨论(0)
  • 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:

    <?php
      if( A > B ){
    echo '<div class="option-a">';
    } 
        else{
    echo '<div class="option-b">';
    }
    ?>
    

    Then your CSS can be like

    .option-a {
    background-color:red;
    }
    .option-b {
    background-color:blue;
    }
    
    0 讨论(0)
  • 2020-12-01 14:29

    No you can't do if in CSS, but you can choose which style sheet you will use

    Here is an example :

    <!--[if IE 6]>
    Special instructions for IE 6 here
    <![endif]-->
    

    will use only for IE 6 here is the website where it is from http://www.quirksmode.org/css/condcom.html , only IE has conditional comments. Other browser do not, although there are some properties you can use for Firefox starting with -moz or for safari starting with -webkit. You can use javascript to detect which browser you're using and use javascript if for whatever actions you want to perform but that is a bad idea, since it can be disabled.

    0 讨论(0)
  • 2020-12-01 14:33

    CSS itself doesn't have conditional statements, but here's a hack involving custom properties (a.k.a. "css variables").

    In this trivial example, you want to apply a padding based on a certain condition—like an "if" statement.

    :root   { --is-big: 0; }
    
    .is-big { --is-big: 1; }
    
    .block {
      padding: calc(
        4rem * var(--is-big) +
        1rem * (1 - var(--is-big))
      );
    }
    

    So any .block that's an .is-big or that's a descendant of one will have a padding of 4rem, while all other blocks will only have 1rem. Now I call this a "trivial" example because it can be done without the hack.

    .block {
      padding: 1rem;
    }
    
    .is-big .block,
    .block.is-big {
      padding: 4rem;
    }
    

    But I will leave its applications to your imagination.

    0 讨论(0)
提交回复
热议问题