Use CSS to automatically add 'required field' asterisk to form inputs

前端 未结 16 2761
有刺的猬
有刺的猬 2020-11-28 19:10

What is a good way to overcome the unfortunate fact that this code will not work as desired:

16条回答
  •  感情败类
    2020-11-28 19:38

    Although this is the accepted answer, please ignore me and use the :after syntax suggested below. My solution is not accessible.


    A similar outcome could be achieved by using a background image of a picture of an asterisk and setting the background of the label/input/the outer div and a padding of the size of the asterisk image. Something like this:

    .required input {
       padding-right: 25px;
       background-image: url(...);
       background-position: right top;
    }
    

    This will put the asterisk INSIDE the text box, but putting the same on div.required instead of .required input will probably be more what you're looking for, if a little less elegant.

    This method doesn't require an additional input.

提交回复
热议问题