1
0
mirror of https://github.com/phuoc-ng/csslayout.git synced 2025-08-04 13:17:48 +02:00
Files
csslayout/contents/dropdown.mdx
2023-10-06 18:20:10 +07:00

272 lines
5.8 KiB
Plaintext

---
category: Navigation
created: '2019-11-29'
description: Create a dropdown with CSS
keywords: css dropdown, css menu
thumbnail: /assets/css-layout/thumbnails/dropdown.png
title: Dropdown
---
## HTML
```html index.html
<div class="dropdown">
<!-- The trigger element -->
<div class="dropdown__trigger">...</div>
<!-- The content -->
<div class="dropdown__content">...</div>
</div>
```
## CSS
```css styles.css
.dropdown {
position: relative;
}
.dropdown__trigger {
cursor: pointer;
}
/* Hide the dropdown's content by default */
.dropdown__content {
display: none;
/* Position it right below the trigger element */
left: 0;
padding-top: 0.25rem;
position: absolute;
top: 100%;
/* It should be on the top of other elements */
background-color: #fff;
z-index: 9999;
}
/* Show the content when hover on the container */
.dropdown:hover .dropdown__content {
display: block;
}
```
You can use a [triangle button](https://phuoc.ng/collection/css-layout/triangle-buttons/) to indicate that there is content under it.
Move the mouse over the button to see the dropdown.
<Playground>
```css placeholders.css hidden
.lines {
padding: 0.25rem 0;
width: 100%;
align-items: center;
display: flex;
justify-content: center;
flex-direction: column;
}
.line {
background: #d1d5db;
height: 1px;
margin-bottom: 0.25rem;
}
.line.line--20 {
width: 20%;
}
.line.line--40 {
width: 40%;
}
.line.line--60 {
width: 60%;
}
.line.line--80 {
width: 80%;
}
.line.line--100 {
width: 100%;
}
.rectangle {
background: #d1d5db;
border-radius: 0.25rem;
height: var(--rectangle-height);
width: var(--rectangle-width);
}
.rectangle--hor.rectangle--20 {
--rectangle-width: 20%;
}
.rectangle--hor.rectangle--40 {
--rectangle-width: 40%;
}
.rectangle--hor.rectangle--60 {
--rectangle-width: 60%;
}
.rectangle--hor.rectangle--80 {
--rectangle-width: 80%;
}
.rectangle--hor.rectangle--100 {
--rectangle-width: 100%;
}
.rectangle--hor.rectangle--sm {
--rectangle-height: 0.5rem;
}
.rectangle--hor.rectangle--md {
--rectangle-height: 2rem;
}
.rectangle--hor.rectangle--lg {
--rectangle-height: 4rem;
}
.rectangle--ver.rectangle--20 {
--rectangle-height: 20%;
}
.rectangle--ver.rectangle--40 {
--rectangle-height: 40%;
}
.rectangle--ver.rectangle--60 {
--rectangle-height: 60%;
}
.rectangle--ver.rectangle--80 {
--rectangle-height: 80%;
}
.rectangle--ver.rectangle--100 {
--rectangle-height: 100%;
}
.rectangle--ver.rectangle--sm {
--rectangle-width: 0.5rem;
}
.rectangle--ver.rectangle--md {
--rectangle-width: 2rem;
}
.rectangle--ver.rectangle--lg {
--rectangle-width: 4rem;
}
.triangle {
border-style: solid;
height: 0;
width: 0;
}
.triangle--t {
border-color: transparent transparent #d1d5db transparent;
border-width: 0 var(--triangle-size) var(--triangle-size) var(--triangle-size);
}
.triangle--r {
border-color: transparent transparent transparent #d1d5db;
border-width: var(--triangle-size) 0 var(--triangle-size) 1rem;
}
.triangle--b {
border-color: #d1d5db transparent transparent transparent;
border-width: var(--triangle-size) var(--triangle-size) 0 var(--triangle-size);
}
.triangle--l {
border-color: transparent #d1d5db transparent transparent;
border-width: var(--triangle-size) 1rem var(--triangle-size) 0;
}
.triangle--tr {
border-color: transparent #d1d5db transparent transparent;
border-width: 0 var(--triangle-size) var(--triangle-size) 0;
}
.triangle--br {
border-color: transparent transparent #d1d5db transparent;
border-width: 0 0 var(--triangle-size) var(--triangle-size);
}
.triangle--bl {
border-color: transparent transparent transparent #d1d5db;
border-width: var(--triangle-size) 0 0 var(--triangle-size);
}
.triangle--tl {
border-color: #d1d5db transparent transparent transparent;
border-width: var(--triangle-size) var(--triangle-size) 0 0;
}
.triangle--sm {
--triangle-size: 0.5rem;
}
.triangle--md {
--triangle-size: 2rem;
}
.triangle--lg {
--triangle-size: 4rem;
}
```
```css styles.css hidden
body {
align-items: center;
display: flex;
justify-content: center;
height: 24rem;
}
.dropdown {
position: relative;
/* Demo */
width: 6rem;
align-items: flex-start;
display: flex;
justify-content: center;
}
.dropdown__trigger {
cursor: pointer;
/* Demo */
border: 1px solid #d1d5db;
border-radius: 0.25rem;
height: 2rem;
width: 6rem;
padding: 0.25rem 0.5rem;
align-items: center;
display: flex;
justify-content: center;
}
/* Hide the dropdown's content by default */
.dropdown__content {
display: none;
/* Position it right below the trigger element */
left: 0;
padding-top: 0.25rem;
position: absolute;
top: 100%;
/* It should be on the top of other elements */
background-color: #fff;
z-index: 9999;
}
.dropdown__body {
/* Demo */
border: 1px solid #d1d5db;
border-radius: 0.25rem;
height: 6rem;
width: 8rem;
}
/* Show the content when hover on the container */
.dropdown:hover .dropdown__content {
display: block;
}
```
```html index.html hidden
<div class="dropdown">
<div class="dropdown__trigger">
<div class="rectangle rectangle--hor rectangle--sm rectangle--100"></div>
<div class="triangle triangle--b triangle--sm"></div>
</div>
<div class="dropdown__content">
<div class="dropdown__body">
<div class="lines">
<div class="line line--80"></div>
<div class="line line--40"></div>
<div class="line line--100"></div>
<div class="line line--60"></div>
<div class="line line--20"></div>
</div>
</div>
</div>
</div>
```
</Playground>