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

前端 未结 16 2670
有刺的猬
有刺的猬 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:45

    You can achieve the desired result by encapsulating the HTML code in a div tag which contains the "required' class followed by the "form-group" class. *however this works only if you have Bootstrap.

    <div class="form-group required">
        <div class="required">
            <label>Name:</label>
            <input type="text">
        </div>
      <div>
    
    0 讨论(0)
  • 2020-11-28 19:46
    .required label {
        font-weight: bold;
    }
    .required label:after {
        color: #e32;
        content: ' *';
        display:inline;
    }
    

    Fiddle with your exact structure: http://jsfiddle.net/bQ859/

    0 讨论(0)
  • 2020-11-28 19:49

    To put it exactly INTO input as it is shown on the following image:

    enter image description here

    I found the following approach:

    .asterisk_input::after {
    content:" *"; 
    color: #e32;
    position: absolute; 
    margin: 0px 0px 0px -20px; 
    font-size: xx-large; 
    padding: 0 5px 0 0; }
    
     <form>
        <div>              
            <input type="text" size="15" />                                 
            <span class="asterisk_input">  </span>            
        </div>            
    </form> 
    

    Site on which I work is coded using fixed layout so it was ok for me.

    I'm not sure that that it's good for liquid design.

    0 讨论(0)
  • 2020-11-28 19:50

    I think this is the efficient way to do, why so much headache

        <div class="full-row">
         <label for="email-id">Email Address<span style="color:red">*</span></label>
      <input type="email" id="email-id" name="email-id"  ng-model="user.email" >
        </div> 
    
    0 讨论(0)
提交回复
热议问题