Toggle radio input using CSS only

前端 未结 6 1739
慢半拍i
慢半拍i 2020-12-09 04:00

The following code use a script to toggle/uncheck a radio when clicked a second time on the same.

My question is how do I do this using CSS only?

6条回答
  •  情书的邮戳
    2020-12-09 04:37

    If the effect does not need to be persistent, you can achieve something similar playing with :focus instead of using radio buttons.

    To make an element focusable, set the tabindex attribute to an integer. Use a negative one if you don't want the element to be reached via sequential focus navigation (pressing the "tab" key).

    .container {
      display: flex;
      flex-wrap: wrap;
      max-width: 660px;
    }
    .container > .img {
      flex: 1;
      position: relative;
    }
    .container > .img > .unselect {
      display: none;
      position: absolute;
      top: 0; right: 0; bottom: 0; left: 0;
    }
    .container > .txt {
      display: none;
      order: 1;
      flex-basis: 100%;
    }
    .container > .img:focus > .unselect,
    .container > .img:focus + .txt {
      display: block;
    }
    Recipe nr 1
    Recipe nr 2
    Recipe nr 3

提交回复
热议问题