I am trying to design a Select tag as shown in the below figure:
Somehow I managed to design it by wrapping the select tag in a div. but the problem is that when I click the designed arrow, the select tag is not functioning or showing all the lists.
What I am expecting is that when I click on the arrow, the select tag should show all the Options. which is not happening because the arrow section is generated using the parent wrapper elements pseudo elements. I haven't used pseudo element selectors select tag because it seems to be not working.
I can solve this issue using background-image
to the parent wrapper but as I have full rights to change the html as I can, I am looking for better approach without using images or javascript i.e using just CSS.
Here is the fiddle.
<div class="select-wrapper">
<select>
<option>EEE</option>
<option>ECE</option>
<option>EIE</option>
</select>
</div>
.select-wrapper {
display:inline-block;
border:1px solid #bbbbbb;
position:relative;
width:120px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border-radius:5px;
margin-top: 10px;
}
.select-wrapper:before{
content: "";
position:absolute;
right: 5px;
top:8px;
border-width: 5px 5px 5px 5px;
border-style: solid;
border-color: #666666 transparent transparent transparent ;
z-index:3;
}
.select-wrapper:after {
content:"";
display:block;
position:absolute;
top:0px;
bottom:0px;
width:20px;
right:0px;
border-left:1px solid #bababa;
background-color:#ededed;
-webkit-border-top-right-radius:5px;
-moz-border-top-right-radius:5px;
border-top-right-radius:5px;
-webkit-border-bottom-right-radius:5px;
-moz-border-bottom-right-radius:5px;
border-bottom-right-radius:5px;
}
select {
width:100%;
background-color:#ededed;
border:none;
outline:none;
padding:0px;
margin:0px;
position:relative;
}
You can use inline styles to add custome styling to <option> tags. For eg : <option style="font-weight:bold;color:#09C;">Option 1</option> This will apply the styles to this particular <option> element only.
A style attribute on a <select> tag assigns a unique style to the dropdown. Its value is CSS that defines the appearance of the dropdown control.
Selected value is rendered in <div class="select__trigger"> . <div class="arrow"> content is an HTML-Entity which will produce a nice downwards facing arrow for us. <div class="custom-options"> is wrapper of all options. Each option has data-value attribute, which value represents option's value.
Add pointer-events:none;
to your pseudo element classes.
FIDDLE
NB: IE10- doesn't support the pointer-events property (caniuse says that IE11 will, though)
So for IE:
either you'll have to settle with the arrow not being click-able or
you could use use Modernizr to detect whether pointer-events is supported - and if not (IE10-) - revert to the standard built in arrow. (by not using your special styling classes in this case)
.select-wrapper:before{
content: "";
position:absolute;
right: 5px;
top:8px;
border-width: 5px 5px 5px 5px;
border-style: solid;
border-color: #666666 transparent transparent transparent ;
z-index:3;
pointer-events:none; /* <-- */
}
.select-wrapper:after {
content:"";
display:block;
position:absolute;
top:0px;
bottom:0px;
width:20px;
right:0px;
border-left:1px solid #bababa;
background-color:#ededed;
-webkit-border-top-right-radius:5px;
-moz-border-top-right-radius:5px;
border-top-right-radius:5px;
-webkit-border-bottom-right-radius:5px;
-moz-border-bottom-right-radius:5px;
border-bottom-right-radius:5px;
pointer-events:none; /* <-- */
}
Using :after
, :before
pseudo creates a virtual element and you are overlaying that over your select box, and hence you cannot trigger your select
element. The best thing to do is to use background-image
here.. I've made the below from scratch, you can check it out.
Demo
.select_wrap {
width: 180px; /* Make sure it is less than the select width */
overflow: hidden; /* Hide default arrow */
border: 2px solid #515151;
border-radius: 5px;
}
select {
width: 220px; /* Your wish, make sure it overflows parent */
padding: 5px;
border: 0;
background: #f5f5f5; /* Fall back */
background: url(http://s2.postimg.org/s44rm4vbp/Untitled_1.png), linear-gradient(to bottom, #f5f5f5 0%,#f6f6f6 47%,#dddddd 100%);
background-repeat: no-repeat;
background-size: 30px 30px, auto auto;
background-position: 150px 0, center center;
}
I used a css3 property.
pointer-events:none
Check the fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With