Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 :after pseudo element with input [duplicate]

Just playing with :before and :after.

It seems I can't use them on/with an input button? Thought I could potentially use it do display an asterisk for required fields. Not necessarily what I will do, just an option

input {
    position: relative;
}
input:after {
    content: ''; 
    background: url(accept.png) 0 0 no-repeat; 
    height: 16px; 
    position: absolute; 
    right: -20px; 
    top: 5px; 
    width: 16px;
}

Same code works fine on an li

li {
    clear: both; 
    margin: 0 0 10px; 
    position: relative;
}
li:after {
    content: ''; 
    background: url(accept.png) 0 0 no-repeat; 
    height: 16px; 
    position: absolute; 
    right: -20px; 
    top: 5px; 
    width: 16px;   
}
like image 568
user508605 Avatar asked Dec 06 '10 18:12

user508605


People also ask

Can you have 2 after pseudo elements?

You can't add two ::after pseudo-elements to one DOM element.

Can I use after on input?

As Robert Koritnik's answer points out, :before and :after can only be applied to container elements and input elements are not containers.

Can inputs have pseudo elements?

Elements like inputs, images, and any other self closing element can't use pseudo elements because they aren't “container elements”. Meaning, they don't allow any nested elements or content inside of them.

What is after pseudo element?

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.


1 Answers

I've also thought that the same thing would be useful, but alas, the simplest way I have been able to get the before/after pseudo elements to work reliably is by wrapping the input in a SPAN tag and applying a class to that.

This discussion provides some insight as to why input:after doesn't work: http://forums.mozillazine.org/viewtopic.php?f=25&t=291007&start=0&st=0&sk=t&sd=a

You can do something like:

.required:after {
    content: "REQUIRED";
    color: orange;
    margin-left: 1em; /* space between the input element and the text */
    padding: .1em;
    background-color: #fff;
    font-size: .8em;
    border: .1em solid orange;
}

<span class="required"><input id="foo" /></span>
like image 64
Tim M. Avatar answered Sep 30 '22 12:09

Tim M.