Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a red asterisk to required fields

I am wanting to add a red asterisk for my required fields. So far I have tried using this:

.required-field::before {
  content: "*";
  color: red;
  float: right;
}
<div class="required-field">Issued By:</div>
<input type="text" id="issuedBy">

But the problem is it keeps putting the asterisk too far right. I want it to be right next to the text "Status:" and "Issued By:". I tried removing the float attribute, but it then places the red asterisk in front of the text.

like image 577
cela Avatar asked Aug 07 '17 15:08

cela


2 Answers

Rather than ::before use ::after and remove the float: right.

::before places a pseudoelement before the element you're selecting. ::after will place it after, so rather than putting the asterisk before the element you want and moving it with a float/position you can just place it after naturally.

The reason the asterisk was moved too far to the right is because floating moves the element to the right side of the parent container (not always the parent element, let me know if you want me to elaborate). So by floating right you were telling it to move to the far right of the label container which means just to the left of the text boxes, and not to the right of the label text.

https://jsfiddle.net/h4depmdf/1/

.required-field::after {
    content: "*";
    color: red;
}
like image 143
Don Avatar answered Sep 20 '22 03:09

Don


Hey you are floating the asterisk to right

instead use

.required-field::after {
  content: "*";
  color: red;
  margin-left:2px
}

You are using pseudo ::before selector which is placing the content before the element. Use pseudo ::after to place it after the element.

like image 40
Orlando P. Avatar answered Sep 21 '22 03:09

Orlando P.