?? how to style select input in css

Hello, glad you are here. I am kunaal and today we will see how to make a custom select input, a custom options input. You can see demo below.

Demo

Video Tutorial –

If you find this article hard or for better explanation. You…


This content originally appeared on DEV Community and was authored by Techy Programmers

Hello, glad you are here. I am kunaal and today we will see how to make a custom select input, a custom options input. You can see demo below.

Demo

Video Tutorial -

If you find this article hard or for better explanation. You can watch video tutorial.

If you like the video tutorial. Please consider subscribing my youtube channel.

Let's code

In index.html inside body tag write this

<div class="container">
    <button class="select" name="select" value="options">options</button>
    <div class="options">
        <p class="item active">option 1</p>
        <p class="item">option 2</p>
        <p class="item">option 3</p>
        <p class="item">option 4</p>
    </div>
</div>

And add some CSS

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

*:focus{
    outline: none;
}

body{
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #ff6767;
    font-family: 'roboto', sans-serif;
}

.container{
    position: relative;
}

.select{
    position: relative;
    width: 200px;
    height: 40px;
    border-radius: 10px;
    border: none;
    text-transform: capitalize;
    color: #fff;
    background: #292929;
    text-align: left;
    padding: 0 15px;
    font-size: 16px;
    cursor: pointer;
}

.select::after{
    content: '';
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%) rotate(45deg);
    width: 6px;
    height: 6px;
    border-right: 2px solid #fff;
    border-bottom: 2px solid #fff;
}

.select:hover{
    background: #222222;
}

.select.active{
    background: #222222;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}

.options{
    position: absolute;
    top: 40px;
    left: 0;
    width: 100%;
    height: fit-content;
    background: rgba(0, 0, 0, 0.5);
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    overflow: hidden;
    display: none;
}

.options.active{
    display: block;
}

.options .item{
    color: #fff;
    text-transform: capitalize;
    width: 100%;
    height: 30px;
    padding: 0 15px;
    line-height: 30px;
    cursor: pointer;
}

.options .item.active{
    background: #292929;
}

we also need to write JS. So let's write some

const select = document.querySelector('.select');
const optionBox = document.querySelector('.options');
const options = [...document.querySelectorAll('.options .item')];

let activeOption = 0; // default should be 0

window.onclick = (e) => {
    if(!e.target.className.includes('select')){
        select.classList.remove('active');
        optionBox.classList.remove('active');
    } else{
        select.classList.toggle('active');
        optionBox.classList.toggle('active');
    }
}

options.forEach((item, i) => {
    item.onmousemove = () => {
        hoverOptions(i);
    }
})

const hoverOptions = (i) => {
    options[activeOption].classList.remove('active');
    options[i].classList.add('active');
    activeOption = i;
    setValue();
}

window.onkeydown = (e) => {
    if(select.className.includes('active')){
        e.preventDefault();
        if(e.key === 'ArrowDown' && activeOption < options.length - 1){
            hoverOptions(activeOption + 1);
        } else if(e.key === 'ArrowUp' && activeOption > 0){
            hoverOptions(activeOption - 1);
        } else if(e.key === 'Enter'){
            select.classList.remove('active');
            optionBox.classList.remove('active');
        }
    }
}

const setValue = () => {
    select.innerHTML = select.value = options[activeOption].innerHTML;
}

setValue();

I hope you understood everything. If you have any doubt or you find any mistake that I made or you have any suggestion feel free to ask me in comment.

If you are interested in programming and want to know how I a 15yr old teen do coding make these design. You can follow me on my Instagram. I am also planning to post my game development stuff on Instagram.

My youtube Channel, Instagram


This content originally appeared on DEV Community and was authored by Techy Programmers


Print Share Comment Cite Upload Translate Updates
APA

Techy Programmers | Sciencx (2021-05-29T12:04:49+00:00) ?? how to style select input in css. Retrieved from https://www.scien.cx/2021/05/29/%f0%9f%94%a5%f0%9f%94%a5-how-to-style-select-input-in-css/

MLA
" » ?? how to style select input in css." Techy Programmers | Sciencx - Saturday May 29, 2021, https://www.scien.cx/2021/05/29/%f0%9f%94%a5%f0%9f%94%a5-how-to-style-select-input-in-css/
HARVARD
Techy Programmers | Sciencx Saturday May 29, 2021 » ?? how to style select input in css., viewed ,<https://www.scien.cx/2021/05/29/%f0%9f%94%a5%f0%9f%94%a5-how-to-style-select-input-in-css/>
VANCOUVER
Techy Programmers | Sciencx - » ?? how to style select input in css. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/29/%f0%9f%94%a5%f0%9f%94%a5-how-to-style-select-input-in-css/
CHICAGO
" » ?? how to style select input in css." Techy Programmers | Sciencx - Accessed . https://www.scien.cx/2021/05/29/%f0%9f%94%a5%f0%9f%94%a5-how-to-style-select-input-in-css/
IEEE
" » ?? how to style select input in css." Techy Programmers | Sciencx [Online]. Available: https://www.scien.cx/2021/05/29/%f0%9f%94%a5%f0%9f%94%a5-how-to-style-select-input-in-css/. [Accessed: ]
rf:citation
» ?? how to style select input in css | Techy Programmers | Sciencx | https://www.scien.cx/2021/05/29/%f0%9f%94%a5%f0%9f%94%a5-how-to-style-select-input-in-css/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.