1
0
mirror of https://github.com/phuoc-ng/csslayout.git synced 2025-08-12 17:14:19 +02:00

feat: Update contents

This commit is contained in:
Phuoc Nguyen
2023-08-19 19:27:15 +07:00
parent f7af6da9e4
commit 71d51b358a
318 changed files with 15536 additions and 6884 deletions

151
contents/switch.mdx Normal file
View File

@@ -0,0 +1,151 @@
---
category: Input
created: '2019-11-17'
description: Create a switch with CSS flexbox
keywords: css custom checkbox, css flexbox, css switch, css switcher
thumbnail: /assets/css-layout/thumbnails/switch.png
title: Switch
---
The checkbox is placed inside a label. So when clicking on the label, the checkbox will be checked even though it's hidden.
## HTML
```html
<label class="switch switch--on">
<input type="checkbox" class="switch__input" />
<!-- Circle -->
<div class="switch__circle"></div>
</label>
<label class="switch switch--off">
<input type="checkbox" class="switch__input" />
<!-- Circle -->
<div class="switch__circle"></div>
</label>
```
## CSS
```css
.switch {
display: flex;
/* Rounded border */
border-radius: 9999px;
/* Size */
height: 2rem;
width: 4rem;
/* Demo */
margin-bottom: 0.5rem;
}
.switch__input {
/* Hide the input */
display: none;
}
.switch__circle {
/* Rounded border */
border-radius: 9999px;
/* Size */
width: 2rem;
background-color: #fff;
}
```
The switch comes with two variants:
```css
/* ON status */
.switch--on {
background-color: #357edd;
border: 1px solid #357edd;
/* Push the circle to the right */
justify-content: flex-end;
}
/* OFF status */
.switch--off {
background-color: #d1d5db;
border: 1px solid #d1d5db;
}
.switch--off .switch__circle {
border: 1px solid #d1d5db;
}
```
<Playground>
```css styles.css hidden
body {
align-items: center;
display: flex;
justify-content: center;
}
.switch {
display: flex;
/* Rounded border */
border-radius: 9999px;
/* Size */
height: 2rem;
width: 4rem;
/* Demo */
margin-bottom: 0.5rem;
}
.switch__input {
/* Hide the input */
display: none;
}
.switch__circle {
/* Rounded border */
border-radius: 9999px;
/* Size */
width: 2rem;
background-color: #fff;
}
/* ON status */
.switch--on {
background-color: #357edd;
border: 1px solid #357edd;
/* Push the circle to the right */
justify-content: flex-end;
}
/* OFF status */
.switch--off {
background-color: #d1d5db;
border: 1px solid #d1d5db;
}
.switch--off .switch__circle {
border: 1px solid #d1d5db;
}
```
```html index.html hidden
<label class="switch switch--off">
<input type="checkbox" class="switch__input" />
<div class="switch__circle"></div>
</label>
<label class="switch switch--on">
<input type="checkbox" class="switch__input" />
<div class="switch__circle"></div>
</label>
```
</Playground>