Styling checkboxes and radio buttons without JavaScript
Tags: css, webdev, webdesign, codenewbie
Published: 2020-07-21 19:22:14

---
title: Styling checkboxes and radio buttons without JavaScript
published: true
date: 2020-07-21 19:11:12 UTC
tags: css,webdevelopment,webdesign,codenewbie
canonical_url: 
---


Styling checkboxes and radio buttons without JavaScript
When it comes to web forms, checkboxes and radio buttons are elements that always appear. But in the environment when everything also needs to be beautiful, default ones aren't good enough. And it is a task with which many developers with less experience in CSS struggle. In this post, I am describing how to achieve it on the example of a radio button. 

[![](https://cdn-images-1.medium.com/max/870/1*fC1TiemRWYDWObQUlpAnBg.png)](https://medium.com/@kristijan.pajtasev/styling-checkboxes-and-radio-buttons-without-javascript-9dfe4e6b22ca?source=rss-e134e822ba27------2) 

## Default browser elements problems
Many problems can happen when using default HTML elements. The first one is different looks between different browsers. In general, you want to have the same design and behavior in all browsers. You want colors and sizes to match with the rest of your app. With default ones, that is not the case. In the images below, you can notice differences between radio buttons in Chrome and ones in Microsoft Edge. While the only difference is color, the difference is still apparent. And in other browsers could be something else.

![Chrome default element](https://dev-to-uploads.s3.amazonaws.com/i/5wgb9pypj0zumg3cvm3m.PNG)
![Microsoft Edge default element](https://dev-to-uploads.s3.amazonaws.com/i/y9ce8321ar8n9gm1bcd2.PNG)

The second big issue is that there are no CSS rules for them. That means that you can't style them directly. But there are different tricks for that, and that is the whole goal of this post.

## Solution overview
First, let's cover a few things I am using to get this done before going into code.

### Label

One thing that helps with this solution is the label tag, wrap your element into the label tab. When you do this, clicking anywhere on the label triggers click on the input element.

### Input

I am still using proper input types, depending on what I want to get. The display of the input element is hidden, and that is why clicking on the label is so essential for this solution. What makes the selected indicator visible is using before and after pseudo-selectors. These use absolute position, and because of it, the label needs to have a position defined as relative.

## Code
### Step 1
We need to set our basic structure. That is a form with two labels, each containing input radio.

![HTML structure](https://dev-to-uploads.s3.amazonaws.com/i/dj2yfkcr0kdf5i7nzzbz.PNG)

### Step 2
When we have our structure set, it is time for CSS—first, initial styling for the label. As said before, the label has to have a relative position. Because input elements are positioned absolute, there is left padding. Cursor styling and font size are just there for a bit better user experience, but that should be according to your needs.

![Label styling](https://dev-to-uploads.s3.amazonaws.com/i/d59qwn2tp8zvlvrd79ao.PNG)
 
### Step 3
Now we need to hide the default input element. Using display none properly is a solution. But that would make after and before content also hidden. That is why I am setting width and height to 0. It is there, just not visible. 

![Radio button stryling](https://dev-to-uploads.s3.amazonaws.com/i/3muvfnl8gpsj9jd6sqis.PNG)
 
### Step 4
Now we are almost there. Both before and after styling is mostly the same. Content needs to be defined, but it is an empty string. It inherits position and left and top properties values from default radio button styling. Width and height can be any value you want, but it is essential to have a display defined as inline-block. Without it, it just wouldn't be displayed. The border-radius rule is there to make a circle.

![Before and after styling](https://dev-to-uploads.s3.amazonaws.com/i/k1q29v3npcirfooflk4g.PNG)

After styling is almost the same as before. The main thing to notice is that it paired with checked pseudo-selector. The reason is that we only want to see this when the radio button is selected. Additional, and last difference is size and position. That is to have a smaller circle placed in the center of the larger one as an indicator of selected.


You can find a full and running example in following code pen:
{% codepen https://codepen.io/kristijan-pajtasev/pen/JjGwOOq %}


## Wrap up

There are many ways you can choose to do this. What this one gives you is that it is clean and only uses HTML and CSS and works in all browsers. No need for extra JavaScript logic. Here, I only showed it for making radio buttons simple circles, but by using the same approach, you can style checkboxes and make more complex ones. Maybe instead of using rounded borders, background images. All in an effortless way.

___

For more, you can follow me on [Twitter](https://twitter.com/hi_iam_chris_), [LinkedIn](https://www.linkedin.com/in/kpajtasev/), [GitHub](https://github.com/kristijan-pajtasev/) and [Instagram](https://www.instagram.com/hi_iam_chris_/).