Pure CSS Custom Checkbox Style

Pure CSS Custom Checkbox Style
Code Snippet:Accessible Custom Checkboxes with CSS only
Author: Geoffrey Crofte
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 513
License: MIT
Edit Code online: View on CodePen
Read More

This pure CSS code snippet helps you to style custom checkbox. It comes with modern styles on modern browsers and classical checkboxes for IE8 and lower. The code hides the original checkbox and creates custom checkbox styles for improved accessibility and design. It’s a helpful solution for elevating your checkbox design.

Plus, it maintains compatibility with older browsers like IE8 for a seamless design across all platforms. You can use this code on your website to style checkboxes for more modern look.

How to Create Pure CSS Custom Checkbox Style

1. Begin by creating your HTML structure. Add checkboxes within <input> elements and their corresponding labels with <label> elements. Each checkbox should have a unique id attribute, and the for attribute of the label should match the checkbox’s id.

<h1>Accessible Custom checkboxes with CSS only</h1>
<h2>"automatic" fallback for IE</h2>


<form action="#">
  <p>
    <input type="checkbox" id="test1" />
    <label for="test1">Red</label>
  </p>
  <p>
    <input type="checkbox" id="test2" checked="checked" />
    <label for="test2">Yellow</label>
  </p>
  <p>
    <input type="checkbox" id="test3" checked="checked" disabled="disabled" />
    <label for="test3">Green</label>
  </p>
    <p>
      <input type="checkbox" id="test4" disabled="disabled" />
      <label for="test4">Brown</label>
  </p>
</form>

<p class="txtcenter">
  <a class="btn" href="https://www.creativejuiz.fr/blog/en/tutorials/customize-checkbox-and-radio-button-with-css">Read the article</a>
</p>

<p class="txtcenter">Custom styles on modern browsers.<br/>Classical checkboxes on IE8 and lower.</p>

<p class="txtcenter copy">by <a href="https://twitter.com/geoffreycrofte">@geoffreycrofte</a><br />see also <a href="https://codepen.io/CreativeJuiz/pen/zqKtp">Flat UI Styles for Checkboxes</a></p>

2. Copy and paste the following CSS code into your project. This CSS code defines the styles for both checked and unchecked checkboxes and handles various states like disabled and focused checkboxes.

/* Base for label styling */
	[type="checkbox"]:not(:checked),
	[type="checkbox"]:checked {
		position: absolute;
		left: 0;
		opacity: 0.01;
	}
	[type="checkbox"]:not(:checked) + label,
	[type="checkbox"]:checked + label {
		position: relative;
		padding-left: 2.3em;
		font-size: 1.05em;
		line-height: 1.7;
		cursor: pointer;
	}

	/* checkbox aspect */
	[type="checkbox"]:not(:checked) + label:before,
	[type="checkbox"]:checked + label:before {
		content: '';
		position: absolute;
		left: 0;
		top: 0;
		width: 1.4em;
		height: 1.4em;
		border: 1px solid #aaa;
		background: #FFF;
		border-radius: .2em;
		box-shadow: inset 0 1px 3px rgba(0,0,0, .1), 0 0 0 rgba(203, 34, 237, .2);
		-webkit-transition: all .275s;
				transition: all .275s;
	}

	/* checked mark aspect */
	[type="checkbox"]:not(:checked) + label:after,
	[type="checkbox"]:checked + label:after {
		content: '✕';
		position: absolute;
		top: .525em;
		left: .18em;
		font-size: 1.375em;
		color: #CB22ED;
		line-height: 0;
		-webkit-transition: all .2s;
				transition: all .2s;
	}

	/* checked mark aspect changes */
	[type="checkbox"]:not(:checked) + label:after {
		opacity: 0;
		-webkit-transform: scale(0) rotate(45deg);
				transform: scale(0) rotate(45deg);
	}

	[type="checkbox"]:checked + label:after {
		opacity: 1;
		-webkit-transform: scale(1) rotate(0);
				transform: scale(1) rotate(0);
	}

	/* Disabled checkbox */
	[type="checkbox"]:disabled:not(:checked) + label:before,
	[type="checkbox"]:disabled:checked + label:before {
		box-shadow: none;
		border-color: #bbb;
		background-color: #e9e9e9;
	}

	[type="checkbox"]:disabled:checked + label:after {
		color: #777;
	}

	[type="checkbox"]:disabled + label {
		color: #aaa;
	}

	/* Accessibility */
	[type="checkbox"]:checked:focus + label:before,
	[type="checkbox"]:not(:checked):focus + label:before {
		box-shadow: inset 0 1px 3px rgba(0,0,0, .1), 0 0 0 6px rgba(203, 34, 237, .2);
	}




/*
 * Useless styles, just for demo design
 */
body {
  font-family: "Open Sans", "Segoe WP", "Segoe UI", Helvetica, Arial, sans-serif;
  text-align: center;
  color: #34495E;
    background: #FCFDFD;
}

a[href^="https://www.creativejuiz"] {
  color: #34495E;
  text-decoration: underline;
}

h1 {
  margin-top: 1em;
}

h2 {
  margin-top: 0;
  margin-bottom: 2em;
  color: #CB22ED;
  font-weight: normal;
}

form {
  width: 120px;
  margin: 0 auto 55px;
  text-align: left;
}

div p:first-child {
  font-weight: bold;
  font-size: 1.2em;
}

p { 
  color: #aaa;
}

p a {
  color: inherit;
}

p + p {
  margin-top: 3em;
}

form p {
  margin: 15px 0;
  color: #34495E;
}

a[href^="https://twitter.com"] {
  color: #1da1f2;
}

.btn.btn {
  display: inline-block;
  padding: 8px 24px;
  text-decoration: none;
  border-radius: 40px;
  background: #34495E;
  color: #F2F2F2;
  transition: all .4s;
}

.btn.btn:hover,
.btn.btn:focus {
  background: #000;
}

The CSS code includes styles for the checkboxes’ appearance, label positioning, and checked mark. Customize these styles to match your website’s design by modifying properties like color, size, and border-radius.

That’s all! hopefully, you have successfully created Custom Checkboxes in your project. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X