I want a group of radio buttons to look like a group of toggle buttons (but still function like radio buttons). It's not necessary that they look exactly like toggle buttons.
How can I do this only with CSS and HTML?
EDIT: I will be satisfied making the little circle disappear and changing the style when the button is checked/unchecked.
I usually hide the real radio buttons with CSS (or make them into individual hidden inputs), put in the imagery I want (you could use an unordered list and apply your styles to the li element) and then use click events to toggle the inputs. That approach also means you can keep things accessible for users who aren't on a normal web browser-- just hide your ul by default and show the radio buttons.
Depending on which browsers you aim to support, you could use the
:checked
pseudo-class selector in addition to hiding the radio buttons.Using this HTML:
You could use something like the following CSS:
For instance, (to keep the custom CSS brief) if you were using Bootstrap, you might add
class="btn"
to your<label>
elements and style them appropriately to create a toggle that looks like:...which just requires the following additional CSS:
I've included this as well as the extra fallback styles in a radio button toggle jsFiddle demo. Note that
:checked
is only supported in IE 9, so this approach is limited to newer browsers.However, if you need to support IE 8 and are willing to fall back on JavaScript*, you can hack in pseudo-support for
:checked
without too much difficulty (although you can just as easily set classes directly on the label at that point).Using some quick and dirty jQuery code as an example of the workaround:
You can then make a few changes to the CSS to complete things:
*If you're using Modernizr, you can use the
:selector
test to help determine if you need the fallback. I called my test "checkedselector" in the example code, and the jQuery event handler is subsequently only set up when the test fails.Here's my version of that nice CSS solution JS Fiddle example posted above.
http://jsfiddle.net/496c9/
HTML
CSS
Styled with coloured buttons :)
PURE CSS AND HTML (as asked)
Example Image:
After looking for something really clean and straight forward, I ended up building this with ONE simple change from another code that was built only thinking on checkboxes, so I tryed the funcionality for RADIOS and it worked too(!).
The CSS (SCSS) is fully from @mallendeo (as established on the JS credits), what I did was simply change the type of the input to RADIO, and gave the same name to all the radio switches.... and VOILA!! They deactivate automatically one to the other!!
Very clean, and as you asked it's only CSS and HTML!!
It is exactly what I was looking for since 3 days after trying and editing more than a dozen of options (which mostly requiered jQuery, or didn't allow labels, or even wheren't really compatible with current browsers). This one's got it all!
I'm obligated to include the code in here to allow you to see a working example, so:
If you run the snippet, you'll see I leave the iOS radio checked and disabled, so you can watch how it is also affected when activating another one. I also included 2 labels for each radio, one before and one after. The copy of the original code to show the working checkboxes in the same window is also included.
I know this is an old question, but since I was just looking to do this, I thought I would post what I ended up with. Because I am using Bootstrap, I went with a Bootstrap option.
HTML
jQuery
I chose to store the value in a hidden field so that it would be easy for me to get the value server-side.
Here is the solution that works for all browsers (also IE7 and IE8; didn't check for IE6):
http://jsfiddle.net/RkvAP/230/
HTML
JS
CSS
Makes use of minimal JS (jQuery, two lines).