8

So I'm trying to get the arrow in a select tag to be a custom color. Example:

<input type="checkbox" class="checkbox1" checked="checked" />
<input type="text" spellcheck="false" size="20" />
<select class="dropdown">
    <option value=">">&gt;</option>
    <option value="<">&lt;</option>
    <option value=">=">&ge;</option>
    <option value="<=">&le;</option>
    <option value="==">&#61;</option>
    <option value="!=">&ne;</option>
</select>

I've seen a lot of advice to use an image instead, but I can't do that. Maybe using &#9660;▼ could be part of a solution?

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
bonzo
  • 312
  • 2
  • 4
  • 19
  • 1
    The problem is that CSS cannot replace the arrow in a `select` list since it is rendered by the browser. Moreover, you cannot use the `:before` and `:after` psuedo-selectors on a `select` list which is why I believe that you won't be able to use the `▼` as well. Your best bet is to use the background-image method. – Fahad Hasan Oct 26 '14 at 01:20
  • @takkun Did you check my update? – Michail Michailidis Oct 27 '14 at 15:01

1 Answers1

7

LATEST UPDATE: (jsFiddle: http://jsfiddle.net/ztayse92/) Made the arrow of the previous solution to be under the dropdown and used transparent background. This is as perfect as it can get and it solves the overlay/hover not clickable region problem.

UPDATE: Hack answer without using an image: (jsFiddle: http://jsfiddle.net/swpha0s0/4/)

I made a container with relative positioning and I put the drop-down select and the overlay symbol (arrow) with absolute positioning. In CSS I also remove the arrow styling completely. The only drawback is that since the arrow symbol is an overlay it is not clickable or in other words when the mouse clicks on the arrow the drop down won't open. I made the arrow take least space possible. I also thought of putting a click handler on the arrow and open the drop-down with JavaScript but according to this (Is it possible to use JS to open an HTML select to show its option list?) it is not possible. I made the cursor be default when you hover over arrow so the user doesn't take the wrong feedback that this is clickable as well. So this is the closest solution to what you want - I am curious why no images are allowed and what is the use case for that??

HTML:

<div class="parent">
<select class="dropdown">
    <option value="&gt;">&gt;</option>
    <option value="&lt;">&lt;</option>
    <option value="&gt;=">&ge;</option>
    <option value="&lt;=">&le;</option>
    <option value="==">&#61;</option>
    <option value="!=">&ne;</option>
</select><div class="overlay-arrow">&#9660;</div>
</div>

CSS:

select.dropdown {
     -webkit-appearance: none;       
     -moz-appearance: none;    
     appearance: none;    
     background-position: 22px 3px;                
     background-size: 13px 13px; 
     width: 40px;
    height: 20px;
    margin-left: 4px;
    position: absolute;
    cursor: pointer;

}

.overlay-arrow {
    font-size: 9px;
    color: red;
    position: absolute;
    right: 0px;
    top : 10px;
    cursor: default;
    line-height: 1px;

}

.parent {
    position: relative;
    width: 40px;
    height: 20px;
    float: left;
    margin: 0px;
    padding: 0px;
}

Original Answer with image: (jsFiddle: http://jsfiddle.net/swpha0s0/2/) :

I would suggest you use CSS like that

select.dropdown {
     -webkit-appearance: none;       
     -moz-appearance: none;    
     appearance: none;
     background: url('http://www.durhamcollege.ca/wp-content/themes/dc/images/green_download_arrow.png')  no-repeat;         
     background-position: 22px 3px;                
     background-size: 13px 13px; 
     width: 40px;
     height: 20px;
     margin-left: 4px;
}

.dropdown-container {
     float :left;
     margin: 0px;
}

input {
    margin-top:4px;
    float: left;
    display: block;
}
input[type=checkbox]{
    margin-top:8px;   
}

You have to statically change the background-position and center it the way you like but it is good if you just need to change the arrow appearance or put your own image. It is not possible to do that without image/css or javascript (if you want to change the widget entirely). You can toggle a class when you click it to change the background so you can have another arrow pointing upwards too.

Community
  • 1
  • 1
Michail Michailidis
  • 11,792
  • 6
  • 63
  • 106
  • The issue with your suggestion though is that you're using an image, and as I said in the original post, I can not use an image for a solution. I just want to be able to change the black default arrow to a different color. – bonzo Oct 27 '14 at 00:06
  • From what I know and read this is not possible in any way without images+css or javascript+css. When you said you can't I thought you didn't mean I am not allowed to. But I just thought of a hack and I am going to post it though ;) stay tuned! – Michail Michailidis Oct 27 '14 at 01:52
  • @takkun Please check my updated answer - this is the best it can be done. Please let me know what you think, accept it if it is what you are looking for and please explain the use case as I am curious why no images are allowed for the drop down? I edited your title so it is more explicit too :) – Michail Michailidis Oct 27 '14 at 02:41
  • Btw I just found another answer like that but has the same problem (arrow is not clickable) - http://stackoverflow.com/a/25926156/986160. It is even hackier because it sets the arrow color through the border-colors.. so the borders have the same color as the arrow too.. – Michail Michailidis Oct 27 '14 at 02:52
  • Alright, thanks. I'll look into your suggestions. The reason why I can't use an image, the guys I'm working under don't want me to. I might be able to convince them otherwise because I know, it'd make things a heck of a lot easier. Same for the custom buttons they're expecting me to make. – bonzo Oct 29 '14 at 02:24
  • I see that is so sad! Hopefully they will be able to elaborate the reason.. We are not in the years of text browsers or very low bandwidth... Does the dropdown has to be a ` – Michail Michailidis Oct 29 '14 at 02:42
  • Hey, I'm sorry I haven't replied in a long time. I was temporary moved to another project. About a week ago though I was put back on this problem and I actually found a solution that doesn't use any scripting or an image. Essentially I used css to make the arrow invisible, then attached a ▼ to the div and moved it to the same position as the original dropdown arrow. With that kind of hack, I'm able to change the size with font-size, and the color like any sort of text. Also I know firefox by default wouldn't work with that so I used
    -moz-appearance:none;
    – bonzo Dec 10 '14 at 18:37
  • well this is exactly my first solution if you see - the problem is that it is not clickable as I said and there is no way to propagate the click event - both for the arrow or the image solution I proposed.. I think it deserves upvote/acceptance since it is exactly what you ended up with. Did those guys give you any good reason why this very bad approach is the only way to go?? – Michail Michailidis Dec 11 '14 at 05:46
  • 1
    Dang... I'm dumb. Yeah you're spot on then for that. I guess I just needed to figure it out in my own dumb way. Sorry about all of that. :/ You've been great throughout all of this though. Thank you. – bonzo Dec 15 '14 at 22:47
  • Glad my answer was useful! You can accept it and upvote it so it can be found/considered by other users more easily. Thank you :) – Michail Michailidis Dec 16 '14 at 15:25
  • @takkun I revisited your question and I was able to solve the problem with the cursor by putting the arrow element under the dropdown and making its background transparent (check update) – Michail Michailidis May 07 '15 at 06:46