Put icon inside input element in a form

前端 未结 17 1600
旧时难觅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:20

    You Can Try this : Bootstrap-4 Beta
    https://www.codeply.com/go/W25zyByhec

    <div class="container">
                <form>
                    <div class="row">
                        <div class="input-group mb-3 col-sm-6">
                          <input type="text" class="form-control border-right-0" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
                            <div class="input-group-prepend bg-white">
                                <span class="input-group-text border-left-0 rounded-right bg-white" id="basic-addon1"><i class="fas fa-search"></i></span>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
    

    



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

    A simple and easy way to position an Icon inside of an input is to use the position CSS property as shown in the code below. Note: I have simplified the code for clarity purposes.

    1. Create the container surrounding the input and icon.
    2. Set the container position as relative
    3. Set the icon as position absolute. This will position the icon relative to the surrounding container.
    4. Use either top, left, bottom, right to position the icon in the container.
    5. Set the padding inside the input so the text does not overlap the icon.

    #input-container {
      position: relative;
    }
    
    #input-container > img {
      position: absolute;
      top: 12px;
      left: 15px;
    }
    
    #input-container > input {
      padding-left: 40px;
    }
    <div id="input-container">
      <img/>
      <input/>
    </div>

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

    A solution without background-images:

    #input_container {
        position:relative;
        padding:0 0 0 20px;
        margin:0 20px;
        background:#ffffd;
        direction: rtl;
        width: 200px;
    }
    #input {
        height:20px;
        margin:0;
        padding-right: 30px;
        width: 100%;
    }
    #input_img {
        position:absolute;
        bottom:2px;
        right:5px;
        width:24px;
        height:24px;
    }
    <div id="input_container">
        <input type="text" id="input" value>
        <img src="https://cdn4.iconfinder.com/data/icons/36-slim-icons/87/calender.png" id="input_img">
    </div>

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

    This works for me for more or less standard forms:

      <button type="submit" value="Submit" name="ButtonType" id="whateveristheId" class="button-class">Submit<img src="/img/selectedImage.png" alt=""></button>
    
    0 讨论(0)
  • 2020-11-22 16:29

    I had situation like this. It didn't work because of background: #ebebeb;. I wanted to put background on the input field and that property was constantly showing up on the top of the background image, and i couldn't see the image! So, I moved the background property to be above the background-image property and it worked.

    input[type='text'] {
        border: 0;
        background-image: url('../img/search.png');
        background-position: 9px 20px;
        background-repeat: no-repeat;
        text-align: center;
        padding: 14px;
        background: #ebebeb;
    }
    

    Solution for my case was:

    input[type='text'] {
        border: 0;
        background: #ebebeb;
        background-image: url('../img/search.png');
        background-position: 9px 20px;
        background-repeat: no-repeat;
        text-align: center;
        padding: 14px;
    }
    

    Just to mention, border, padding and text-align properties are not important for the solution. I just replicated my original code.

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

    Using with font-icon

    <input name="foo" type="text" placeholder="&#61447;">
    

    OR

    <input id="foo" type="text" />
    
    #foo::before
    {
      font-family: 'FontAwesome';
      color:red;
      position: relative;
      left: -5px;
      content: "\f007";    
    }
    
    0 讨论(0)
提交回复
热议问题