Copied SVG to clipboard
Something went wrong
Copied code to clipboard
Something went wrong

Default

User image

Default

Name

  • Osmo Discount
    -25%
The Vault/

Toggle (CSS Only Trick)

Toggle (CSS Only Trick)

Documentation

Webflow

Code

Setup: External Scripts

External Scripts in Webflow

Make sure to always put the External Scripts before the Javascript step of the resource.

In this video you learn where to put these in your Webflow project? Or how to include a paid GSAP Club plugin in your project?

HTML

Copy

Step 1: Copy structure to Webflow

Copy structure to Webflow

In the video below we described how you can copy + paste the structure of this resource to your Webflow project.

Copy to Webflow

Webflow structure is not required for this resource.

Step 1: Add HTML

HTML

Copy
<section class="section-resource">
  <div class="btn-toggle">
    <div class="btn-toggle__toggle">
      <div class="btn-toggle__toggle-dot"></div>
    </div><p class="btn-toggle__p">I'm a toggle</p>
    <input type="checkbox" class="btn-toggle__checkbox">
  </div>
</section>

HTML structure is not required for this resource.

Step 2: Add CSS

CSS

Copy
.section-resource {
  background-color: #e4d8d5;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  font-size: 4em;
  display: flex;
  transition: background-color 0.4s cubic-bezier(0.35, 1, 0.6, 1);
}

.btn-toggle {
  grid-column-gap: .5em;
  grid-row-gap: .5em;
  cursor: pointer;
  align-items: center;
  display: flex;
  position: relative;
}

.btn-toggle__toggle {
  background-color: #ff4c24;
  border-radius: 2em;
  flex-shrink: 0;
  width: 2em;
  height: 1.25em;
  display: flex;
}

.btn-toggle__toggle-dot {
  background-color: #fff;
  border-radius: 50%;
  width: 1em;
  height: 1em;
  margin-top: .125em;
  margin-left: .125em;
  transition: all 0.4s cubic-bezier(0.35, 1.5, 0.6, 1);
  transform: translateX(0em) rotate(0.001deg);
}

.btn-toggle__p {
  margin-bottom: 0;
  font-size: 1.25em;
  line-height: 1;
  position: relative;
  transition: color 0.4s cubic-bezier(0.35, 1, 0.6, 1);
}

.btn-toggle__checkbox {
  opacity: 0;
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 100%;
  position: absolute;
  cursor: pointer;
}

@media screen and (max-width: 767px) {
  .section-resource {
    font-size: 7vw;
  }
}

/* Animations based on :checked */

.section-resource:has(input[type="checkbox"]:checked) {
  background: #301139;
}

.btn-toggle:has(input[type="checkbox"]:checked) .btn-toggle__toggle-dot {
  transform: translateX(0.75em) rotate(0.001deg);
}

.btn-toggle:has(input[type="checkbox"]:checked) .btn-toggle__p {
  color: #EFEEEC;
}

Step 2: Add custom Javascript

Custom Javascript in Webflow

In this video, Ilja gives you some guidance about using JavaScript in Webflow:

Step 2: Add Javascript

Step 3: Add Javascript

Javascript

Copy

Step 3: Add custom CSS

Step 2: Add custom CSS

Custom CSS in Webflow

Curious about where to put custom CSS in Webflow? Ilja explains it in the below video:

CSS

Copy
/* Background Color */
.section-resource {
  transition: background-color 0.4s cubic-bezier(0.35, 1, 0.6, 1);
}

.section-resource:has(input[type="checkbox"]:checked) {
  background: #301139;
}

/* Toggle Dot */
.btn-toggle__toggle-dot {
  transition: all 0.4s cubic-bezier(0.35, 1.5, 0.6, 1);
  transform: translateX(0em) rotate(0.001deg);
}

.btn-toggle:has(input[type="checkbox"]:checked) .btn-toggle__toggle-dot {
  transform: translateX(0.75em) rotate(0.001deg);
}

/* Toggle Text */
.btn-toggle__p {
  transition: color 0.4s cubic-bezier(0.35, 1, 0.6, 1);
}

.btn-toggle:has(input[type="checkbox"]:checked) .btn-toggle__p {
  color: #EFEEEC;
}

Implementation

In this example we animate the Toggle Dot, Background and Text color. But you can go as crazy as you want. Only possible because of the :has() CSS selector.

Resource Details

CSS
Only
Trick
:has selector
Basic
Toggle
Animation
Button

Original source

Dennis Snellenberg

Creator Credits

We always strive to credit creators as accurately as possible. While similar concepts might appear online, we aim to provide proper and respectful attribution.