Put icon inside input element in a form

前端 未结 17 1599
旧时难觅i
旧时难觅i 2020-11-22 16:10

How do I put an icon inside a form\'s input element?

\"Screenshot

相关标签:
17条回答
  • 2020-11-22 16:33

    The CSS solutions posted by others are the best way to accomplish this.

    If that should give you any problems (read IE6), you can also use a borderless input inside of a div.

    <div style="border: 1px solid #DDD;">
        <img src="icon.png"/>
        <input style="border: none;"/>
    </div>
    

    Not as "clean", but should work on older browsers.

    0 讨论(0)
  • 2020-11-22 16:33
     <label for="fileEdit">
        <i class="fa fa-cloud-upload">
        </i>
        <input id="fileEdit" class="hidden" type="file" name="addImg" ng-file-change="onImageChange( $files )" ng-multiple="false" accept="{{ contentType }}"/>
      </label>
    

    For example you can use this : label with hidden input (icon is present).

    0 讨论(0)
  • 2020-11-22 16:37

    use this css class for your input at start, then customize accordingly:

        
     .inp-icon{
    background: url(https://i.imgur.com/kSROoEB.png)no-repeat 100%;
    background-size: 16px;
     }
    <input class="inp-icon" type="text">

    0 讨论(0)
  • 2020-11-22 16:37

    I achieved this with the code below.

    First, you flex the container which makes the input and the icon be on the same line. Aligning items makes them be on the same level.

    Then, make the input take up 100% of the width regardless. Give the icon absolute positioning which allows it to overlap with the input.

    Then add right padding to the input so the text typed in doesn't get to the icon. And finally use the right css property to give the icon some space from the edge of the input.

    Note: The Icon tag could be a real icon if you are working with ReactJs or a placeholder for any other way you work with icons in your project.

    .inputContainer {
      display: flex;
      align-items: center;
      position: relative;
    }
    
    .input {
      width: 100%;
      padding-right: 40px;
    }
    
    .inputIcon {
      position: absolute;
      right: 10px;
    }
    <div class="inputContainer">
       <input class="input" />
       <Icon class="inputIcon" />
     </div>

    0 讨论(0)
  • 2020-11-22 16:38

    This works for me:

    input.valid {
       border-color: #28a745;
       padding-right: 30px;
       background-image: url('https://www.stephenwadechryslerdodgejeep.com/wp-content/plugins/pm-motors-plugin/modules/vehicle_save/images/check.png');
       background-repeat: no-repeat;
       background-size: 20px 20px;
       background-position: right center;
    }
    <form>
    <label for="name">Name</label>
    <input class="valid" type="text" name="name" />
    </form>

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