How do I style form drop down lists? - css

I have searched far and wide on the Internet but have not found anything helpful regarding how to style the dropdown portion of a dropdown list in a form. I would appreciate a pointer in the right direction. Thanks.

I've been working on the same problem for a while. Came up with a pretty simple solution using a holder div that is shorter then the dropdown itself. I also use a background image to get the dropdowns arrow to look the way I like. Check it out http://www.danielneumann.com/blog/how-to-style-dropdown-with-css-only/
All you need is a div around the select tag and 2 CSS classes.
HTML:
<div class="mainselection">
<select name="State" id="input7">
<option></option>
<option value="Alabama">Alabama</option>
...
<option value="Wisconsin">Wisconsin</option>
<option value="Wyoming">Wyoming</option>
</select>
</div>
CSS:
.mainselection {
overflow:hidden;
width:350px;
margin-left:35px;
background: url("images/dropdown_arrow.png") no-repeat #fff 319px 2px;
/* dropdown_arrow.png is a 31x28 image */
}
select {
border:0;
background:transparent;
height:32px;
border:1px solid #d8d8d8;
width:350px;
-webkit-appearance: none;
}
Then after a little Javascript verification, I can also switch the class on the div to .dropdownbad to give it a red border.
.dropdownbad {
border:2px solid #c13339;
}
The default and error states are shown here:

You can apply styles using the SELECT selector or applying a classname to a SELECT element. However, you'll run into issues with IE < 8 applying things like borders to the element.
You can then target options by using the OPTION selector.
SELECT { border: solid 1px red; font-weight: bold; }
OPTION { background:green; font-style: italic; }
Should give you a drop down with a red border (if using FF or IE8 in Standards mode) with bold text, and the options should be italic with a green background.

Check out this website for CSS only solution:
http://www.htmllion.com/default-select-dropdown-style-just-css.html
HTML:
<form>
<select>
<option>CSS</option>
<option>HTML </option>
<option>HTML 5</option>
</select>
</form>
CSS:
<style>
select {
border: 0 !important; /*Removes border*/
-webkit-appearance: none; /*Removes default chrome and safari style*/
-moz-appearance: none; /* Removes Default Firefox style*/
background: #0088cc url(img/select-arrow.png) no-repeat 90% center;
width: 100px; /*Width of select dropdown to give space for arrow image*/
text-indent: 0.01px; /* Removes default arrow from firefox*/
text-overflow: ""; /*Removes default arrow from firefox*/ /*My custom style for fonts*/
color: #FFF;
border-radius: 15px;
padding: 5px;
box-shadow: inset 0 0 5px rgba(000,000,000, 0.5);
}
</style>

Its possible, but convoluted to say the least. You can't actually style the drop down portion of a drop down list consistantly across different browsers as they all support them in different ways (I mean really varied support).
When I had a problam like this a few months ago, the only solution I found was to, using javascript, convert the drop down list into a ul/li drop down menu, which I could style. Of course there are numerous event that need handling, like selecting a value.
Luckly there's a plugin for JQuery that allows this be a trivial task. (The given Brainfault link for this plugin isn't working anymore.)

As mentioned above it's pretty much impossible to do using straight html, I have had good results with jQuery Combobox though.

Since this question was asked, browser technology has far improved. You can now create a custom dropdown menu entirely using CSS with no javascript.
Check out this blog post:
http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu

I have dropdowns in my cart that were light gray if not selected. I was able to turn the text black with this:
#customer_details ul {
color: black !important;
}
That was all I needed to change, so I can't say what else you could do.

Related

i.e set border to option tag

I am trying to creatre border to each option element from :
<select multiple class="myScrollDiv" name="selectedGroup" [(ngModel)]="selectedGroup">
<option *ngFor="let group of groupOptions" [ngValue]="group" class="myul-{{group.g}}" >
{{group.g}}
</option>
</select>
my css:
.myExtDialog .myScrollDiv
{ display: block;
height: 234px;
width : 100%;
overflow: auto;
border: none;
background-color:whitesmoke;
line-height: 16px;
text-align: left;
}
.myExtDialog .myScrollDiv option
{
border : 1px grey solid ;
}
the css works o.k. in chrome but in i.e. it doesn't show borders for the option tags .
how can i fix this ?
Unfortunately all browsers style their own form components differently so it's very unlikely you'll get the result you want using pure CSS that works across all main browsers.
This article is quite old but the information is still relevant - https://css-tricks.com/dropdown-default-styling/
It's easy to style the select tag itself (easier if you apply appearance: none; which will remove the browser's default styles), but not to style the option tags.
If you really need to style the option tags you might have to consider javascript.

