Can I use a :before or :after pseudo-element on an

2018-12-31 00:03发布

I am trying to use the :after CSS pseudo-element on an input field, but it does not work. If I use it with a span, it works OK.

<style type="text/css">
.mystyle:after {content:url(smiley.gif);}
.mystyle {color:red;}
</style>

This works (puts the smiley after "buu!" and before "some more")

<span class="mystyle">buuu!</span>a some more

This does not work - it only colors someValue in red, but there is no smiley.

<input class="mystyle" type="text" value="someValue">

What am I doing wrong? should I use another pseudo-selector?

Note: I cannot add a span around my input, because it is being generated by a third-party control.

19条回答
大哥的爱人
2楼-- · 2018-12-31 01:05

You can't put a pseudo element in an input element, but can put in shadow element, like a placeholder!

input[type="text"] {   
  &::-webkit-input-placeholder {
    &:before {
      // your code
    }
  }
}

To make it work in other browsers, use :-moz-placeholder, ::-moz-placeholder and :-ms-input-placeholder in different selectors. Can't group the selectors, because if a browser doesn't recognize the selector invalidates the entire statement.

UPDATE: The above code works only with CSS pre-processor (SASS, LESS...), without pre-processors use:

input[type="text"]::-webkit-input-placeholder:before { // your code }
查看更多
登录 后发表回答