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

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

    Is that what you had in mind?

    http://jsfiddle.net/erqrN/1/

    <label class="required">Name:</label>
    <input type="text">
    
    <style>
      .required:after {
        content:" *";
        color: red;
      }
    </style>
    

    .required:after {
      content:" *";
      color: red;
    }
    <label class="required">Name:</label>
    <input type="text">

    See https://developer.mozilla.org/en-US/docs/Web/CSS/pseudo-elements

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 19:39
    input[required], select[required] {
        background-image: url('/img/star.png');
        background-repeat: no-repeat;
        background-position-x: right;
    }
    

    Image has some 20px space on the right not to overlap with select dropdown arrow

    enter image description here

    And it looks like this: enter image description here

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

    write in CSS

    .form-group.required .control-label:after {content:"*";color:red;}
    

    and HTML

    <div class="form-group required">
    
        <label class="control-label">Name:</label>
    
        <input type="text">
    
    </div>
    
    0 讨论(0)
  • 2020-11-28 19:42

    It is 2019 and previous answers to this problem are not using

    1. CSS grid
    2. CSS variables
    3. HTML5 form elements
    4. SVG in CSS

    CSS grid is the way to do forms in 2019 as you can have your labels preceding your inputs without having extra divs, spans, spans with asterisks in and other relics.

    Here is where we are going with minimal CSS:

    The HTML for the above:

    <form action="https://www.example.com/register/" method="post" id="form-validate" enctype="multipart/form-data">
        <p class="form-instructions">Please enter the following information to create your account.</p>
        <label for="firstname">First name</label>
        <input type="text" id="firstname" name="firstname" value="" title="First name" maxlength="255" required="">
        <label for="lastname">Last name</label>
        <input type="text" id="lastname" name="lastname" value="" title="Last name" maxlength="255" required="">
        <label for="email_address">Email address</label>
        <input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" name="email" id="email_address" value="" title="Email address" size="30" required="">
        <label for="password">Password</label>
        <input type="password" name="password" id="password" title="Password" required="">
        <label for="confirmation">Confirm password</label>
        <input type="password" name="confirmation" title="Confirm password" id="confirmation" required="">
        <input type="checkbox" name="is_subscribed" title="Subscribe to our newsletter" value="1" id="is_subscribed" class="checkbox">
        <label for="is_subscribed">Subscribe to the newsletter</label>
        <input type="checkbox" name="persistent_remember_me" id="remember_meGCJiRe0GbJ" checked="checked" title="Remember me">
        <label for="remember_meGCJiRe0GbJ">Remember me</label>
        <p class="required">* Required</p>
        <button type="submit" title="Register">Register</button>
    </form>
    

    Placeholder text can be added too and is highly recommended. (I am just answering this mid-form).

    Now for the CSS variables:

    --icon-required: url('data:image/svg+xml,\
    <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="-10 -6 16 16"> \
      <line id="line" y1="-3" y2="3" stroke="%23df0000" stroke-linecap="butt" transform="rotate(15)"></line> \
      <line id="line" y1="-3" y2="3" stroke="%23df0000" stroke-linecap="butt" transform="rotate(75)"></line> \
      <line id="line" y1="-3" y2="3" stroke="%23df0000" stroke-linecap="butt" transform="rotate(-45)"></line> \
    </svg>');
    
    --icon-tick: url('data:image/svg+xml,\
    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100" viewBox="-2 -2 16 16"> \
                <path fill="green" stroke-linejoin="round" d="M2 6L1 7l3 4 7-10h-1L4 8z"/> \
    </svg>');
    

    The CSS for the form elements:

    input[type=text][required],
    input[type=email][required],
    input[type=password][required],
    input[type=tel][required] {
        background-image: var(--icon-required);
        background-position-x: right;
        background-repeat: no-repeat;
        background-size: contain;
    }
    
    input:valid {
        --icon-required: var(--icon-tick);
    }
    

    The form itself should be in CSS grid:

    form {
        align-items: center;
        display: grid;
        grid-gap: var(--form-grid-gap);
        grid-template-columns: var(--form-grid-template-columns);
        margin: auto;
    }
    

    The values for the columns can be set to 1fr auto or 1fr with anything such as <p> tags in the form set to span 1/-1. You change the variables in your media queries so that you have the input boxes going full width on mobile and as per above on desktop. You can also change your grid gap on mobile if you wish by using the CSS variables approach.

    When the boxes are valid then you should get a green tick instead of the asterisk.

    The SVG in CSS is a way of saving the browser from having to do a round trip to the server to get an image of the asterisk. In this way you can fine tune the asterisks, the examples here are at an unusual angle, you can edit this out as the SVG icon above is entirely readable. The viewbox can also be amended to place the asterisk above or below the centre.

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

    This example puts an asterisk symbol in front of a label to denote that particular input as a required field. I set the CSS properties using % and em to makesure my webpage is responsive. You could use px or other absolute units if you want to.

    #name {
       display: inline-block;
       margin-left: 40%;
       font-size:25px;
        
    }
    .nameinput{
       margin-left: 10px;
       font-size:90%;
       width: 17em;
       
    }
    
    .nameinput::placeholder {
      font-size: 0.7em;
      vertical-align: middle;
    }
    
    #name p{
       margin:0;
       border:0;
       padding:0;
       display:inline-block;
       font-size: 40%;
       vertical-align: super;
    }
    <label id="name" value="name">
    <p>*</p> 
    Name:  <input class="nameinput" type="text" placeholder="Enter your name" required>
    </label>

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