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

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

    .asterisc {
      display: block;
      color: red;
      margin: -19px 185px;
    }
    <input style="width:200px">
    <span class="asterisc">*</span>

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

    For those who end up here, but have jQuery:

    // javascript / jQuery
    $("label.required").append('<span class="red-star"> *</span>')
    
    // css
    .red-star { color: red; }
    
    0 讨论(0)
  • 2020-11-28 19:30

    Use jQuery and CSS

    jQuery(document).ready(function() {
     jQuery("[required]").after("<span class='required'>*</span>");
    });
    .required {
        position: absolute;
        margin-left: -10px;
        color: #FB0000;
        font-size: 15px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="text" value="xxx" required>

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

    Here is a simple "CSS only" trick I created and am using to dynamically add a red asterisk on the labels of required form elements without losing browsers' default form validation.

    The following code works perfectly on all the browsers and for all the main form elements.

    .form-group {
      display: flex;
      flex-direction: column;
    }
    
    label {
      order: 1;
      text-transform: capitalize;
      margin-bottom: 0.3em;
    }
    
    input,
    select,
    textarea {
      padding: 0.5em;
      order: 2;
    }
    
    input:required+label::after,
    select:required+label::after,
    textarea:required+label::after {
      content: " *";
      color: #e32;
    }
    <div class="form-group">
      <input class="form-control" name="first_name" id="first_name" type="text" placeholder="First Name" required>
      <label class="small mb-1" for="first_name">First Name</label>
    </div>
    
    <br>
    
    <div class="form-group">
      <input class="form-control" name="last_name" id="last_name" type="text" placeholder="Last Name">
      <label class="small mb-1" for="last_name">Last Name</label>
    </div>

    Important: You must preserve the order of elements that is the input element first and label element second. CSS is gonna handle it and transform it in the traditional way, that is the label first and input second.

    0 讨论(0)
  • 2020-11-28 19:32
    input[required]{
        background-image: radial-gradient(#F00 15%, transparent 16%), radial-gradient(#F00 15%, transparent 16%);
        background-size: 1em 1em;
        background-position: right top;
        background-repeat: no-repeat;
    }
    
    0 讨论(0)
  • 2020-11-28 19:36

    What you need is :required selector - it will select all fields with 'required' attribute (so no need to add any additional classes). Then - style inputs according to your needs. You can use ':after' selector and add asterisk in the way suggested among other answers

    0 讨论(0)
提交回复
热议问题