41

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 should choose a sad smiley.

I think this should be done using radio buttons, with the smileys instead of the radio buttons. Maybe I'm wrong though...

Do you know how I can achieve this?

Thanks!

learning-html
  • 411
  • 1
  • 5
  • 3
  • Possible duplicate of [Use Image instead of radio button](http://stackoverflow.com/questions/17541614/use-image-instead-of-radio-button) – Richard Cotrina Jan 20 '17 at 15:43

10 Answers10

68

Let's keep them simple, shall we. First off, using pure HTML + CSS:

<div id="emotion">
    <input type="radio" name="emotion" id="sad" />
        <label for="sad"><img src="sad_image.png" alt="I'm sad" /></label>

    <input type="radio" name="emotion" id="happy" />
        <label for="happy"><img src="happy_image.png" alt="I'm happy" /></label>
</div>

This will degrade nicely if there's no JavaScript. Use id and for attributes to link up the label and radiobutton so that when the image is selected, the corresponding radiobutton will be filled. This is important because we'll need to hide the actual radiobutton using JavaScript. Now for some jQuery goodness. First off, creating the CSS we'll need:

.input_hidden {
    position: absolute;
    left: -9999px;
}

.selected {
    background-color: #ccc;
}

#emotion label {
    display: inline-block;
    cursor: pointer;
}

#emotion label img {
    padding: 3px;
}

Now for the JavaScript:

$('#emotion input:radio').addClass('input_hidden');
$('#emotion label').click(function(){
    $(this).addClass('selected').siblings().removeClass('selected');
});

The reason why we're not using display: none here is for accessibility reasons. See: http://www.jsfiddle.net/yijiang/Zgh24/1 for a live demo, with something more fancy.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
  • The only thing you seem to be missing here is setting the appropriate input's `checked` value when the corresponding image is clicked. – Rob Sobers Aug 19 '11 at 01:55
  • @Rob clicking on `label`s would check the appropriate `input`s - no JavaScript needed. You can comment out the first line of the JavaScript code to see this. – Yi Jiang Aug 19 '11 at 08:10
  • Ah, you're right! Thanks. I modified your example slightly to make the image the clickable thing, which would be why I saw that behavior. My bad. – Rob Sobers Aug 19 '11 at 13:26
  • 10
    JavaScript is no longer needed for this. `input[type=radio]:checked + label>img` will select the image just fine. See: http://codepen.io/DavidBradbury/pen/HuCqx – David Bradbury Jun 24 '13 at 18:37
37

You can take advantage of CSS3 to do that, by hidding the by-default input radio button with CSS3 rules:

.class-selector input{
    margin:0;padding:0;
    -webkit-appearance:none;
       -moz-appearance:none;
            appearance:none;
}

And then using labels for images as the following demos:

JSFiddle Demo 1

From top to bottom: Unfocused, MasterCard Selected, Visa Selected, Mastercard hovered

JSFiddle Demo 2

Visa selected

Gist - How to use images for radio-buttons

Community
  • 1
  • 1
Richard Cotrina
  • 2,525
  • 1
  • 23
  • 23
  • 1
    Great solution, but how to make it work with responsive design. In css i see .drinkcard-cc - width and height fixed.. – Peter Apr 15 '15 at 10:25
  • 2
    Sorry for the delayed answer. Just wrap them into a responsive container, then set the width or height at 100%. – Richard Cotrina Aug 09 '15 at 21:38
  • 1
    Hi @RichardCotrina is there a way of making this compatible with IE? – Raulnd Oct 09 '15 at 15:33
  • 1
    @Raulnd maybe setting ``display:none;`` to the ````. I can't test IE compatibility right now. Please let me know if it works :) – Richard Cotrina Oct 09 '15 at 16:12
  • @RichardCotrina that hides the radio but then you wouldnt know which one is selected because both images are full color. I hope I explained myself – Raulnd Oct 10 '15 at 16:14
  • 1
    @Raulnd Uhmm, You should add ``-ms-filter`` filter rules for grayscale, and just in case that doesn't work, try playing with ``opacity``. – Richard Cotrina Oct 10 '15 at 16:52
  • 2
    @RichardCotrina found it! opacity: 0.4; filter: alpha(opacity=40); – Raulnd Oct 10 '15 at 18:43
4

Here's a pure HTML+CSS solution.

HTML:

<div class="image-radio">
  <input type="radio" value="true" checked="checked" name="ice_cream" id="ice_cream_vanilla">
  <label for="ice_cream_vanilla">Vanilla</label>
  <input type="radio" value="true" name="ice_cream" id="ice_cream_chocolate">
  <label for="ice_cream_chocolate">Chocolate</label>
