8

I am trying to design a Select tag as shown in the below figure:

enter image description here

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;
}
Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271

5 Answers5

4

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; /* <-- */
}
Danield
  • 121,619
  • 37
  • 226
  • 255
  • @ Daniel - Adding pointer-events:none; directly to .select-wrapper (and not :before or :after) does not work. Can you help me understand why? +1 for the answer :) – VJS Oct 14 '13 at 06:07
  • 1
    @VijetaShetty : If you add it to the wrapper - which holds the whole select element - then no pointer events will be registered on the whole select element. – Danield Oct 14 '13 at 06:17
  • Thanks.. this is interesting.. btw, I am still trying to get this using as many nested wrapper elements as possible. if I fail, I will mark your post as the answer. (_that will make my post duplicate_). – Mr_Green Oct 14 '13 at 06:25
  • @Danield Why does it need `pointer-events:none` to be specified in both `.select-wrapper:after` and `.select-wrapper:before`. If I keep only one(either in **`before`** or **`after`**) it registers click only on the text-input segment but not on the button. Why should I keep it in both the style definition? I would be very heppy if you please explain what portion of the output `.select-wrapper:after` and `.select-wrapper:before` signify. Awaiting your response. – Rajesh Paul Oct 14 '13 at 06:55
  • 1
    @RajeshPaul: clicking the select tag triggers the dropdown. If I add (absolutely-positioned) layers on top - then obviously a click on the above layers won't trigger the dropdown. Now both the before and after psedo-elements are layers above the select. The before contains the arrow, and the after contains the background – Danield Oct 14 '13 at 07:04
  • @Danield Thanx for your valuable explanation; What background??? There is no specification of background. Pls explain that. – Rajesh Paul Oct 14 '13 at 08:46
  • In the after pseudo element class: background-color:#ededed; <-- that's where background is defined. It's a layer of width 20px and background #ededed – Danield Oct 14 '13 at 08:57
2

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;
}
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
1

I used a css3 property.

    pointer-events:none

Check the fiddle

VJS
  • 1,017
  • 9
  • 21
  • I was working on the fiddle. I didn't see his answer. Was learning and checking. But thanks for the encouragement Vaibhav. – VJS Oct 14 '13 at 06:05
  • Oh than it's kwel, will take my comment, if it's that so, I will upvote this too :) – Mr. Alien Oct 14 '13 at 06:11
1

This seems to be the closest solution to design the select tag.

Working Fiddle

HTML

<div class="parent">
    <div class="select-wrapper">
        <select>
            <option>EEE</option>
            <option>ECE</option>
            <option>EIE</option>
        </select>
    </div>
</div>

CSS

.select-wrapper {
    display:inline-block;
    position:relative;
    width:140px;
    -webkit-border-radius:5px;
    -moz-border-radius:5px;
    border-radius:5px;
    margin-top: 10px;
    overflow:hidden;
    background-color:#ededed;
    margin-left:-20px;
    border-right: 1px solid #bababa;
}
.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:transparent;
    border:none;
    outline:none;
    padding:0px;
    margin:0px;
    position:relative;
    z-index:4;
    margin-left:20px;
    border: 1px solid #bababa;
    border-right: 0px;
    -webkit-border-radius:5px;
    -moz-border-radius:5px;
    border-radius:5px;
}
.parent {
    display:inline-block;
    overflow:hidden;
}

You can also make it more good using :active selector in combination with :after/:before pseudo element selectors. Something like this:

.select-wrapper:active:before{
    /** css here **/
}
.select-wrapper:active:after{
    /** css here **/
}

Sample fiddle

Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • 1
    Notice that the options jut out to the right of the select element. I wrote about this solution in detail [here (Approach #1)](http://stackoverflow.com/a/13968900/703717) – Danield Oct 14 '13 at 07:10
  • @Danield yup... which I compromised. Actually, I can't go with pointer-events because of no IE support (atleast IE9). Anyway, I will mark your post as answer because you explained this before.. – Mr_Green Oct 14 '13 at 07:15
  • Thanks, @Mr_Green. I like your design as well. :-) – Danield Oct 14 '13 at 07:18
0

you can use my custom select i creat a new design select element based on the orginal with css and jquery

you can download example here

https://github.com/yanivsuzana/new_select

yaniv.s
  • 1
  • 1