Skip to content

Custom Radio Buttons and Checkboxes with CSS

Accessible custom radio buttons and checkboxes with CSS are fairly straightforward to produce nowadays, but they do involve a fair bit of CSS.

The CSS features used for custom radio buttons and checkboxes include pseudo-elements, absolute/relative positioning, use of the appearance property, rounded corners, along with some borders and transforms to create a checkmark shape. The code below is the base styles for the radio buttons and checkboxes, but the demo includes some extra styles for aesthetic reasons.

I’ve included some comments in the code to indicate what each part does, should you need to customize it.

/* Context for relative positioning */
label {
  position: relative;
}

/* Base styles for both types of inputs */
input[type="radio"],
input[type="checkbox"] {
  appearance: none;
  background: #fff;
  border: 2px solid #777;
  height: 1.5em;
  width: 1.5em;
  border-radius: 100%;
  vertical-align: text-bottom;
  position: relative;
}

/* Remove the circular shape from checkboxes */
input[type="checkbox"] {
  border-radius: 0;
}

/* Styles for the pseudo-elements */
input[type="radio"]::before,
input[type="checkbox"]::before {
  content: "";
  position: absolute;
  margin: auto;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  top: 0;
}

/* Center of the checked radio button */
input[type="radio"]:checked::before {
  border: 5px solid transparent;
  border-radius: 100%;
  background: hotpink;
  margin: 4px;
}

/* The checkmark shape */
input[type="checkbox"]:checked::before {
  border-right: 5px solid hotpink;
  border-bottom: 6px solid hotpink;
  height: 90%;
  width: 30%;
  transform: rotate(50deg) translateY(-20%) translateX(-10%);
}

/* Some focus styles for accessibility */
input[type="radio"]:focus,
input[type="checkbox"]:focus {
  outline: solid 1px;
  outline-offset: 2px;
}
Code language: CSS (css)

For a full discussion of this topic along with some notes on accessibility, check out Scott O’Hara’s post and his full interactive demo. Full demo for this snippet is below.

Note: To the best of our knowledge, the information above and the snippet are accurate and up to date. However, in case you notice something wrong, please report snippet or leave a comment below.
View all Snippets
Louis Lazaris
Share:

0 Comments
Inline Feedbacks
View all comments

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!

0
Would love your thoughts, please comment.x