How to put a background image for a select/dropdown in ie7?

Seems background-image doesn't work on <select> in ie7.
I'm wondering if anybody had luck using any of the filters like AlphaImageLoader or so or any other ideas ?
Please see my fiddle here.
You won't be able to do this. Select boxes have very limited styling capabilities in old IE versions, and this is certainly way beyond their capabilities. I note that it does work in IE8, which is something of a surprise, but I'm pretty certain you'll find it impossible in IE7.
Given that, the only way you'll get anywhere close to this in IE7 is by writing your own selectbox replacement widget in Javascript (or using an existing third party one). Frankly, that seems like overkill for a browser with such low usage these days.
#Spudley is correct.
However, you could do this... which is a huge hack.
Basically, put the img in a div and position it over the select box. You would also need to add some to the option elements
HTML
<select class="example">
<option> dublin</option>
<option> wicklow</option>
<option> kerry</option>
<option> galway</option>
<option> tipperary</option>
<option> cork</option>
</select>
<div></div>
CSS
div{
background-image: url('http://i.stack.imgur.com/Kkwug.png');
background-repeat:no-repeat;
background-position:1% 45%;
height:50px;
width:25px;
position:relative;
top:-35px;
left:3px;
z-index:2000;
}
.example{
background:#F37D7D;
position:relative;
width:100px;
}
Here is the demo: http://fiddle.jshell.net/fQPR4/12/ or http://fiddle.jshell.net/fQPR4/12/show/
NB: You would need to hack the top rule in order for it to work and look the same in IE7 and modern browsers.
Do this:
.example {
background: url(http://www.stackoverflow.com/favicon.ico) 96% / 25% no-repeat #fff;
}
select::-ms-expand {
display: none;
}
select{
border: 1px solid #ccc;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
<select class="example">
<option>dublin</option>
<option>wicklow</option>
<option>kerry</option>
<option>galway</option>
<option>tipperary</option>
<option>cork</option>
</select>
<select class="example">
<option>dublin</option>
<option>wicklow</option>
<option>kerry</option>
<option>galway</option>
<option>tipperary</option>
<option>cork</option>
</select>

Change IE background color on unopened, focused select box

I'd like to change the blue background color from IE when a drop down is focused, but I can't seem to find any CSS to do this.
<select id=focusSelect><option>Option</option></select>
JS:
document.getElementById("focusSelect").focus();
CSS:
select:focus{
background-color: red;
}
http://jsfiddle.net/TafDD/3/
Specifically this is for when the drop down is not open. Styling the options is not a problem.
I also can't find any definitive answer on whether this is possible to do at all.
Setting the option background color also does not clear the blue color.
option {
background-color: green;
}
http://jsfiddle.net/srycroft/yE2Zg/
In Internet Explorer 11/Edge (not sure about previous versions) you can do this:
select:focus::-ms-value {
color: black;
background: red;
}
You should also specify the font color because it otherwise defaults to white (to originally contrast against the blue), so you'll want to override it too.
Here's a dabblet demo
Appreciate this is an oldish question, but to prevent the blue background on a selected option in a select dropdown in IE, use the MS pseudo element -ms-value as mentioned by WillRice above. Importantly though you need to set a color css attribute as well for the text as this will get defaulted to white.
select::-ms-value {
background: none; /* remove blue background on ie10/ie11 when selected*/
color:#000;
}
More info here
I'm using the CSS below and it is working in latest IE11, Edge, Firefox and Chrome (I have not tested it with earlier browsers). Just remove border-radius and padding if you don't need them. And thanks to willrice for his contribution:
select {
-webkit-appearance: none;
-moz-appearance: none;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
padding: 5px;
}
select:focus::-ms-value {
background: white;
color: black;
}
select::-ms-expand {
display: none;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
padding: 5px;
}
I've been fiddling around with css and javascript and have searched the internet to find a solution. Unfortunately it looks like it's not possible to change IE's blue highlight itself. In the following example I've used a combination of CSS an JS to achieve nearly the same result in ie as you have on http://jsfiddle.net/TafDD/3/ . Have a look at it.
An example is worth a thousand words: (tested in IE7)
<!DOCTYPE html>
<html>
<head>
<title>CSS Form Select Focus Color Change Test Page</title>
<style type="text/css">
/* Set the desired background color for the whole select element */
form select {
background-color: #fff;
}
form select option {
background: transparent;
}
/* Set the desired color for the focus state */
select:focus, select.focus {
background-color: #f00;
outline: none;
}
</style>
</head>
<body>
<form action="" method="POST">
<div id="selectWrap">
<select id="focusSelect" name="test_select">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
</div>
</form>
<!--[if lt IE 9]><script>
// NOTE: This is a pure JavaScript variant.
// You could also use something like jQuery.
var selectBox = document.getElementById('focusSelect');
// This will add the .focus class to the select
// giving it the defined background color
selectBox.onfocusin = function() {
this.className = 'focus';
};
// and this will restore the original background
// color by removing the .focus class
selectBox.onfocusout = function() {
this.className = '';
};
// This removes the blue highlight after an option is selected
selectBox.onchange = function() {
this.blur();
};
</script><![endif]-->
</body>
</html>
I hope this helps you.
I also recommend you have a look at:
jQuery.customSelect()
Style a Select Box Using Only CSS
DropKick.js
Custom Style All Your Form Elements with Pure CSS and No JavaScript
…and an overview of 40 Techniques:
Form Elements: 40+ CSS/JS Styling and Functionality Techniques
These sites will give you information on how to further style the select with css and / or javascript.
Have fun reading and happy coding!

Text size for drop down menu/input select not working in Safari

First question...
I'm having trouble getting ANY of the Drop down menu/Input Select's to appear with size 18 font in Safari.
Works fine in FF.
Code:
<form class="form">
<select name="make">
<option value="0"> All</option>
</select>
</form>
Css:
.form input{
font-size:18px;
margin-bottom:0px;
}
Any ideas? Can view live at [http://www.motolistr.com][1]
Best,
Nick
EDIT 1:
Thanks for the quick reply. I added a style to the select itself to avoid confusion. I tried;
<select name='make' style='font-size: 18pt;'>
</select>
And
<select name='make' style='font-size: 18px;'>
</select>
And
<select name='make' style='font-size: 1.3em;'>
</select>
Still not working in SAFARI...Again FF works fine with all 3.
Best,
Nick
To style a select in Safari you first have to turn off the os styling:
select {
-webkit-appearance: none;
}
Funny thing though: If you change the background- or border-properties on your select Safari will all of a sudden also apply your font-size.
I figured out a way that safari will pick up on font-size ... all you need to do is set a border color, like the following.
-webkit-appearance: none; will make you lose all of safari's attributes, like the arrows... below you can increase the size without losing that.
Will Work in Safari
<select style=" font-size: 3em; border: black;">
<option>TEXT</option>
</select>
Won't Work in Safari
<select style=" font-size: 3em;">
<option>TEXT</option>
</select>
It appers select controls are non-stylable in Safari; it always uses its own OS X-style widget drawing routines to display them. Until recently, this was the norm: browsers would typically use plain OS-provided widgets for form fields. CSS2 doesn't really say how styles should apply to form fields (if at all).
Some browsers today apply the select's font style to the options (IE7, Opera); some allow the on-page select and the pop-up options to be styled differently (Mozilla, Chrome), so the best you can do for consistency is:
.form select, .form option {
font: Whatever 18px;
}
But if you absolutely need a stylable drop-down in Safari you will need to write your own clunky ersatz-select in JavaScript. (Or see one of the many existing scripts and framework plugins that do this.)
First off this
.form input{
font-size:18px;
margin-bottom:0px;
}
will not work because you are not styling the select element you are styling input elements. Try this and it will most likely work.
.form select {
font-size:18px;
margin-bottom:0px;
}
At least in Safari 5.1 (I don't have 3 running anymore) you can turn off the default styling with:
select{-webkit-appearance: none}
Then it will conform to your font sizing.
The select technically isn't an input tag. Try assigning a class to your select and set the style for the class.
EDIT: Turns out that Aqua style selects only have three different font sizes available. If you need to set an exact font size, you can turn off Aqua by giving the item a background color, then set the size. FYI, it appears that 20px works without setting the background so it must size up to the next supported Aqua size.
Reference: http://particletree.com/notebook/design-friendly-select-elements-in-safari-3/. Test page with various styles at http://particletree.com/examples/safari3/drop.html.
<select name='make' class='big-input'>
</select>
.big-input
{
background: #fff; // turns off Aqua
font-size: 18pt; // assuming you meant 18pt, not 18px
margin-bottom: 0px;
}
Setting line-height:100% will constrain the height of the select box for a more consistent look, but it still doesn't affect the actual font size.
In some cases it can help:
select {
-webkit-appearance: menulist-button;
font-size: 30px;
}
I found a way of changing the font size of a select element in Safari through the use of percentages.
Your code then becomes:
<select name='make' style='font-size: 120%;'></select>
For a 13px font size (which I found very appealing).
This is tested in Safari 5.1.3
You can target Safari select tag by doing this:
select {
width: 224px;
line-height: 1.8; (This can be in px too)
}
try this
<style>
select { border:0; color:#000000; background:transparent;
font-size:20px; font-weight:bold; padding:2px 10px; width:378px;
*width:350px; *background:#FFFFFF; -webkit-appearance: none; }
#mainselection { overflow:hidden; width:350px;
-moz-border-radius: 9px 9px 9px 9px;
-webkit-border-radius: 9px 9px 9px 9px;
border-radius: 9px 9px 9px 9px;
box-shadow: 1px 1px 11px #330033;
background: url("img/arrow.gif") no-repeat scroll 319px 5px #FFFFFF;
}
</style>

Any way to remove IEs black border around submit button in active forms?

I am implementing a design that uses custom styled submit-buttons. They are quite simply light grey buttons with a slightly darker outer border:
input.button {
background: #eee;
border: 1px solid #ccc;
}
This looks just right in Firefox, Safari and Opera. The problem is with Internet Explorer, both 6 and 7.
Since the form is the first one on the page, it's counted as the main form - and thus active from the get go. The first submit button in the active form receives a solid black border in IE, to mark it as the main action.
If I turn off borders, then the black extra border in IE goes away too. I am looking for a way to keep my normal borders, but remove the outline.
Well this works here:
<html>
<head>
<style type="text/css">
span.button {
background: #eee;
border: 1px solid #ccc;
}
span.button input {
background:none;
border:0;
margin:0;
padding:0;
}
</style>
</head>
<body>
<span class="button"><input type="button" name="..." value="Button"/></span>
</body>
</html>
if you dont want to add a wrapper to the input / button then try doing this. As this is invalid CSS then make sre its for IE only. Have the border as per for other browsers but use the filter:chroma for IE...
<!--[if IE]>
<style type="text/css">
input {
filter:chroma(color=#000000);
border:none;
}
</style>
<![endif]-->
worked for me.
I know I'm almost 2 years late to the game, but I found another solution (at least for IE7).
If you add another input type="submit" to your form before any other submit button in the form the problem will go away. Now you just need to hide this new, black-border-absorbing-button.
This works for me (overflow needs to be "auto"):
<input type="submit" value="" style="height:0;overflow:auto;position:absolute;left:-9999px;" />
Note: I am using an HTML5 doctype (<!doctype html>).
I've found an answer that works for me on another forum. It removes the unwanted black border in ie6 and ie7. It's probable that some/many of you have not positioned your input="submit" in form tags. Don't overlook this. It worked for me after trying everything else.
If you are using a submit button, make sure it is within a form and not just a fieldset:
<form><fieldset><input type="submit"></fieldset></form>
I was able to combine David Murdoch's suggestion with some JQuery such that the fix will automatically be applied for all 'input:submit' elements on the page:
// Test for IE7.
if ($.browser.msie && parseInt($.browser.version, 10) == 7) {
$('<input type="submit" value="" style="height:0;overflow:auto;position:absolute;left:-9999px;" />')
.insertBefore("input:submit");
}
You can include this in a Master Page or equivalent, so it gets applied to all pages in your site.
It works, but it does feel a bit wrong, somehow.
I'm building on #nickmorss's example of using filters which didn't really work out for my situation... Using the glow filter instead worked out much better for me.
<!--[if IE]>
<style type="text/css">
input[type="submit"], input[type="button"], button
{
border: none !important;
filter: progid:DXImageTransform.Microsoft.glow(color=#d0d0d0,strength=1);
height: 24px; /* I had to adjust the height from the original value */
}
</style>
<![endif]-->
Right, well here's an ugly fix for you to weigh up... Stick the button in a <span>, nuke the border on the button and give the border to the span instead.
IE is a bit iffy about form element margins so this might not work precisely. Perhaps giving the span the same background as the button might help in that respect.
span.button {
background: #eee;
border: 1px solid #ccc;
}
span.button input {
background: #eee;
border:0;
}
and
<span class="button"><input type="button" name="..." value="Button"/></span>
The best solution I have found, is to move the border to a wrapping element, like this:
<div class='submit_button'><input type="submit" class="button"></div>
With this CSS:
.submit_button { width: 150px; border: 1px solid #ccc; }
.submit_button .button { width: 150px; border: none; }
The main problem with this solution is that the button now is a block-element, and needs to be fixed-width. We could use inline-block, except that Firefox2 does not support it.
Any better solutions are welcome.
I think
filter:chroma(color=#000000); as metnioned a wile ago is the best as you can apply in certain class. Otherwise you will have to go and apply an extra tag on every button you have that is if you are using classes of course.
.buttonStyle {
filter:chroma(color=#000000);
BACKGROUND-COLOR:#E5813C solid;
BORDER-BOTTOM: #cccccc 1px solid;
BORDER-LEFT: #cccccc 1px solid;
BORDER-RIGHT: #cccccc 1px solid;
BORDER-TOP: #cccccc 1px solid; COLOR:#FF9900;
FONT-FAMILY: Verdana, Arial, Helvetica, sans-serif;
FONT-SIZE: 10px; FONT-WEIGHT: bold;
TEXT-DECORATION: none;
}
That did it for me!
I had this problem and solved it with a div around the button, displayed it as a block, and positioned it manually. the margins for buttons in IE and FF was just too unpredictable and there was no way for them both to be happy. My submit button had to be perfectly lined up against the input, so it just wouldnt work without positioning the items as blocks.
This is going to work:
input[type=button]
{
filter:chroma(color=#000000);
}
This works even with button tag, and eventually you can safely use the background-image css property.
The correct answer to this qustion is:
outline: none;
... works for IE and Chrome, in my knowledge.
A hackish solution might be to use markup like this:
<button><span>Go</span></button>
and apply your border styles to the span element.
add *border:none
this removes the border for IE6 and IE7, but keeps it for the other browsers
With the sliding doors technique, use two spans inside of the button. And eliminate any formatting on the button in your IE override.
<button><span class="open">Search<span class="close"></span></span></button>
I can't comment (yet) so I have to add my comment this way. I thing Mr. David Murdoch's advice is the best for Opera ( here ). OMG, what a lovely girl he's got btw.
I've tried his approach in Opera and I succeeded basically doubling the input tags in this way:
<input type="submit" value="Go" style="display:none;" id="WorkaroundForOperaInputFocusBorderBug" />
<input type="submit" value="Go" />
This way the 1st element is hidden but it CATCHES the display focus Opera would give to the 2nd input element instead. LOVE IT!
At least in IE7 you can style the border althogh you can't remove it (set it to none).
So setting the color of the border to the same color that your background should do.
.submitbutton {
background-color: #fff;
border: #fff dotted 1px;
}
if your background is white.
For me the below code actually worked.
<!--[if IE]>
<style type="text/css">
input[type=submit],input[type=reset],input[type=button]
{
filter:chroma(color=#000000);
color:#010101;
}
</style>
<![endif]-->
Got it from #Mark's answer and loaded it only for IE.

Resources