のえら

技術備忘とかメモとか.間違いはつっこんでいただきたい所存.アフィリエイトはやっていません.

ラジオボタンの見た目をチェックボックスにしたい

「必須にしたいからラジオボタンをおきたい、でも見た目はチェックボックスにしたい」というお客様の要望をかなえるために調べたり作ったりしたのでメモらいず。
チェックボックスでも、jQueryでゴリゴリっと書けば必須にできるけど、見た目だけ変えて対応したい場合の書き方。
なんにせよ、チェックボックスっぽいのに外せない!!ってなるやもしれん。。

パターン1

実際のラジオボタンを隠して、ラベルをフラットなチェックボックスに見せる。

http://martinwolf.org/2013/09/13/pure-css-radio-and-checkbox-inputs/
※リンク先ではチェックボックスラジオボタンそれぞれでフラットデザインにする方法を解説していますが、今回はチェックボックスのスタイルをそのままラジオボタンにあてておりまする

見た目(※firefoxで確認)

f:id:noterr0001:20140829175733p:plain

サンプル
<!DOCTYPE html>
<html lang="ja">
 <head>
  <style id="holderjs-style" type="text/css">
   .flat-rg {
    position: relative;
    display: inline-block;
    padding-left: 20px;
    line-height: 1.2;
    margin-bottom: 0;
   }
   .flat-rg:before {
    content: '';
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 15px;
    height: 15px;
    border: 1px solid #222;
   }
   input[type="radio"] {
    display: none;
   }
   input[type="radio"]:checked + label:before {
    background: #222;
   }
  </style>
 </head>
 <body>
  <input id="radio1" type="radio" name="radio-group" checked>
  <label for="radio1" class="flat-rg"> Checkbox 1</label>
  
  <input id="radio2" type="radio" name="radio-group">
  <label for="radio2" class="flat-rg"> Checkbox 2</label>
 </body>
</html>

パターン2

パターン1とBootstrapの複合技。ラジオボタンを隠すのは変わらないけど、contentでフォントを指定することでアイコンをボタンのように見せられる。ちょっと見た目がカッコよくなる、と個人的には気に入っている。

見た目(※firefoxで確認)

f:id:noterr0001:20140829175737p:plain

サンプル
<!DOCTYPE html>
<html lang="ja">
 <head>
  <link type-"text/css" rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <style id="holderjs-style" type="text/css">
   .flat-rg {
    position: relative;
    display: inline-block;
    padding-left: 20px;
    line-height: 1.2;
    margin-bottom: 0;
   }
   .flat-rg:before {
    content: "\e157";
    display: block;
    position: relative;
    top: 1px;
    display: inline-block;
    font-family: 'Glyphicons Halflings';
    font-weight: normal;
   }
   input[type="radio"] {
    display: none;
   }
   input[type="radio"]:checked + label:before {
    background: none;
    content: "\e067";
   }
  </style>
 </head>
 <body>
  <input id="radio1" type="radio" name="radio-group" checked>
  <label for="radio1" class="flat-rg"> Checkbox 1</label>
  
  <input id="radio2" type="radio" name="radio-group">
  <label for="radio2" class="flat-rg"> Checkbox 2</label>
 </body>
</html>

パターン3

http://stackoverflow.com/questions/279421/can-you-style-an-html-radio-button-to-look-like-a-checkbox

appearanceプロパティで、インタフェースの見た目を変えてしまおう、という。このプロパティすげぇ。
ベンダープレフィックスあるけど、IEoperaはダメっぽい(not currently supported)

見た目(※firefoxで確認)

f:id:noterr0001:20140829175744p:plain

サンプル
<!DOCTYPE html>
<html lang="ja">
 <head>
  <style id="holderjs-style" type="text/css">
   input[type="radio"] {
    -webkit-appearance: checkbox;
    -moz-appearance: checkbox;
    -ms-appearance: checkbox;     /* not currently supported */
    -o-appearance: checkbox;      /* not currently supported */
   }
  </style>
 </head>
 <body>
  <label><input type="radio" name="radio" checked> Checkbox 1</label>
  <label><input type="radio" name="radio"> Checkbox 2</label>
 </body>
</html>