Skip to content

Instantly share code, notes, and snippets.

@AnonyFriday
Last active March 2, 2022 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnonyFriday/7a48773d4a9a859ad1e814cb281871e1 to your computer and use it in GitHub Desktop.
Save AnonyFriday/7a48773d4a9a859ad1e814cb281871e1 to your computer and use it in GitHub Desktop.
CSS Switch
!<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./css/index.css">
</head>
<body>
<label for="switch">
<input type="checkbox" name="" id="switch">
<span class="switch--bg">
<span></span>
</span>
</label>
</body>
</html>
*,
*::after,
*::before {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html,
body {
height: 100vh;
}
.switch {
display: inline-block;
input {
display: none;
}
&--bg {
display: inline-block;
width: 100px;
height: 50px;
background-color: aquamarine;
border-radius: 25px;
position: relative;
span {
display: inline-block;
position: absolute;
right: 4px;
left: auto;
top: 50%;
transform: translateY(-50%);
width: 40px;
height: 40px;
border-radius: 20px;
background-color: red;
}
}
// :checked state
input:checked + &--bg {
background-color: gray;
}
input:checked + &--bg {
span {
left: 4px;
right: auto;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment