
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to create custom select boxes with CSS and JavaScript?
To create custom select boxes with CSS and JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <style> .customSelect { position: relative; font-family: Arial, Helvetica, sans-serif; } .customSelect select { display: none; } .select-selected { background-color: rgb(78, 11, 122); } .select-selected:after { position: absolute; content: ""; top: 14px; right: 10px; width: 0; height: 0; border: 6px solid transparent; border-color: #fff transparent transparent transparent; } .select-selected.select-arrow-active:after { border-color: transparent transparent #fff transparent; top: 7px; } .select-items div, .select-selected { color: #ffffff; padding: 8px 16px; border: 1px solid transparent; cursor: pointer; user-select: none; } .select-items { position: absolute; background-color: rgb(138, 29, 148); top: 100%; left: 0; right: 0; z-index: 99; } .select-hide { display: none; } .select-items div:hover, .sameSelected { background-color: rgba(0, 0, 0, 0.1); } </style> </head> <body> <h1>Custom Select Example</h1> <div class="customSelect" style="width:200px;"> <select> <option value="0">Select Animal:</option> <option value="Giraffe">Giraffe</option> <option value="Lion">Lion</option> <option value="Cow">Cow</option> <option value="Dog">Dog</option> <option value="Tiger">Tiger</option> </select> </div> <script> var customSelectEle, i, j, selElmnt, divEle, divEleSelected, c; customSelectEle = document.querySelector(".customSelect"); selElmnt = customSelectEle.getElementsByTagName("select")[0]; divEle = document.createElement("DIV"); divEle.setAttribute("class", "select-selected"); divEle.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML; customSelectEle.appendChild(divEle); divEleSelected = document.createElement("DIV"); divEleSelected.setAttribute("class", "select-items select-hide"); Array.from(selElmnt).forEach((item, index) => { c = document.createElement("DIV"); c.innerHTML = selElmnt.options[index].innerHTML; c.addEventListener("click", function(e) { var y, i, k, selEleParent, selEleSibling; selEleParent = this.parentNode.parentNode.getElementsByTagName( "select" )[0]; selEleSibling = this.parentNode.previousSibling; for (i = 0; i < selEleParent.length; i++) { if (selEleParent.options[i].innerHTML == this.innerHTML) { selEleParent.selectedIndex = i; selEleSibling.innerHTML = this.innerHTML; y = this.parentNode.getElementsByClassName("sameSelected"); for (k = 0; k < y.length; k++) { y[k].removeAttribute("class"); } this.setAttribute("class", "sameSelected"); break; } } selEleSibling.click(); }); divEleSelected.appendChild(c); }); customSelectEle.appendChild(divEleSelected); divEle.addEventListener("click", function(e) { e.stopPropagation(); closeSelect(this); this.nextSibling.classList.toggle("select-hide"); this.classList.toggle("select-arrow-active"); }); function closeSelect(elmnt) { var customSelectEle, y, i, arrNo = []; customSelectEle = document.getElementsByClassName("select-items"); y = document.getElementsByClassName("select-selected"); for (i = 0; i < y.length; i++) { if (elmnt == y[i]) { arrNo.push(i); } else { y[i].classList.remove("select-arrow-active"); } } for (i = 0; i < customSelectEle.length; i++) { if (arrNo.indexOf(i)) { customSelectEle[i].classList.add("select-hide"); } } } document.addEventListener("click", closeSelect); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the select menu, the menu will open as follows −
- Related Articles
- How to create custom range sliders with CSS and JavaScript?\n\n
- How to create custom checkboxes and radio buttons with CSS?
- How to create a custom scrollbar with CSS?
- Align text and select boxes to the same width with HTML and CSS
- Is it possible to select text boxes with JavaScript?
- How to create tabs with CSS and JavaScript?
- How to create popups with CSS and JavaScript?
- How to create Custom Cursor using CSS
- How to create tab headers with CSS and JavaScript?
- How to create full-page tabs with CSS and JavaScript?
- How to create a responsive slideshow with CSS and JavaScript?
- How to create responsive Modal Images with CSS and JavaScript?
- How to create a Modal Box with CSS and JavaScript?
- How to create a scroll indicator with CSS and JavaScript?
- How to create a collapsible section with CSS and JavaScript?

Advertisements