【参考】cssだけでcheckboxやradioのデザインを変更 – WEBdev
この記事がとても参考になり、発見もあったので、「cssだけでcheckboxやradioのデザインを変更」を書き砕いてみます。
input[type=”radio”]について
DEMO
HTML
<input type="radio" name="radio01" id="radio01_yes"><label for="radio01_yes">らじおぼたん</label>
SASS
input[type="radio"] {
display: inline-block;
margin-right: 6px;
& + label {
position: relative;
display: inline-block;
margin-right: 12px;
font-size: 14px;
font-size: 1.4rem;
line-height: 2;
cursor: pointer;
vertical-align: middle;
}
@media screen and (max-width:スマホ切り替えサイズ) {
& + label {
margin-right: 12px;
font-size: 12px;
font-size: 1.2rem;
line-height: 1.25;
}
}
}
@media (min-width: 1px) {
input[type="radio"] {
display: none;
margin: 0;
&:checked + label::after {
content: "";
position: absolute;
top: 50%;
box-sizing: border-box;
display: block;
}
& + label::before {
content: "";
position: absolute;
top: 50%;
left: 0;
box-sizing: border-box;
display: block;
background: #ffffff;
}
}
input[type="radio"] {
& + label {
padding: 0 10px 0 31px;
line-height: 22px;
}
& + label::before {
width: 22px;
height: 22px;
margin-top: -11px;
}
@media screen and (max-width:スマホ切り替えサイズ) {
& + label {
padding: 0 0 0 27px;
line-height: 18px;
}
& + label::before {
width: 18px;
height: 18px;
margin-top: -9px;
}
}
}
input[type="radio"][disabled] {
cursor: default;
}
input[type="radio"] + label {
background: none;
}
input[type="radio"][disabled] + label {
color: #999999;
cursor: default;
}
input[type="radio"] + label::before {
border: 2px solid #cccccc;
border-radius: 30px;
@media screen and (max-width:スマホ切り替えサイズ) {
border-width: 1px;
}
}
input[type="radio"][disabled] + label::before {
border-color: #999999;
}
input[type="radio"]:checked + label::before {
border-color: #5e942e;
background-color: #e3eeda;
}
input[type="radio"][disabled]:checked + label::before {
border-color: #999999;
background-color: #ffffff;
}
input[type="radio"]:checked + label::after {
left: 8px;
width: 6px;
height: 6px;
margin-top: -3px;
background: #5e942e;
border-radius: 6px;
@media screen and (max-width:スマホ切り替えサイズ) {
left: 6px;
width: 5px;
height: 5px;
border-radius: 5px;
}
}
input[type="radio"][disabled]:checked + label::after {
background: #999999;
}
}
図解
まず、最初にinput[type=”radio”]は「display: none;」で非表示にしてから、labelとlabelの疑似要素を使用してスタイルをつけて行きます。
input[type="radio"] {
display: none;
& + label::before {
display: block;
@media (min-width: 1px)
input[type="radio"] + label {
padding: 0 10px 0 31px;
line-height: 22px;
}
| @media (min-width: 1px)
input[type="radio"] + label::before {
width: 22px;
height: 22px;
margin-top: -11px;
border: 2px solid #cccccc;
border-radius: 30px;
} |
| @media (min-width: 1px)
input[type="radio"]:checked + label::before {
border-color: #5e942e;
background-color: #e3eeda;
} |
| @media (min-width: 1px)
input[type="radio"]:checked + label::after {
left: 8px;
width: 6px;
height: 6px;
margin-top: -3px;
background: #5e942e;
border-radius: 6px;
} |
input[type=”checkbox”]について
DEMO
HTML
<input type="checkbox" id="namae1"><label for="namae1">ちぇっくぼっくす</label>
SASS
input[type="checkbox"] {
display: inline-block;
margin-right: 6px;
& + label {
position: relative;
display: inline-block;
margin-right: 12px;
font-size: 14px;
font-size: 1.4rem;
line-height: 2;
cursor: pointer;
vertical-align: middle;
}
@media screen and (max-width:スマホ切り替えサイズ) {
& + label {
margin-right: 12px;
font-size: 12px;
font-size: 1.2rem;
line-height: 1.25;
}
}
}
@media (min-width: 1px) {
input[type="checkbox"] {
display: none;
margin: 0;
&:checked + label::after {
content: "";
position: absolute;
top: 50%;
box-sizing: border-box;
display: block;
}
& + label::before {
content: "";
position: absolute;
top: 50%;
left: 0;
box-sizing: border-box;
display: block;
background: #ffffff;
}
}
input[type="checkbox"] {
& + label {
padding: 0 10px 0 32px;
line-height: 22px;
}
& + label::before {
width: 22px;
height: 22px;
margin-top: -11px;
}
@media screen and (max-width:スマホ切り替えサイズ) {
& + label {
padding: 0 10px 0 25px;
line-height: 15px;
}
& + label::before {
width: 15px;
height: 15px;
margin-top: -7.5px;
}
}
}
input[type="checkbox"] + label::before {
border-radius: 4px;
border: 2px solid #cccccc;
@media screen and (max-width:スマホ切り替えサイズ) {
border-radius: 2.5px;
border-width: 1px;
}
}
input[type="checkbox"]:checked + label::before, {
border-color: #5e942e;
background-color: #e3eeda;
}
input[type="checkbox"]:checked + label::after {
left: 6px;
width: 10px;
height: 6px;
margin-top: -5px;
border-left: 3px solid #5e942e;
border-bottom: 3px solid #5e942e;
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
@media screen and (max-width:スマホ切り替えサイズ) {
left: 4px;
width: 8px;
height: 6px;
margin-top: -4px;
border-left-width: 2px;
border-bottom-width: 2px;
}
}
}
図解
まず、最初にinput[type=”checkbox”]は「display: none;」で非表示にしてから、labelとlabelの疑似要素を使用してスタイルをつけて行きます。
input[type="checkbox"] {
display: none;
&:checked + label::after {
display: block;
}
& + label::before {
display: block;
}
@media (min-width: 1px)
input[type="checkbox"] + label {
padding: 0 10px 0 32px;
line-height: 22px;
}
| @media (min-width: 1px)
input[type="checkbox"] + label::before {
border-radius: 4px;
border: 2px solid #cccccc;
width: 22px;
height: 22px;
margin-top: -11px;
} |
| @media (min-width: 1px)
input[type="checkbox"]:checked + label::before {
border-color: #5e942e;
background-color: #e3eeda;
} |
| @media (min-width: 1px)
input[type="checkbox"]:checked + label::after {
left: 6px;
width: 10px;
height: 6px;
margin-top: -5px;
border-left: 3px solid #5e942e;
border-bottom: 3px solid #5e942e;
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
} |