How to style a <select> dropdown with CSS only without JavaScript
Is there a CSS-only way to style a <select>dropdown?
We need to style a <select>form without any JavaScript. What are the properties we can use to do in CSS?
<select> tags can be styled through CSS just like any other HTML element on an HTML page rendered in a browser. Below is an example that will position a select element on the page and render the text of the options in blue.
Example HTML file:
html code
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html><head><title>Select Styling</title><linkhref="selectExample.css"rel="stylesheet"></head><body><selectid="styledSelect"class="blueText"><optionvalue="apple">Apple</option><optionvalue="orange">Orange</option><optionvalue="cherry">Cherry</option></select></body></html>
[ad type=”banner”]
Example CSS file:
css code
/* All select elements on page */select{position: relative;}/* Style by class. Effects the text of the contained options. */.blueText{color:#0000FF;}/* Style by id. Effects position of the select drop down. */#styledSelect{left:100px;}
You may style any HTML element by its tag name:
css code
select{font-weight: bold;}
you can also use a CSS class to style, like any other element:
html code
<selectclass="important"><option>Important Option</option><option>Another Important Option</option></select><styletype="text/css">.important{font-weight: bold;}</style>
Styling Select Box with CSS3:
HTML:
html code
<label><select><optionselected> Select Box </option><option>Short Option</option><option>This Is A Longer Option</option></select></label>
CSS:
css code
body, html{background:#444;text-align:center;padding:50px0;}select{padding:3px;margin:0;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;-webkit-box-shadow:03px0#ccc,0-1px#fff inset;-moz-box-shadow:03px0#ccc,0-1px#fff inset;box-shadow:03px0#ccc,0-1px#fff inset;background:#f8f8f8;color:#888;border:none;outline:none;display: inline-block;-webkit-appearance:none;-moz-appearance:none;appearance:none;cursor:pointer;}/* Targetting Webkit browsers only. FF will show the dropdown arrow with so much padding. */@media screen and(-webkit-min-device-pixel-ratio:0){select{padding-right:18px}}label{position:relative}label:after{content:'<>';font:11px"Consolas", monospace;color:#aaa;-webkit-transform:rotate(90deg);-moz-transform:rotate(90deg);-ms-transform:rotate(90deg);transform:rotate(90deg);right:8px;top:2px;padding:002px;border-bottom:1px solid #ddd;position:absolute;pointer-events:none;}label:before{content:'';right:6px;top:0px;width:20px;height:20px;background:#f8f8f8;position:absolute;pointer-events:none;display:block;}
[ad type=”banner”]
How to CSS form drop down style without using JavaScript .
As of Internet Explorer 10, you can use the ::-ms-expand pseudo element selector to style, and hide, the drop down arrow element.
css code
select::-ms-expand{display:none;/* or visibility: hidden; to keep it's space/hitbox */}
The largest variation we have noticed when styling select drop-downs is Safari and Google Chrome rendering (Firefox is fully customizable through CSS).
Here the simplest solution by using WebKit:
css code
select{-webkit-appearance: none;}
It removes the dropdown arrow.
You can add a dropdown arrow using a nearby div with a background, negative margin or absolutely positioned over the select dropdown.
It uses rotated background layers to cut out a dropdown arrow, as pseudo-elements wouldn’t work for the select element.
Css:
css code
select{font:40012px/1.3"Helvetica Neue", sans-serif;-webkit-appearance: none;appearance: none;border:1px solid hotpink;line-height:1;outline:0;color:hotpink;border-color:hotpink;padding:0.65em2.5em0.55em0.75em;border-radius:3px;background:linear-gradient(hotpink,hotpink) no-repeat,linear-gradient(-135deg,rgba(255,255,255,0)50%,white50%) no-repeat,linear-gradient(-225deg,rgba(255,255,255,0)50%,white50%) no-repeat,linear-gradient(hotpink,hotpink) no-repeat;background-color:white;background-size:1px100%,20px20px,20px20px,20px60%;background-position: right 20px center, right bottom, right bottom, right bottom;}
<select>
<option>So many options</option>
<option>...</option>
</select>
Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.