</div>

SCSS:

  // use an image instead of the native radio widget
.image-radio {   
  input[type=radio] {
    display: none;
  }
  input[type=radio] + label {
    background: asset-url('icons/choice-unchecked.svg') no-repeat left;
    padding-left: 2rem;
  }
  input[type=radio]:checked + label {
    background: asset-url('icons/choice-checked.svg') no-repeat left;
  }
}
AlexChaffee
  • 8,092
  • 2
  • 49
  • 55
2

With pure html (no JS), you can't really substitute a radio-button for an image (at least, I don't think you can). You could, though use the following to make the same connection to the user:

<form action="" method="post">
    <fieldset>
       <input type="radio" name="feeling" id="feelingSad" value="sad" /><label for="feelingSad"><img src="path/to/sad.png" /></label>
       <label for="feelingHappy"><input type="radio" name="feeling" id="feelingHappy" value="happy" /><img src="path/to/happy.png" /></label>
    </fieldset>
</form>
David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • Wrap the images in `label` elements for more semantic goodness, as well as to automatically make the buttons. Also, `alt` attributes are **mandatory**, so you'll need those too on the `img` tags. – Yi Jiang Oct 09 '10 at 15:37
  • That's what I get for rushing...thanks @Yi and @cherouvim, you're both absolutely right. – David Thomas Oct 09 '10 at 15:42
1

I have edited one of the previous post. Now, it is way more simple and it works perfectly.

<input style="position: absolute;left:-9999px;" type="radio" name="emotion" id="sad" />
<label for="sad"><img src="red.gif" style="display: inline-block;cursor: pointer;padding: 3px;" alt="I'm sad" /></label>

<input  style="position: absolute;left:-9999px;" type="radio" name="emotion" id="happy" />
<label for="happy"><img src="blue.gif" style="display: inline-block;cursor: pointer;padding: 3px;" alt="I'm happy" /></label>
Can
  • 13
  • 2
0

Faced with the same problem I created a simple jQuery plugin http://xypaul.github.io/radioimg.js/

It works using hidden radio buttons and labels containing images as shown below.

<input type="radio" style="display: none;" id="a" />
<label for="a">
   <img class="" />
</label>
Paul
  • 1,190
  • 3
  • 12
  • 24
0

You cannot style things like radio buttons, checkboxes, scrollsbars (etc.) at all. These are native to the OS and the browser and not something you can manipulate.

You can simulate this, however by hiding the radio buttons and only showing an image instead as in.

<input type="radio" style="display: none;" id="sad" /><label for="sad"><img class="sad_image" /></label>
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

Use jQuery. Keep your checkbox elements hidden and create a list like this:

<ul id="list">
    <li><a href="javascript:void(0)" id="link1">Happy face</a></li>
    <li><a href="javascript:void(0)" id="link2">Sad face</a></li>
</ul>

<form action="file.php" method="post">
    <!-- More code -->
    <input type="radio" id="option1" name="radio1" value="happy" style="display:none"/>
    <input type="radio" id="option2" name="radio1" value="sad" style="display:none"/>
    <!-- More code -->
</form>

<script type="text/javascript">
$("#list li a").click(function() {
    $('#list .active').removeClass("active");
    var id = this.id;
    var newselect = id.replace('link', 'option');
    $('#'+newselect).attr('checked', true);
    $(this).addClass("active").parent().addClass("active");
    return false;
});
</script>

This code would add the checked attribute to your radio inputs in the background and assign class active to your list elements. Do not use inline styles of course, don't forget to include jQuery and everything should run out of the box after you customize it.

Cheers!

Claudiu
  • 3,261
  • 1
  • 15
  • 27
0

another alternative is to use a form replacement script/library. They usually hide the original element and replace them with a div or span, which you can style in whatever way you like.

Examples are:

http://customformelements.net (based on mootools) http://www.htmldrive.net/items/show/481/jQuery-UI-Radiobutton-und-Checkbox-Replacement.html

Makibo
  • 1,679
  • 21
  • 31
0

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

   <div class="customize-radio">
        <label>Favourite Smiley</label>
        <br>
        <label for="hahaha">
          <input type="radio" name="smiley" id="hahaha">
          <span class="haha-img"></span>
          HAHAHA
        </label>
        <label for="kiss">
          <input type="radio" name="smiley" id="kiss">
          <span class="kiss-img"></span>
          Kiss
        </label>
        <label for="tongueOut">
          <input type="radio" name="smiley" id="tongueOut">
          <span class="tongueout-img"></span>
          TongueOut
        </label>
    </div>

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

sr9yar
  • 4,850
  • 5
  • 53
  • 59
Beginner
  • 21
  • 3