mirror of
https://github.com/phuoc-ng/csslayout.git
synced 2025-08-04 21:27:26 +02:00
192 lines
3.3 KiB
Plaintext
192 lines
3.3 KiB
Plaintext
---
|
|
category: Input
|
|
created: '2019-12-21'
|
|
description: Create a chip component with CSS flexbox
|
|
keywords: css chip, css flexbox, css tag
|
|
thumbnail: /assets/css-layout/thumbnails/chip.png
|
|
title: Chip
|
|
---
|
|
|
|
## HTML
|
|
|
|
```html index.html
|
|
<div class="chip">
|
|
<!-- Content -->
|
|
<div class="chip__content">...</div>
|
|
|
|
<!-- The close button -->
|
|
<button class="chip__button">
|
|
<div class="chip__button-line chip__button-line--first"></div>
|
|
<div class="chip__button-line chip__button-line--second"></div>
|
|
</button>
|
|
</div>
|
|
```
|
|
|
|
## CSS
|
|
|
|
```css styles.css
|
|
.chip {
|
|
/* Center the content */
|
|
align-items: center;
|
|
display: inline-flex;
|
|
justify-content: center;
|
|
|
|
/* Background color */
|
|
background-color: #d1d5db;
|
|
|
|
/* Rounded border */
|
|
border-radius: 9999px;
|
|
|
|
/* Spacing */
|
|
padding: 0.25rem 0.5rem;
|
|
}
|
|
|
|
.chip__content {
|
|
margin-right: 0.25rem;
|
|
}
|
|
```
|
|
|
|
The [close button](https://phuoc.ng/collection/css-layout/close-button/) is used to create a button for removing the chip:
|
|
|
|
```css
|
|
.chip__button {
|
|
/* Reset */
|
|
background-color: transparent;
|
|
border-color: transparent;
|
|
|
|
/* Cursor */
|
|
cursor: pointer;
|
|
|
|
/* Size */
|
|
height: 1rem;
|
|
width: 1rem;
|
|
|
|
/* Used to position the inner */
|
|
position: relative;
|
|
}
|
|
|
|
.chip__button-line {
|
|
/* Background color */
|
|
background-color: #9ca3af;
|
|
|
|
/* Position */
|
|
position: absolute;
|
|
|
|
/* Size */
|
|
height: 1px;
|
|
width: 100%;
|
|
}
|
|
|
|
.chip__button-line--first {
|
|
/* Position */
|
|
left: 0px;
|
|
top: 50%;
|
|
transform: translate(0%, -50%) rotate(45deg);
|
|
|
|
/* Size */
|
|
height: 1px;
|
|
width: 100%;
|
|
}
|
|
|
|
.chip__button-line--second {
|
|
/* Position */
|
|
left: 50%;
|
|
top: 0px;
|
|
transform: translate(-50%, 0%) rotate(45deg);
|
|
|
|
/* Size */
|
|
height: 100%;
|
|
width: 1px;
|
|
}
|
|
```
|
|
|
|
<Playground>
|
|
```css styles.css hidden
|
|
body {
|
|
align-items: center;
|
|
display: flex;
|
|
justify-content: center;
|
|
}
|
|
|
|
.chip {
|
|
/* Center the content */
|
|
align-items: center;
|
|
display: inline-flex;
|
|
justify-content: center;
|
|
|
|
/* Background color */
|
|
background-color: #d1d5db;
|
|
|
|
/* Rounded border */
|
|
border-radius: 9999px;
|
|
|
|
/* Spacing */
|
|
padding: 0.25rem 0.5rem;
|
|
}
|
|
|
|
.chip__content {
|
|
margin-right: 0.25rem;
|
|
}
|
|
|
|
.chip__button {
|
|
/* Reset */
|
|
background-color: transparent;
|
|
border-color: transparent;
|
|
|
|
/* Cursor */
|
|
cursor: pointer;
|
|
|
|
/* Size */
|
|
height: 1rem;
|
|
width: 1rem;
|
|
|
|
/* Used to position the inner */
|
|
position: relative;
|
|
}
|
|
|
|
.chip__button-line {
|
|
/* Background color */
|
|
background-color: #9ca3af;
|
|
|
|
/* Position */
|
|
position: absolute;
|
|
|
|
/* Size */
|
|
height: 1px;
|
|
width: 100%;
|
|
}
|
|
|
|
.chip__button-line--first {
|
|
/* Position */
|
|
left: 0px;
|
|
top: 50%;
|
|
transform: translate(0%, -50%) rotate(45deg);
|
|
|
|
/* Size */
|
|
height: 1px;
|
|
width: 100%;
|
|
}
|
|
|
|
.chip__button-line--second {
|
|
/* Position */
|
|
left: 50%;
|
|
top: 0px;
|
|
transform: translate(-50%, 0%) rotate(45deg);
|
|
|
|
/* Size */
|
|
height: 100%;
|
|
width: 1px;
|
|
}
|
|
```
|
|
|
|
```html index.html hidden
|
|
<div class="chip">
|
|
<div class="chip__content">CSS</div>
|
|
<button class="chip__button">
|
|
<div class="chip__button-line chip__button-line--first"></div>
|
|
<div class="chip__button-line chip__button-line--second"></div>
|
|
</button>
|
|
</div>
|
|
```
|
|
</Playground>
|