Animated Checkbox Styling with CSS

CSSCSSBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to use CSS to create custom checkboxes with animation on state change. We will use an SVG element to create the check symbol, flexbox to layout the checkboxes, and CSS animations to create a zoom effect. By the end of this lab, you will have the skills to create stylish and interactive checkboxes for your web projects.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL css(("`CSS`")) -.-> css/BasicConceptsGroup(["`Basic Concepts`"]) css(("`CSS`")) -.-> css/BasicStylingGroup(["`Basic Styling`"]) css(("`CSS`")) -.-> css/CoreLayoutGroup(["`Core Layout`"]) css(("`CSS`")) -.-> css/AdvancedLayoutGroup(["`Advanced Layout`"]) css(("`CSS`")) -.-> css/IntermediateStylingGroup(["`Intermediate Styling`"]) css(("`CSS`")) -.-> css/DynamicStylingGroup(["`Dynamic Styling`"]) css(("`CSS`")) -.-> css/CSSPreprocessorsGroup(["`CSS Preprocessors`"]) css/BasicConceptsGroup -.-> css/selectors("`Selectors`") css/BasicStylingGroup -.-> css/colors("`Colors`") css/CoreLayoutGroup -.-> css/box_model("`Box Model`") css/CoreLayoutGroup -.-> css/margin_and_padding("`Margin and Padding`") css/CoreLayoutGroup -.-> css/borders("`Borders`") css/CoreLayoutGroup -.-> css/width_and_height("`Width and Height`") css/CoreLayoutGroup -.-> css/display_property("`Display Property`") css/CoreLayoutGroup -.-> css/positioning("`Positioning`") css/AdvancedLayoutGroup -.-> css/flexbox("`Flexbox`") css/IntermediateStylingGroup -.-> css/backgrounds("`Backgrounds`") css/DynamicStylingGroup -.-> css/animations("`Animations`") css/DynamicStylingGroup -.-> css/transitions("`Transitions`") css/DynamicStylingGroup -.-> css/transformations("`Transformations`") css/CSSPreprocessorsGroup -.-> css/mixins("`Mixins`") css/IntermediateStylingGroup -.-> css/pseudo_classes("`Pseudo-classes`") subgraph Lab Skills css/selectors -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/colors -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/box_model -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/margin_and_padding -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/borders -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/width_and_height -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/display_property -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/positioning -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/flexbox -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/backgrounds -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/animations -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/transitions -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/transformations -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/mixins -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} css/pseudo_classes -.-> lab-35185{{"`Animated Checkbox Styling with CSS`"}} end

Custom Checkbox

index.html and style.css have already been provided in the VM.

To create a styled checkbox with animation on state change:

  1. Create a check symbol as an <svg> element with a <symbol> element inside. Use the <use> element to insert it as a reusable SVG icon.
  2. Create a container div with a class of .checkbox-container. Use flexbox to create the appropriate layout for the checkboxes.
  3. Hide the <input> element and associate a label with it to display a checkbox and the provided text.
  4. To animate the check symbol on state change, use stroke-dashoffset.
  5. To create a zoom animation effect, use transform: scale(0.9) via a CSS animation.

HTML:

<svg class="checkbox-symbol">
  <symbol id="check" viewbox="0 0 12 10">
    <polyline
      points="1.5 6 4.5 9 10.5 1"
      stroke-linecap="round"
      stroke-linejoin="round"
      stroke-width="2"
    ></polyline>
  </symbol>
</svg>

<div class="checkbox-container">
  <input class="checkbox-input" id="apples" type="checkbox" />
  <label class="checkbox" for="apples">
    <span>
      <svg width="12px" height="10px">
        <use xlink:href="#check"></use>
      </svg>
    </span>
    <span>Apples</span>
  </label>
  <input class="checkbox-input" id="oranges" type="checkbox" />
  <label class="checkbox" for="oranges">
    <span>
      <svg width="12px" height="10px">
        <use xlink:href="#check"></use>
      </svg>
    </span>
    <span>Oranges</span>
  </label>
</div>

CSS:

.checkbox-symbol {
  position: absolute;
  width: 0;
  height: 0;
  pointer-events: none;
  user-select: none;
}

.checkbox-container {
  box-sizing: border-box;
  background: #ffffff;
  color: #222;
  height: 64px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: row wrap;
}

.checkbox-container * {
  box-sizing: border-box;
}

.checkbox-input {
  position: absolute;
  visibility: hidden;
}

.checkbox {
  user-select: none;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 6px;
  overflow: hidden;
  transition: all 0.3s ease;
  display: flex;
}

.checkbox:not(:last-child) {
  margin-right: 6px;
}

.checkbox:hover {
  background: rgba(0, 119, 255, 0.06);
}

.checkbox span {
  vertical-align: middle;
  transform: translate3d(0, 0, 0);
}

.checkbox span:first-child {
  position: relative;
  flex: 0 0 18px;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  transform: scale(1);
  border: 1px solid #cccfdb;
  transition: all 0.3s ease;
}

.checkbox span:first-child svg {
  position: absolute;
  top: 3px;
  left: 2px;
  fill: none;
  stroke: #fff;
  stroke-dasharray: 16px;
  stroke-dashoffset: 16px;
  transition: all 0.3s ease;
  transform: translate3d(0, 0, 0);
}

.checkbox span:last-child {
  padding-left: 8px;
  line-height: 18px;
}

.checkbox:hover span:first-child {
  border-color: #0077ff;
}

.checkbox-input:checked + .checkbox span:first-child {
  background: #0077ff;
  border-color: #0077ff;
  animation: zoom-in-out 0.3s ease;
}

.checkbox-input:checked + .checkbox span:first-child svg {
  stroke-dashoffset: 0;
}

@keyframes zoom-in-out {
  50% {
    transform: scale(0.9);
  }
}

Please click on 'Go Live' in the bottom right corner to run the web service on port 8080. Then, you can refresh the Web 8080 Tab to preview the web page.

Summary

Congratulations! You have completed the Custom Checkbox lab. You can practice more labs in LabEx to improve your skills.

Other CSS Tutorials you may like