search
HomeWeb Front-endCSS TutorialUse CSS3 to write code similar to check boxes and buttons with switches in iOS

This article mainly introduces the use of CSS3 to write check boxes and buttons with switches similar to those in iOS. Friends in need can refer to

checkbox multiple selection

Recently I wrote a checkbox suitable for mobile terminals, as shown in the picture:
2016411114913458.png (812×522)

ps: The checkbox in the middle is iconfont, iOS style.

Specific HTML:

<p class="mui-checkbox-con">
    <label>
        <input class="mui-checkbox" type="checkbox">默认未选中</label>
</p>
<p class="mui-checkbox-con">
    <label>
        <input class="mui-checkbox" type="checkbox" checked>默认选中</label>
</p>
<p class="mui-checkbox-con">
    <label>
        <input class="mui-checkbox checkbox-orange" type="checkbox" checked>橘黄色 checkbox-orange</label>
</p>
<p class="mui-checkbox-con">
    <label>
        <input class="mui-checkbox checkbox-green" type="checkbox" checked>绿色 checkbox-green</label>
</p>
<p class="mui-checkbox-con">
    <label>
        <input class="mui-checkbox" type="checkbox" disabled>禁用</label>
</p>

CSS code (exported by SCSS, the layout is a bit strange):

.mui-checkbox {   
  -webkit-appearance: none;   
  position: relative;   
  width: 25px;   
  height: 25px;   
  margin-right: 10px;   
  background-color: #FFFFFF;   
  border: solid 1px #d9d9d9;   
  border-top-left-radius: 20px;   
  border-top-rightright-radius: 20px;   
  border-bottom-left-radius: 20px;   
  border-bottom-rightright-radius: 20px;   
  background-clip: padding-box;   
  display: inline-block; }   
  .mui-checkbox:focus {   
    outline: 0 none;   
    outline-offset: -2px; }   
  .mui-checkbox:checked {   
    background-color: #18b4ed;   
    border: solid 1px #FFFFFF; }   
    .mui-checkbox:checked:before {   
      display: inline-block;   
      margin-top: 1px;   
      margin-left: 2px;   
      font-family: iconfont;   
      content: "\e667";   
      color: #FFFFFF;   
      font-size: 18px; }   
  .mui-checkbox:disabled {   
    background-color: #d9d9d9;   
    border: solid 1px #d9d9d9; }   
    .mui-checkbox:disabled:before {   
      display: inline-block;   
      margin-top: 1px;   
      margin-left: 2px;   
      font-family: iconfont;   
      content: "\e667";   
      color: #FFFFFF;   
      font-size: 18px; }   
  .mui-checkbox.checkbox-green:checked {   
    background-color: #5cb85c; }   
  .mui-checkbox.checkbox-orange:checked {   
    background-color: #f0ad4e; }   
  .mui-checkbox.checkbox-s {   
    width: 19px;   
    height: 19px; }   
    .mui-checkbox.checkbox-s:before {   
      display: inline-block;   
      margin-top: 1px;   
      margin-left: 2px;   
      font-family: iconfont;   
      content: "\e667";   
      color: #FFFFFF;   
      font-size: 13px; }   
.mui-checkbox-anim {   
  -webkit-transition: background-color ease 0.2s;   
          transition: background-color ease 0.2s; }

SCSS code:

@mixin checkedCon($fs:18px) {   
  &:before {   
    display: inline-block;   
    margin-top: 1px;   
    margin-left: 2px;   
    font-family: iconfont;   
    content: "\e667";   
    color: #FFFFFF;   
    font-size: $fs;   
  }   
}   
$duration: .4s;   
.mui-checkbox {   
  -webkit-appearance: none;   
  position: relative;   
  width: 25px;   
  height: 25px;   
  margin-right: 10px;   
  background-color: #FFFFFF;   
  border: solid 1px #d9d9d9;   
  border-top-left-radius: 20px;   
  border-top-rightright-radius: 20px;   
  border-bottom-left-radius: 20px;   
  border-bottom-rightright-radius: 20px;   
  background-clip: padding-box;   
  display: inline-block;   
  &:focus {   
    outline: 0 none;   
    outline-offset: -2px
  }   
  &:checked {   
    background-color: #18b4ed;   
    border: solid 1px #FFFFFF;   
    @include checkedCon();   
  }   
  &:disabled {   
    background-color: #d9d9d9;   
    border: solid 1px #d9d9d9;   
    @include checkedCon();   
  }   
  &.checkbox-green:checked {   
    background-color: #5cb85c;   
  }   
  &.checkbox-orange:checked {   
    background-color: #f0ad4e;   
  }   
  &.checkbox-s {   
    width: 19px;   
    height: 19px;   
    @include checkedCon(13px);   
  }   
}   
.mui-checkbox-anim{   
  //border等其他元素不做过渡效果,增加视觉差,更有动画效果   
  transition: background-color ease $duration/2;   
}

With switch switch
itself. The purpose of making this UI is to support mobile pages, and webkit also supports single-tagged input elements using pseudo-classes (:before or:after), so I didn’t make any changes. Much support and optimization, I just want to keep the html as clean as possible, so I don’t use other elements for simulation. If you want to use it on a desktop application or support other browsers, you can modify it slightly yourself. I haven't tested it anyway.

Today I continue to share an iOS-style switch button, which looks very common, as shown in the picture:
2016411115110047.png (818×350)

## Mainly uses
<label><input class="mui-switch" type="checkbox"> 默认未选中</label>
<label><input class="mui-switch" type="checkbox" checked> 默认选中</label>
<label><input class="mui-switch mui-switch-animbg" type="checkbox"> 默认未选中,简单的背景过渡效果,加mui-switch-animbg类即可</label>
<label><input class="mui-switch mui-switch-animbg" type="checkbox" checked> 默认选中</label>
<label><input class="mui-switch mui-switch-anim" type="checkbox"> 默认未选中,过渡效果,加 mui-switch-anim   
类即可</label>
<label><input class="mui-switch mui-switch-anim" type="checkbox" checked> 默认选中</label>
In actual use, two transition effects were later added, and the mui-switch-animbg and mui-switch-anim classes were added respectively. Check the specific effects. The demo page below.

CSS code (exported by SCSS, the layout is a bit strange):

.mui-switch {   
  width: 52px;   
  height: 31px;   
  position: relative;   
  border: 1px solid #dfdfdf;   
  background-color: #fdfdfd;   
  box-shadow: #dfdfdf 0 0 0 0 inset;   
  border-radius: 20px;   
  border-top-left-radius: 20px;   
  border-top-rightright-radius: 20px;   
  border-bottom-left-radius: 20px;   
  border-bottom-rightright-radius: 20px;   
  background-clip: content-box;   
  display: inline-block;   
  -webkit-appearance: none;   
  user-select: none;   
  outline: none; }   
  .mui-switch:before {   
    content: &#39;&#39;;   
    width: 29px;   
    height: 29px;   
    position: absolute;   
    top: 0px;   
    left: 0;   
    border-radius: 20px;   
    border-top-left-radius: 20px;   
    border-top-rightright-radius: 20px;   
    border-bottom-left-radius: 20px;   
    border-bottom-rightright-radius: 20px;   
    background-color: #fff;   
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); }   
  .mui-switch:checked {   
    border-color: #64bd63;   
    box-shadow: #64bd63 0 0 0 16px inset;   
    background-color: #64bd63; }   
    .mui-switch:checked:before {   
      left: 21px; }   
  .mui-switch.mui-switch-animbg {   
    transition: background-color ease 0.4s; }   
    .mui-switch.mui-switch-animbg:before {   
      transition: left 0.3s; }   
    .mui-switch.mui-switch-animbg:checked {   
      box-shadow: #dfdfdf 0 0 0 0 inset;   
      background-color: #64bd63;   
      transition: border-color 0.4s, background-color ease 0.4s; }   
      .mui-switch.mui-switch-animbg:checked:before {   
        transition: left 0.3s; }   
  .mui-switch.mui-switch-anim {   
    transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s; }   
    .mui-switch.mui-switch-anim:before {   
      transition: left 0.3s; }   
    .mui-switch.mui-switch-anim:checked {   
      box-shadow: #64bd63 0 0 0 16px inset;   
      background-color: #64bd63;   
      transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s; }   
      .mui-switch.mui-switch-anim:checked:before {   
        transition: left 0.3s; }   
/*# sourceMappingURL=mui-switch.css.map */

SCSS code:

@mixin borderRadius($radius:20px) {   
  border-radius: $radius;   
  border-top-left-radius: $radius;   
  border-top-rightright-radius: $radius;   
  border-bottom-left-radius: $radius;   
  border-bottom-rightright-radius: $radius;   
}   
$duration: .4s;   
$checkedColor: #64bd63;   
.mui-switch {   
  width: 52px;   
  height: 31px;   
  position: relative;   
  border: 1px solid #dfdfdf;   
  background-color: #fdfdfd;   
  box-shadow: #dfdfdf 0 0 0 0 inset;   
  @include borderRadius();   
  background-clip: content-box;   
  display: inline-block;   
  -webkit-appearance: none;   
  user-select: none;   
  outline: none;   
  &:before {   
    content: &#39;&#39;;   
    width: 29px;   
    height: 29px;   
    position: absolute;   
    top: 0px;   
    left: 0;   
    @include borderRadius();   
    background-color: #fff;   
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);   
  }   
  &:checked {   
    border-color: $checkedColor;   
    box-shadow: $checkedColor 0 0 0 16px inset;   
    background-color: $checkedColor;   
    &:before {   
      left: 21px;   
    }   
  }   
  &.mui-switch-animbg {   
    transition: background-color ease $duration;   
    &:before {   
      transition: left 0.3s;   
    }   
    &:checked {   
      box-shadow: #dfdfdf 0 0 0 0 inset;   
      background-color: $checkedColor;   
      transition: border-color $duration, background-color ease $duration;   
      &:before {   
        transition: left 0.3s;   
      }   
    }   
  }   
  &.mui-switch-anim {   
    transition: border cubic-bezier(0, 0, 0, 1) $duration, box-shadow cubic-bezier(0, 0, 0, 1) $duration;   
    &:before {   
      transition: left 0.3s;   
    }   
    &:checked {   
      box-shadow: $checkedColor 0 0 0 16px inset;   
      background-color: $checkedColor;   
      transition: border ease $duration, box-shadow ease $duration, background-color ease $duration*3;   
      &:before {   
        transition: left 0.3s;   
      }   
    }   
  }   
}

The above is the entire content of this article, I hope it will be helpful to everyone's study , please pay attention to the PHP Chinese website for more related content!

Related recommendations:

Analysis of scale scaling in transform of css3

About list-style modifying list attributes in CSS Problems with controlling the style of the li tag

About the code that uses ellipses to replace CSS text when it exceeds the div or span

The above is the detailed content of Use CSS3 to write code similar to check boxes and buttons with switches in iOS. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Modern Scroll Shadows Using Scroll-Driven AnimationsModern Scroll Shadows Using Scroll-Driven AnimationsMay 07, 2025 am 10:34 AM

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Revisiting Image MapsRevisiting Image MapsMay 07, 2025 am 09:40 AM

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

State of Devs: A Survey for Every DeveloperState of Devs: A Survey for Every DeveloperMay 07, 2025 am 09:30 AM

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more. 

What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft