Webデザイン

【PR】を含みます。

【CSS】ラジオボタンのデザインをカスタマイズする方法

CSS ラジオボタンのデザインをカスタマイズする方法

CSSでラジオボタンのデザインをカスタマイズする方法を解説します。

appearance: none;でデフォルトのスタイルを削除後、カスタムデザインを当ててカスタマイズします。

【テンプレート】ラジオボタンのデザインをカスタマイズ

サンプル

実装コード

HTML
Copy
  1. <label class="radio_container">
  2.   <input class="radio" type="radio" name="radio">
  3.   ラジオボタン
  4. </label>
  5. <label class="radio_container">
  6.   <input class="radio" type="radio" name="radio">
  7.   ラジオボタン
  8. </label>
CSS
Copy
  1. .radio_container {
  2.   display: inline-block;
  3.   cursor: pointer;
  4. }
  5. /* 選択前のデザイン */
  6. .radio {
  7.   appearance: none; /* デフォルトスタイルを削除 */
  8.   -webkit-appearance: none; /* Safari and Chrome */
  9.   -moz-appearance: none; /* Firefox */
  10.   margin: 0;
  11.   margin-right: 3px;
  12.   width: 15px;
  13.   height: 15px;
  14.   vertical-align: middle;
  15.   border-radius: 100%;
  16.   border-width: 1px;
  17.   border-style: solid;
  18.   border-color: #c6c6c6;
  19.   background-color: #fff;
  20.   position: relative;
  21.   top: -2px;
  22. }
  23. /* 選択後のデザイン */
  24. .radio:checked {
  25.   border-color: #f09896;
  26.   background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" stroke-width="3" fill="%23f09896" /></svg>');
  27.   background-size: 11px;
  28.   background-repeat: no-repeat;
  29.   background-position: center;
  30. }

コード解説

選択前のデザイン

appearance: none;
ラジオボタンのデフォルトスタイルを削除します。
これにより、カスタムスタイルが適用可能になります。
margin: 0;
ラジオボタンの余白をリセットします。
margin-right: 3px;
ラジオボタンの右側に3pxの余白を追加します。
width: 15px;
ラジオボタンの幅を指定します。
height: 15px;
ラジオボタンの高さを指定します。
vertical-align: middle;
ラジオボタンを縦方向に中央揃えします。
border-radius: 100%;
円にします。
border-width: 1px;
ボーダーの幅を指定します。
border-style: solid;
ボーダーのスタイルを指定します。
border-color: #c6c6c6;
ボーダーの色を指定します。
background-color: #fff;
背景色を設定します。
position: relative;
top: -2px;
ラジオボタンの位置を微調整します。

選択後のデザイン

.radio:checked
ラジオボタンが選択された状態のスタイルを指定できます。
border-color: #f09896;
選択されたときのボーダーの色を指定します。
background-image
SVG形式の円画像をデータURIとして埋め込んでいます。
これにより、外部画像を使用せずにを円を表示できます。
background-size: 11px;
背景画像のサイズを指定します。
background-repeat: no-repeat;
背景画像を繰り返さないようにします。
background-position: center;
背景画像を中央に配置します。

選択時のラジオボタンの円の色を変更

サンプル

実装コード

円の色は、SVG形式の円画像(background-image)のfill="%23468fd6"で指定しています。

fill="%23468fd6"%23は # 記号に対応し、468fd6が16進数カラーコードです。

HTML
Copy
  1. <label class="radio_container">
  2.   <input class="radio2" type="radio" name="radio2">
  3.   ラジオボタン
  4. </label>
  5. <label class="radio_container">
  6.   <input class="radio2" type="radio" name="radio2">
  7.   ラジオボタン
  8. </label>
CSS
Copy
  1. .radio_container {
  2.   display: inline-block;
  3.   cursor: pointer;
  4. }
  5. /* 選択前のデザイン */
  6. .radio2 {
  7.   appearance: none; /* デフォルトスタイルを削除 */
  8.   -webkit-appearance: none; /* Safari and Chrome */
  9.   -moz-appearance: none; /* Firefox */
  10.   margin: 0;
  11.   margin-right: 3px;
  12.   width: 15px;
  13.   height: 15px;
  14.   vertical-align: middle;
  15.   border-radius: 100%;
  16.   border-width: 1px;
  17.   border-style: solid;
  18.   border-color: #468fd6;
  19.   background-color: #fff;
  20.   position: relative;
  21.   top: -2px;
  22. }
  23. /* 選択後のデザイン */
  24. .radio2:checked {
  25.   border-color: #468fd6;
  26.   background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" stroke-width="3" fill="%23468fd6" /></svg>');
  27.   background-size: 11px;
  28.   background-repeat: no-repeat;
  29.   background-position: center;
  30. }

ラジオボタンにホバーアニメーションを実装

サンプル

実装コード

HTML
Copy
  1. <label class="radio_container">
  2.   <input class="radio3" type="radio" name="radio3">
  3.   ラジオボタン
  4. </label>
  5. <label class="radio_container">
  6.   <input class="radio3" type="radio" name="radio3">
  7.   ラジオボタン
  8. </label>
CSS
Copy
  1. .radio_container {
  2.   display: inline-block;
  3.   cursor: pointer;
  4. }
  5. /* 選択前のデザイン */
  6. .radio3 {
  7.   appearance: none; /* デフォルトスタイルを削除 */
  8.   -webkit-appearance: none; /* Safari and Chrome */
  9.   -moz-appearance: none; /* Firefox */
  10.   margin: 0;
  11.   margin-right: 3px;
  12.   width: 15px;
  13.   height: 15px;
  14.   vertical-align: middle;
  15.   border-radius: 100%;
  16.   border-width: 1px;
  17.   border-style: solid;
  18.   border-color: #c6c6c6;
  19.   background-color: #fff;
  20.   background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" stroke-width="3" fill="%23dfb1b0" /></svg>');
  21.   background-size: 0px;
  22.   background-repeat: no-repeat;
  23.   background-position: center;
  24.   position: relative;
  25.   top: -2px;
  26.   transition: 0.3s;
  27. }
  28. /* ホバー時のデザイン */
  29. .radio3:hover {
  30.   background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" stroke-width="3" fill="%23dfb1b0" /></svg>');
  31.   background-size: 11px;
  32.   background-repeat: no-repeat;
  33.   background-position: center;
  34. }
  35. /* 選択後のデザイン */
  36. .radio3:checked {
  37.   border-color: #f09896;
  38.   background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" stroke-width="3" fill="%23f09896" /></svg>');
  39.   background-size: 11px;
  40.   background-repeat: no-repeat;
  41.   background-position: center;
  42. }

HTML・CSS入門におすすめの1冊

アイコン画像

もみじ

Web製作未経験でも読みやすい構成で、初学者でも安心して学習することができます。

実践的な内容を学ぶことができ解説も丁寧で分かりやすく、基礎的な内容はこの1冊で学ぶことができます。

-Webデザイン
-,