HTML Code
<div class="container">
<div class="select-btn">
<span class="btn-text">Languages</span>
<span class="arrow-dwn">
<i class="fa-solid fa-chevron-down"></i>
</span>
</div>
<ul class="list-items">
<li class="item">
<span class="checkbox">
<i class="fa-solid fa-check check-icon"></i>
</span>
<span class="item-text">French</span>
</li>
<li class="item">
<span class="checkbox">
<i class="fa-solid fa-check check-icon"></i>
</span>
<span class="item-text">English</span>
</li>
<li class="item">
<span class="checkbox">
<i class="fa-solid fa-check check-icon"></i>
</span>
<span class="item-text">Spanish</span>
</li>
<li class="item">
<span class="checkbox">
<i class="fa-solid fa-check check-icon"></i>
</span>
<span class="item-text">Chinese</span>
</li>
<li class="item">
<span class="checkbox">
<i class="fa-solid fa-check check-icon"></i>
</span>
<span class="item-text">Japanese</span>
</li>
<li class="item">
<span class="checkbox">
<i class="fa-solid fa-check check-icon"></i>
</span>
<span class="item-text">Korean</span>
</li>
<li class="item">
<span class="checkbox">
<i class="fa-solid fa-check check-icon"></i>
</span>
<span class="item-text">Italian</span>
</li>
<li class="item">
<span class="checkbox">
<i class="fa-solid fa-check check-icon"></i>
</span>
<span class="item-text">German</span>
</li>
</ul>
</div>
CSS Code
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
.container{
position: relative;
max-width: 320px;
width: 100%;
margin: 80px auto 30px;
}
.select-btn{
display: flex;
height: 50px;
align-items: center;
justify-content: space-between;
padding: 0 16px;
border-radius: 8px;
cursor: pointer;
background-color: #fff;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
}
.select-btn .btn-text{
font-size: 17px;
font-weight: 400;
color: #333;
}
.select-btn .arrow-dwn{
display: flex;
height: 21px;
width: 21px;
color: #fff;
font-size: 14px;
border-radius: 50%;
background: rgb(91, 73, 255);
align-items: center;
justify-content: center;
transition: 0.3s;
}
.select-btn.open .arrow-dwn{
transform: rotate(-180deg);
}
.list-items{
position: relative;
margin-top: 15px;
border-radius: 8px;
padding: 16px;
background-color: #fff;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
max-height: 220px;
overflow-y: scroll;
overflow-x: hidden;
display: none;
}
.select-btn.open ~ .list-items{
display: block;
}
/* Always show the scrollbar of the dropdown */
.select-btn.open ~ .list-items::-webkit-scrollbar {
width: 8px;
height: 0;
}
.select-btn.open ~ .list-items::-webkit-scrollbar-thumb {
background-color: rgba(0,0,0,.2);
border-radius: 8px;
}
.select-btn.open ~ .list-items::-webkit-scrollbar-thumb:hover {
background-color: rgba(0,0,0,.3);
}
.list-items .item{
display: flex;
align-items: center;
list-style: none;
height: 50px;
cursor: pointer;
transition: 0.3s;
padding: 0 15px;
border-radius: 8px;
}
.list-items .item:hover{
background-color: #e7edfe;
}
.item .item-text{
font-size: 16px;
font-weight: 400;
color: #333;
}
.item .checkbox{
display: flex;
align-items: center;
justify-content: center;
height: 16px;
width: 16px;
border-radius: 4px;
margin-right: 12px;
border: 1.5px solid #c0c0c0;
transition: all 0.3s ease-in-out;
}
.item.checked .checkbox{
background-color: rgb(91, 73, 255);
border-color: rgb(91, 73, 255);
}
.checkbox .check-icon{
color: #fff;
font-size: 11px;
transform: scale(0);
transition: all 0.2s ease-in-out;
}
.item.checked .check-icon{
transform: scale(1);
}
JS Code
const selectBtn = document.querySelector(".select-btn"),
items = document.querySelectorAll(".item");
selectBtn.addEventListener("click", () => {
selectBtn.classList.toggle("open");
});
items.forEach(item => {
item.addEventListener("click", () => {
item.classList.toggle("checked");
let checked = document.querySelectorAll(".checked"),
btnText = document.querySelector(".btn-text");
if(checked && checked.length > 0){
btnText.innerText = `${checked.length} Selected`;
}else{
btnText.innerText = "Select Language";
}
});
})