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.
I used the
background-image
to create the red dot for required fields.View on Codepen
Summary
It does not work with
<input type="button">
, but it works fine with<input type="checkbox">
.Fiddle: http://jsfiddle.net/gb2wY/50/
HTML:
CSS:
:before
and:after
only works for nodes that can have child nodes since they insert a new node as the first or last node.Oddly, it works with some types of input. At least in Chrome,
works fine, same as
It's just
type=text
and some others that don't work.You have to have some kind of wrapper around the input to use a before or after pseudo-element. Here's a fiddle that has a before on the wrapper div of an input and then places the before inside the input - or at least it looks like it. Obviously, this is a work around but effective in a pinch and lends itself to being responsive. You can easily make this an after if you need to put some other content.
Working Fiddle
Dollar sign inside an input as a pseudo-element: http://jsfiddle.net/kapunahele/ose4r8uj/1/
The HTML:
The CSS:
Here's another approach (assuming you have control of the HTML): add an empty
<span></span>
right after the input, and target that in CSS usinginput.mystyle + span:after
I'm using this approach in AngularJS because it will add
.ng-invalid
classes automatically to<input>
form elements, and to the form, but not to the<label>
.