Add span inside form's placeholder

后端 未结 7 1322
深忆病人
深忆病人 2020-12-03 12:17

I wanna add a color asterix in my form\'s placeholder, is it possible? \"enter

相关标签:
7条回答
  • 2020-12-03 12:33

    At first glance it doesn't seem possible, but it may be a good alternative to create your own fake spanholder element:

    <div class="holder">Email Address <span class="red">*</span></div>
    <input id="input" size="18" type="text" />
    

    Fiddle

    0 讨论(0)
  • 2020-12-03 12:34

    Here is pure css solution IE10+

    .input-placeholder {
      position: relative;
    }
    .input-placeholder input {
      padding: 10px;
      font-size: 25px;
    }
    .input-placeholder input:valid + .placeholder {
      display: none;
    }
    
    .placeholder {
      position: absolute;
      pointer-events: none;
      top: 0;
      bottom: 0;
      height: 25px;
      font-size: 25px;
      left: 10px;
      margin: auto;
      color: #ccc;
    }
    
    .placeholder span {
      color: red;
    }
    <form novalidate>
        <div class="input-placeholder">
            <input type="text" required>
            <div class="placeholder">
                Email <span>*</span>
            </div>
        </div>
        <input type="submit">
    </form>

    0 讨论(0)
  • 2020-12-03 12:34

    As far as I know, this is not possible.

    One solution I have seen used in the past is to add a background-image of a red asterisk to your input field, but that makes it difficult to duplicate the visual alignment you are going for. More info on this method: Use CSS to automatically add 'required field' asterisk to form inputs

    Another solution would be to add the span (and placeholder text) outside of the input field, but that would require some JavaScript to control when it is and isn't visible.

    Here is a JSFiddle I just created for this method (using jQuery): http://jsfiddle.net/nLZr9/

    HTML

    <form>
        <div class="field">
            <label class="placeholder" for="email">
                Email Address
                <span class="red">*</span>
            </label>
            <input id="email" type="text" />
        </div>
    </form>
    

    CSS

    .field {
        position: relative;
        height: 30px;
        width: 200px;
    }
    input, label {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        top: 0;
        background: transparent;
        border: 0;
        padding: 0;
        margin: 0;
        text-indent: 5px;
        line-height: 30px;
    }
    

    JS

    $('.field input')
        .on( 'focus', function () {         
            $(this).siblings('label').hide();
        } )
        .on( 'blur',  function () {
            if ( !$(this).val() )
                $(this).siblings('label').show();
        } );
    
    0 讨论(0)
  • 2020-12-03 12:34

    This is a good case for pseudo-elements.

    .someclass {
          position:relative;
    }
    
    .someclass:after {
          position:absolute;
          top:0;
          right:10px;
          content: "*";
          color:#ff0000
    }
    

    ...adjust to your own layout.

    0 讨论(0)
  • 2020-12-03 12:35

    I found a jQuery plugin that might suit you, the pholder plugin.
    If you look the demo, the placholder of the "Name" field is red.

    There may be other plugins or maybe you can edit it. :)

    0 讨论(0)
  • 2020-12-03 12:47

    You can try the following :

    HTML

    <div class="hold">
        <input type="text" placeholder="" required="required">
        <span class="req_placeholder">Name <span>*</span></span>
    </div>
    

    CSS

    .hold {
        position: relative;
    }
    
    input[required="required"]:valid + .req_placeholder {
        display: none;
    }
    
    .req_placeholder {
        position: absolute;
        top: 2px;
        left: 2px;
    }
    
    .req_placeholder span {
        color: red;
    }
    
    0 讨论(0)
提交回复
热议问题