CSS Equivalent of the “if” statement

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

Is there any way to use conditional statements in CSS?

12条回答
  •  一个人的身影
    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.

提交回复
热议问题