Radio button with custom image

后端 未结 2 1744
日久生厌
日久生厌 2021-01-17 03:02

I\'ve used this example for checkboxes but then later I realized I need one for the radio buttons.

Can someone please customize this example for radio buttons?

2条回答
  •  遇见更好的自我
    2021-01-17 03:28

    Use Pseudo-elements in this case i am using ::before (:before)

    enter image description here

    Update: since firefox doesn't support pseudo-elements on inputs yet, use the adjacent sibling selectors

    enter image description here

    :root{padding: 40px}
    
    [name=radio]{
        display: none
    }
    
    [for^=radio]{
        position: relative;
        margin: 64px
    }
    
    [for^=radio]:before{
        content: '';
        position: absolute;
        left: -15px;
        top: -15px;
        width: 32px;
        height: 32px;
        background: red
    }
    
    [type=radio]:checked + [for^=radio]:before{
        background: green
    }
    
    
    
    
    
    

    Or the General sibling selectors

    enter image description here

    :root{padding: 40px}
    
    [name=radio]{
        display: none
    }
    
    [for^=radio]{
        position: relative;
        margin: 64px
    }
    
    [for^=radio]:before{
        content: '';
        position: absolute;
        left: -15px;
        top: -15px;
        width: 32px;
        height: 32px;
        background: red
    }
    
    [id=radio-1]:checked ~ [for=radio-1]:before,
    [id=radio-2]:checked ~ [for=radio-2]:before,
    [id=radio-3]:checked ~ [for=radio-3]:before{
        background: green
    }
    
    
    
    
    
    
    

    The basic

    [type=radio]{
        position: relative;
        margin: 40px
    }
    
    [type=radio]:before{
        content: '';
        position: absolute;
        left: -15px;
        top: -15px;
        width: 32px;
        height: 32px;
        background: red
    }
    
    [type=radio]:checked:before{
        background: green
    }

    multiple inputs

    [type=radio]{
        position: relative;
        margin: 40px
    }
    
    [type=radio]:before{
        content: '';
        position: absolute;
        left: -15px;
        top: -15px;
        width: 32px;
        height: 32px;
        background: red
    }
    
    [type=radio]:checked:before{
        background: green
    }
    
    
    

提交回复
热议问题