Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  85] [ 4]  / answers: 1 / hits: 54153  / 7 Years ago, mon, august 7, 2017, 12:00:00

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.


More From » html

 Answers
2

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;
}

[#56841] Friday, August 4, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
danyelletyanah

Total Points: 204
Total Questions: 109
Total Answers: 108

Location: Vanuatu
Member since Fri, Oct 22, 2021
3 Years ago
;