htmlcsscheckboxtoggle

Style checkboxes as Toggle Buttons


On my website, users can post articles and tag them accordingly using some pre-set tags. These tags are in the form of checkboxes. Example below:

<input type="checkbox" name="wpuf_post_tags[]" class="new-post-tags" value="Aliens" /> Aliens
<input type="checkbox" name="wpuf_post_tags[]" class="new-post-tags" value="Ghosts" /> Ghosts
<input type="checkbox" name="wpuf_post_tags[]" class="new-post-tags" value="Monsters" /> Monsters

As you might know, the checkboxes will look something like this:

[ ] Aliens

[o] Ghosts

[ ] Monsters

I would like to do is have the checkbox being one large button with the value inside of it. And then make it have a "toggle" effect.

[ Aliens ] [ Ghosts ] [ Monsters ]

How would I go about doing this?


Solution

  • Check this out

    Note that the last selector may not work in older IE.

    .vis-hidden {
      border: 0;
      clip: rect(0 0 0 0);
      height: 1px;
      margin: -1px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1px;
    }
    
    label {
      margin: 10px;
      padding: 5px;
      border: 1px solid gray;
    }
    
    input:focus+label {
      border-color: blue;
    }
    
    input:checked+label {
      border-color: red;
    }
    
    input:focus:checked+label {
      border-color: green;
    }
    <input id="chk_aliens" type="checkbox" name="wpuf_post_tags[]" class="vis-hidden new-post-tags" value="Aliens" />
    <label for="chk_aliens">Aliens</label>
    
    <input id="chk_ghosts" type="checkbox" name="wpuf_post_tags[]" class="vis-hidden new-post-tags" value="Ghosts" />
    <label for="chk_ghosts">Ghosts</label>
    
    <input id="chk_monsters" type="checkbox" name="wpuf_post_tags[]" class="vis-hidden new-post-tags" value="Monsters" />
    <label for="chk_monsters">Monsters</label>