Use images instead of radio buttons

后端 未结 8 1793
Happy的楠姐
Happy的楠姐 2020-11-22 16:47

If I have a radio group with buttons:

\"Image

... how can I show only images in the select option i

8条回答
  •  独厮守ぢ
    2020-11-22 17:29

    • Wrap radio and image in
    • Hide radio button (Don't use display:none or visibility:hidden since such will impact accessibility)
    • Target the image next to the hidden radio using Adjacent sibling selector +

    /* HIDE RADIO */
    [type=radio] { 
      position: absolute;
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    /* IMAGE STYLES */
    [type=radio] + img {
      cursor: pointer;
    }
    
    /* CHECKED STYLES */
    [type=radio]:checked + img {
      outline: 2px solid #f00;
    }
    
    
    

    Don't forget to add a class to your labels and in CSS use that class instead.


    Custom styles and animations

    Here's an advanced version using the element and the :after pseudo:

    body{color:#444;font:100%/1.4 sans-serif;}
    
    
    /* CUSTOM RADIO & CHECKBOXES
       http://stackoverflow.com/a/17541916/383904 */
    .rad,
    .ckb{
      cursor: pointer;
      user-select: none;
      -webkit-user-select: none;
      -webkit-touch-callout: none;
    }
    .rad > input,
    .ckb > input{ /* HIDE ORG RADIO & CHECKBOX */
      position: absolute;
      opacity: 0;
      width: 0;
      height: 0;
    }
    /* RADIO & CHECKBOX STYLES */
    /* DEFAULT  STYLE */
    .rad > i,
    .ckb > i{ 
      display: inline-block;
      vertical-align: middle;
      width:  16px;
      height: 16px;
      border-radius: 50%;
      transition: 0.2s;
      box-shadow: inset 0 0 0 8px #fff;
      border: 1px solid gray;
      background: gray;
    }
    /* CHECKBOX OVERWRITE STYLES */
    .ckb > i {
      width: 25px;
      border-radius: 3px;
    }
    .rad:hover > i{ /* HOVER  STYLE */
      box-shadow: inset 0 0 0 3px #fff;
      background: gray;
    }
    .rad > input:checked + i{ /* (RADIO CHECKED)  STYLE */
      box-shadow: inset 0 0 0 3px #fff;
      background: orange;
    }
    /* CHECKBOX */
    .ckb > input + i:after{
      content: "";
      display: block;
      height: 12px;
      width:  12px;
      margin: 2px;
      border-radius: inherit;
      transition: inherit;
      background: gray;
    }
    .ckb > input:checked + i:after{ /* (RADIO CHECKED)  STYLE */
      margin-left: 11px;
      background:  orange;
    }
    
    
    
    

提交回复
热议问题