How do you create a toggle button?

后端 未结 15 2935
名媛妹妹
名媛妹妹 2020-11-27 11:46

I want to create a toggle button in html using css. I want it so that when you click on it , it stays pushed in and than when you click it on it again it pops out.

15条回答
  •  余生分开走
    2020-11-27 11:58

    here is an example using pure css:

      .cmn-toggle {
        position: absolute;
        margin-left: -9999px;
        visibility: hidden;
      }
      .cmn-toggle + label {
        display: block;
        position: relative;
        cursor: pointer;
        outline: none;
        user-select: none;
      }
      input.cmn-toggle-round + label {
        padding: 2px;
        width: 120px;
        height: 60px;
        background-color: #ffffdffffd;
        border-radius: 60px;
      }
      input.cmn-toggle-round + label:before,
      input.cmn-toggle-round + label:after {
        display: block;
        position: absolute;
        top: 1px;
        left: 1px;
        bottom: 1px;
        content: "";
      }
      input.cmn-toggle-round + label:before {
        right: 1px;
        background-color: #f1f1f1;
        border-radius: 60px;
        transition: background 0.4s;
      }
      input.cmn-toggle-round + label:after {
        width: 58px;
        background-color: #fff;
        border-radius: 100%;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
        transition: margin 0.4s;
      }
      input.cmn-toggle-round:checked + label:before {
        background-color: #8ce196;
      }
      input.cmn-toggle-round:checked + label:after {
        margin-left: 60px;
      }

提交回复
热议问题