Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS :after content below a select element causes click not to work

Tags:

I have this (simplified) css for the select element to get rid of the browser-specific appearance

.select{     display:inline-block;     position:relative; } .select:after{     position:absolute;     bottom:0;right:0;     content:'\2193';  } select{     appearance:none; (-moz and -webkit too)     width:100%;     height:100%; } 

(Best seen in http://jsfiddle.net/kwpke3xh/)

body{      background:#eef;      font-family:sans-serif;  }  .select{      display:inline-block;      background-color:#fff;      border-radius:.5em;      border:.1rem solid #000;      color:#013;      width:8em;      height:1.5em;      vertical-align:middle;      position:relative;  }  .select:after{  	position:absolute;  	bottom:.15em;top:.15em;right:.5rem;  	content:'\2193';   }  select{      -webkit-appearance:none;     -moz-appearance:none;     	appearance:none;      font:inherit;      border:none;      background-color:transparent;      color:inherit;      width:100%;      height:100%;      padding:0 .5em;  }
<span class="select">      <select>          <option>A</option>          <option>B</option>      </select>  </span>

It looks good, aside from Firefox still showing the arrow (as described Firefox 30.0 - -moz-appearance: none not working)

The only technical problem is that when I click on the select element, it shows the option elements, but if I click directly on the arrow, it does not.

Is there a way to avoid this?

like image 482
B7th Avatar asked Sep 11 '14 01:09

B7th


People also ask

How does pseudo-element detect click?

If you must have a click handler on the red region only, you have to make a child element, like a span, place it right after the opening <p> tag, apply styles to p span instead of p:before, and bind to it. Hope it helps!!

What does :: After do in CSS?

::after (:after) In CSS, ::after creates a pseudo-element that is the last child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

Does Z-Index work on pseudo-elements?

It is important to realize that pseudo-elements are considered descendants of their associated element. You may set a negative z-index for these pseudo-elements, but in order for them to actually appear below their parent element, you must create a new stacking context for the parent.

How do I override after CSS?

To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.


Video Answer


1 Answers

The simplest CSS solution would be to add pointer-events: none to the pseudo element. In doing so, you can click through the element because mouse events are removed.

Updated Example

.select:after {     position:absolute;     bottom:.15em;     top:.15em;     right:.5rem;     content:'\2193';     pointer-events: none; } 

(Just take browser support for the property into consideration.)

like image 68
Josh Crozier Avatar answered Nov 13 '22 14:11

Josh Crozier