aboutsummaryrefslogtreecommitdiff
path: root/_posts/2019-01-21-easy-custom-radio-inputs.md
blob: b22d90c3c6a9bf4762ad3fbab0a18621fbba8aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
layout: post
title: "Easy Custom Radio Inputs"
date: 2019-01-21
---


Default radio inputs are notoriously horrible looking and are something designers tend to over-think when trying to customize them. Let's walk through how to create custom radio buttons with *pure CSS*, while still preserving performance and accessibility.

## The Final Product

This is what we will be designing:

![Custom radio inputs](/public/images/custom-radio-inputs.png)

[Live CodePen Example](https://codepen.io/bradleytaunt/pen/oNjwMyX)

- - -

## The bones of our radio inputs (HTML)


    <input class="radio-btn" name="radio-collection" id="radio-1" type="radio">
    <label class="radio-label" for="radio-1"><span>I am very satisfied</span></label>
    
    <input class="radio-btn" name="radio-collection" id="radio-2" type="radio">
    <label class="radio-label" for="radio-2"><span>I am satisfied</span></label>
    
    <input class="radio-btn" name="radio-collection" id="radio-3" type="radio">
    <label class="radio-label" for="radio-3"><span>I am indifferent</span></label>
    
    <input class="radio-btn" name="radio-collection" id="radio-4" type="radio">
    <label class="radio-label" for="radio-4"><span>I am unsatisfied</span></label>
    
    <input class="radio-btn" name="radio-collection" id="radio-5" type="radio">
    <label class="radio-label" for="radio-5"><span>I am very unsatisfied</span></label>


I know it looks like a lot is going on here, but it's pretty straightforward so let's unpackage line by line:

### Radio inputs


    <input class="radio-btn" name="radio-collection" id="radio-1" type="radio">


This is the default `radio` input. We give it:

- a `name` (inputs with a shared `name` are grouped together)
- an `id` (so our label can target this input)
- a `class` (so we can style it later)

**Important**: be sure to have a unique `id` for each input so your labels don't end up connected to multiple radios. In this demo we are simply incrementing them by one.

### Labels

Adding the labels is fairly straightforward, we just include the corresponding input's `id` in the label's `for` attribute. The label content is wrapped in a `span` - which I will explain the reasoning for later.

For styling purposes we also add the `radio-label` class.


    <label class="radio-label" for="radio-1"><span>I am very satisfied</span></label>


This is looking pretty terrible - but that's nothing some good ol' CSS can't fix!

## The flesh of our radio inputs (CSS)

First we give some basic styling to our `label` and `input` classes (along with hover states). The `radio` element is actually hidden from view, but by using the `visibility` attribute we still keep it accessible for screen-readers.


    .radio-label {
        background: white;
        border: 1px solid #eee;
        border-radius: 5px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        cursor: pointer;
        display: inline-block;
        font-weight: 600;
        margin: 0 auto 10px;
        /* This 65px padding makes room for the custom input */
        padding: 20px 20px 20px 65px;
        position: relative;
        transition: .3s ease all;
        width: 100%;
    }
    .radio-label:hover {
        box-shadow: 0 4px 8px rgba(0,0,0,0.05);
    }
    .radio-btn {
        position: absolute;
        visibility: hidden;
    }


Remember that `span` element inside the label? We set it's `user-select` property to `none` so we avoid any possible issue with the user selecting the text on-click:


    .radio-label span {
        -webkit-user-select: none;
        -moz-user-select: none;
        user-select: none;
    }


Next we include the default empty selection element (to mimic the original radio input) via a pseudo element:


    .radio-label:before {
        background: #eee;
        border-radius: 50%;
        content:'';
        height: 30px;
        left: 20px;
        position: absolute;
        /* Half the height of it's parent minus half of it's own height */
        top: calc(50% - 15px);
        transition: .3s ease background-color;
        width: 30px;
    }


## A Few Final Steps

The final step is adding the custom styling for when an `input` item is selected (`:checked`).

You will notice the use of a `base64` element for the custom checkmark - feel free to subsitute this for an actual image or none at all (this is just my personal design preference).


    .radio-btn:checked + .radio-label {
        background: #ECF5FF;
        border-color: #4A90E2;
    }
    .radio-btn:checked + .radio-label:before {
        background-color: #4A90E2;
        background-image:  url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIHdpZHRoPSIyNiIgaGVpZ2h0PSIyMCIgdmVyc2lvbj0iMS4xIiB2aWV3Qm94PSIyLjAyOTY4IC00MC4wOTAzIDI2IDIwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIj48IS0tR2VuZXJhdGVkIGJ5IElKU1ZHIChodHRwczovL2dpdGh1Yi5jb20vaWNvbmphci9JSlNWRyktLT48cGF0aCBkPSJNMjcuOTc0MywtMzYuMTI3MmMwLDAuNDQ2NDI4IC0wLjE1NjI1LDAuODI1ODkzIC0wLjQ2ODc1LDEuMTM4MzlsLTEyLjEyMDUsMTIuMTIwNWwtMi4yNzY3OSwyLjI3Njc5Yy0wLjMxMjUsMC4zMTI1IC0wLjY5MTk2NCwwLjQ2ODc1IC0xLjEzODM5LDAuNDY4NzVjLTAuNDQ2NDI4LDAgLTAuODI1ODkzLC0wLjE1NjI1IC0xLjEzODM5LC0wLjQ2ODc1bC0yLjI3Njc5LC0yLjI3Njc5bC02LjA2MDI3LC02LjA2MDI3Yy0wLjMxMjUsLTAuMzEyNSAtMC40Njg3NSwtMC42OTE5NjUgLTAuNDY4NzUsLTEuMTM4MzljMCwtMC40NDY0MjkgMC4xNTYyNSwtMC44MjU4OTMgMC40Njg3NSwtMS4xMzgzOWwyLjI3Njc5LC0yLjI3Njc5YzAuMzEyNSwtMC4zMTI1IDAuNjkxOTY1LC0wLjQ2ODc1IDEuMTM4MzksLTAuNDY4NzVjMC40NDY0MjksMCAwLjgyNTg5MywwLjE1NjI1IDEuMTM4MzksMC40Njg3NWw0LjkyMTg4LDQuOTM4NjJsMTAuOTgyMSwtMTAuOTk4OWMwLjMxMjUsLTAuMzEyNSAwLjY5MTk2NCwtMC40Njg3NSAxLjEzODM5LC0wLjQ2ODc1YzAuNDQ2NDI4LDAgMC44MjU4OTMsMC4xNTYyNSAxLjEzODM5LDAuNDY4NzVsMi4yNzY3OCwyLjI3Njc5YzAuMzEyNSwwLjMxMjUgMC40Njg3NSwwLjY5MTk2NCAwLjQ2ODc1LDEuMTM4MzlaIiB0cmFuc2Zvcm09InNjYWxlKDEuMDAxOTgpIiBmaWxsPSIjZmZmIj48L3BhdGg+PC9zdmc+');
        background-repeat: no-repeat;
        background-position: center;
        background-size: 15px;
    }


**And that's it.**

For easier reference the entire CSS file can be found below:


    .radio-label {
        background: white;
        border: 1px solid #eee;
        border-radius: 5px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        cursor: pointer;
        display: inline-block;
        font-weight: 600;
        margin: 0 auto 10px;
        padding: 20px 20px 20px 65px;
        position: relative;
        transition: .3s ease all;
        width: 100%;
    }
    .radio-label:hover {
        box-shadow: 0 4px 8px rgba(0,0,0,0.05);
    }
    .radio-label:before {
        background: #eee;
        border-radius: 50%;
        content:'';
        height: 30px;
        left: 20px;
        position: absolute;
        top: calc(50% - 15px);
        transition: .3s ease background-color;
        width: 30px;
    }
    .radio-label span {
        -webkit-user-select: none;
        -moz-user-select: none;
        user-select: none;
    }
    .radio-btn {
        position: absolute;
        visibility: hidden;
    }
    .radio-btn:checked + .radio-label {
        background: #ECF5FF;
        border-color: #4A90E2;
    }
    .radio-btn:checked + .radio-label:before {
        background-color: #4A90E2;
        background-image:  url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIHdpZHRoPSIyNiIgaGVpZ2h0PSIyMCIgdmVyc2lvbj0iMS4xIi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LjY5MTk2NCAwLjQ2ODc1LDEuMTM4MzlaIiB0cmFuc2Zvcm09InNjYWxlKDEuMDAxOTgpIiBmaWxsPSIjZmZmIj48L3BhdGg+PC9zdmc+');
        background-repeat: no-repeat;
        background-position: center;
        background-size: 15px;
    }


- - -

## But wait - we can get even fancier!

Since this demo is based off a survey-type questionaire, wouldn't it be interesting to give the different selectable options their own styling based on their context? Take a look at the further customized version below:

We can do so by adding `positive`, `neutral` and `negative` class names to the radio inputs with their own respective properties:


    .radio-btn.positive:checked + .radio-label {
        background: #EAFFF6;
        border-color: #32B67A;
    }
    .radio-btn.positive:checked + .radio-label:before {
        background-color: #32B67A;
    }
    .radio-btn.neutral:checked + .radio-label:before {
        background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAtMTUgMzAgOC41NzE0MyIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PCEtLUdlbmVyYXRlZCBieSBJSlNWRyAoaHR0cHM6Ly9naXRodWIuY29tL2ljb25qYXIvSUpTVkcpLS0+PHBhdGggZD0iTTMwLC0xMi4zMjE0djMuMjE0MjljMCwwLjczNjYwNyAtMC4yNjIyNzcsMS4zNjcxOSAtMC43ODY4MywxLjg5MTc0Yy0wLjUyNDU1NCwwLjUyNDU1NCAtMS4xNTUxMywwLjc4NjgzMSAtMS44OTE3NCwwLjc4NjgzMWgtMjQuNjQyOWMtMC43MzY2MDcsMCAtMS4zNjcxOSwtMC4yNjIyNzcgLTEuODkxNzQsLTAuNzg2ODMxYy0wLjUyNDU1MywtMC41MjQ1NTMgLTAuNzg2ODMsLTEuMTU1MTMgLTAuNzg2ODMsLTEuODkxNzR2LTMuMjE0MjljMCwtMC43MzY2MDcgMC4yNjIyNzcsLTEuMzY3MTkgMC43ODY4MywtMS44OTE3NGMwLjUyNDU1NCwtMC41MjQ1NTMgMS4xNTUxMywtMC43ODY4MyAxLjg5MTc0LC0wLjc4NjgzaDI0LjY0MjljMC43MzY2MDcsMCAxLjM2NzE5LDAuMjYyMjc3IDEuODkxNzQsMC43ODY4M2MwLjUyNDU1MywwLjUyNDU1NCAwLjc4NjgzLDEuMTU1MTMgMC43ODY4MywxLjg5MTc0WiIgZmlsbD0iI2ZmZiI+PC9wYXRoPjwvc3ZnPg==');
    }
    .radio-btn.negative:checked + .radio-label {
        background: #FFF2F2;
        border-color: #E75153;
    }
    .radio-btn.negative:checked + .radio-label:before {
        background-color: #E75153;
        background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCIgdmVyc2lvbj0iMS4xIiB2aWV3Qm94PSIxLjg1MTg1IC0zOS42OTcgMjAgMjAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPjwhLS1HZW5lcmF0ZWQgYnkgSUpTVkcgKGh0dHBzOi8vZ2l0aHViLmNvbS9pY29uamFyL0lKU1ZHKS0tPjxwYXRoIGQ9Ik0yMS43Mjk5LC0yMy40NzFjMCwwLjQ0NjQyOCAtMC4xNTYyNSwwLjgyNTg5MyAtMC40Njg3NSwxLjEzODM5bC0yLjI3Njc5LDIuMjc2NzljLTAuMzEyNSwwLjMxMjUgLTAuNjkxOTY0LDAuNDY4NzUgLTEuMTM4MzksMC40Njg3NWMtMC40NDY0MjgsMCAtMC44MjU4OTMsLTAuMTU2MjUgLTEuMTM4MzksLTAuNDY4NzVsLTQuOTIxODcsLTQuOTIxODhsLTQuOTIxODgsNC45MjE4OGMtMC4zMTI1LDAuMzEyNSAtMC42OTE5NjQsMC40Njg3NSA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ZmZiI+PC9wYXRoPjwvc3ZnPg==');
    }


I hope this shows new designers that simple custom radio inputs aren't so hard to implement after-all and can actually be pretty fun to design.