cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmloctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1 class="title">
  Topcoat Checkboxes
</h1>
<p>
  Iterating on a simple, portable, and accessible css only solution for styling checkboxes.
</p>

<h2>Normal</h2>

<label class="topcoat-checkbox">
  <input type="checkbox">
  <div class="topcoat-checkbox__checkmark"></div>
</label>

<br>
<br>

<label class="topcoat-checkbox">
  <input type="checkbox" disabled>
  <div class="topcoat-checkbox__checkmark"></div>
</label>  


<div class="spacer">
  <hr>
</div>

<h2>Accessibility</h2>
  
<p>
  The normal checkbox will work on modern browsers with modern screen readers. What follows is a bullet proof approach to make sure your checkbox is accessible for as many of your users as possible.
</p>
  
<label for="checkbox1" class="topcoat-checkbox">
  <input id="checkbox1" aria-label="checkbox with aria-label" type="checkbox" aria-describedby="checkbox1-description">
  <div class="topcoat-checkbox__checkmark"></div>
</label>

<br>
<br>

<label for="checkbox2" class="topcoat-checkbox">
  label
  <input id="checkbox2" type="checkbox" aria-describedby="checkbox2-description">
  <div class="topcoat-checkbox__checkmark"></div>
</label>

<br>
<br>

<label for="checkbox3" class="topcoat-checkbox">
  <input id="checkbox3" aria-labelledby="checkbox3-label" aria-describedby="checkbox3-description" type="checkbox">
  <div class="topcoat-checkbox__checkmark"></div>
  <span id="checkbox3-label">label</span> 
</label>

<br>
  
<p id="checkbox1-description">The first checkbox doesn't have a label in text, which isn't recommended, but, depending on context, could be necessary, e.g. for a checkbox in a datagrid. We use the <code class="prettyprint">aria-label</code> attribute to make sure that the checkbox is properly labelled for assistive technology like screen readers.</p>

<p id="checkbox2-description">The second checkbox uses the <code class="prettyprint">for</code> attribute to explicitly associate the <code class="prettyprint">label</code> tag with the checkbox.</p>

<p id="checkbox3-description">The third checkbox uses the <code class="prettyprint">aria-labelledby</code> attribute to explicitly associate the checkbox with a <code class="prettyprint">span</code> that provides its label and the <code class="prettyprint">aria-describedby</code> attribute to associate the checkbox with this description.</p>

<p>To ensure that assistive technology knows the location of the checkbox, we don't use <pre class="prettyprint">
  input[type=checkbox] {
    position: absolute; 
    top: -9999px; 
    left: -9999px;
  }</pre> to hide  the checkbox offscreen. Instead we use the following, inspired by <a href="https://github.com/jquery/jquery-ui/blob/master/themes/base/jquery.ui.core.css#LC17">jQuery UI's .ui-helper-hidden-accessible class</a>, to hide the checkbox but retain its dimensions and position in the document.
  <pre class="prettyprint">
    input[type=checkbox] {
      border: 0;
      clip: rect(0 0 0 0);
      height: 2rem;
      margin: 0 -2rem -2rem 0;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 2rem;
      opacity: 0.001;
    }</pre>
</p>
  
<script src="//use.edgefonts.net/source-sans-pro:n3,n4,n6;source-code-pro:n3.js"></script>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
            
          
!
            
              body {
  padding: 0 50px;
  background-color: #f4f4f4;
  font-family: source-sans-pro, sans-serif;
}

.title {
  letter-spacing: -.065em
}

.spacer {
  padding: 25px 0;
}

input[type=checkbox] {
  border: 0;
	clip: rect(0 0 0 0);
	height: 2rem;
	margin: 0 -2rem -2rem 0;
	overflow: hidden;
	padding: 0;
	position: absolute;
	width: 2rem;
  opacity: 0.001;
}

.topcoat-checkbox {
  position: relative;
  display: inline-block;
  line-height: 2rem;
  
  -webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}

.topcoat-checkbox__checkmark {
  display: inline-block;
  vertical-align: top;
  width: 2rem;
  height: 2rem;
  
  -webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select: none;
	user-select: none;
}

/* Background */
.topcoat-checkbox__checkmark:before {
  content: '';
  position: absolute;
  box-sizing: border-box;
	width: 2rem;
	height: 2rem;
  background: #e5e9e9;
	border: 1px solid #a5a8a8;
	border-radius: 3px;
  cursor: pointer;
  
	-webkit-box-shadow: inset 0 1px #fff;
	box-shadow: inset 0 1px #fff;
}

/* Checkmark */
.topcoat-checkbox__checkmark:after {
  opacity: 0;
	content: '';
	position: absolute;
  /* Length of check tail */
  width: 28px; 
  /* Lenght of check foot */
  height: 11px;
	background: transparent;
	border: 7px solid #666666;
	border-top: none;
	border-right: none;
	border-radius: 2px;

	-webkit-transform: rotate(-50deg);
	-moz-transform: rotate(-50deg);
	-o-transform: rotate(-50deg);
	-ms-transform: rotate(-50deg);
	transform: rotate(-50deg);
}

:checked + .topcoat-checkbox__checkmark:after {
	opacity: 1;
}

:focus + .topcoat-checkbox__checkmark:before {
  border: 1px solid #0940fd;
  -webkit-box-shadow: 0 0 0 2px #6fb5f1;
  box-shadow: 0 0 0 2px #6fb5f1;
}

:disabled + .topcoat-checkbox__checkmark:before {
  opacity: 0.3;
  cursor: default;
  pointer-events: none;
}

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console