How do I style radio buttons with images - laughing smiley for good, sad smiley for bad?

后端 未结 10 1146
滥情空心
滥情空心 2020-11-30 19:56

I would like to create an HTML form for user feedback. If the overall feedback is good, the user should click on a laughing smiley, if the overall feedback is bad, the user

10条回答
  •  北海茫月
    2020-11-30 20:14

    Images can be placed in place of radio buttons by using label and span elements.

       

    Radio button should be hidden,

    .customize-radio label > input[type = 'radio'] {
        visibility: hidden;
        position: absolute;
    }
    

    Image can be given in the span tag,

    .customize-radio label > input[type = 'radio'] ~ span{
        cursor: pointer;
        width: 27px;
        height: 24px;
        display: inline-block;
        background-size: 27px 24px;
        background-repeat: no-repeat;
    }
    .haha-img {
        background-image: url('hahabefore.png');
    }
    
    .kiss-img{
        background-image: url('kissbefore.png');
    }
    .tongueout-img{
        background-image: url('tongueoutbefore.png');
    }
    

    To change the image on click of radio button, add checked state to the input tag,

    .customize-radio label > input[type = 'radio']:checked ~ span.haha-img{
         background-image: url('haha.png');
    }
    .customize-radio label > input[type = 'radio']:checked ~ span.kiss-img{
        background-image: url('kiss.png');
    }
    .customize-radio label > input[type = 'radio']:checked ~ span.tongueout-img{
            background-image: url('tongueout.png');
    }
    

    If you have any queries, Refer to the following link, As I have taken solution from the below blog, http://frontendsupport.blogspot.com/2018/06/cool-radio-buttons-with-images.html

提交回复
热议问题