Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to add asterisk to required input fields

Tags:

html

css

I don' think I quite understand :before and :after since I'm not able to get this working. I'm simply trying to add an asterisk after required fields in a form.

My code is simple:

<input id="name" type="text" required="required" autofocus="" placeholder="Name" autocomplete="off" value="" maxlength="40" class="required" name="name" />

/* Required field */
.required:after {
  content: '*';
  color: #EF5F5F;
}

Why is this not working?
My fiddle here: http://jsfiddle.net/3EFTN/

like image 702
Steven Avatar asked Mar 31 '13 11:03

Steven


People also ask

What does it mean Fields marked with an asterisk (*) are required?

Fields marked with * are mandatory Using an asterisk (*) symbol content authors notify mandatory field. This is said to be one of the accessible modes of identifying a mandatory field, however this method also will be a problem with screen readers in certain times.

How do you add an asterisk to text?

The asterisk is a punctuation mark that looks like a little star ( * ). The asterisk is made on your keyboard by holding the SHIFT key and pressing the 8 on the top number line. We use the asterisk in English writing to show that a footnote, reference or comment has been added to the original text.


2 Answers

Generated content is added as a child of the item you're adding it to. So the browser is doing this (some attributes skipped for brevity):

<input class="required">
  <span class="after">*</span>
</input>

Which is not valid.

Take a look at spec to understand it better (it has examples about how the browser interprets it).

like image 170
Alessandro Vendruscolo Avatar answered Sep 21 '22 16:09

Alessandro Vendruscolo


:after and :before selectors are not meant to be used for form elements, such as input, so it's impossible to do it with CSS. I'd recommend using jQuery for that or adding another <span> after the element.

like image 34
Lemurr Avatar answered Sep 20 '22 16:09

Lemurr